@667/ghost-storage-github
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -19,5 +19,4 @@ const path = require('path') | ||
generateUnique(file, dir, name, ext, i) { | ||
let filename, | ||
append = '' | ||
async generateUnique(file, dir, i) { | ||
let append = '' | ||
@@ -28,33 +27,32 @@ if (i) { | ||
if (ext) { | ||
filename = name + append + ext | ||
file.originalName = file.name | ||
if (file.ext) { | ||
file.name = file.name.replace(file.ext, append + file.ext) | ||
} else { | ||
filename = name + append | ||
file.name = file.name + append | ||
} | ||
return this.exists(file, dir).then((exists) => { | ||
// if the file exists but the sha is not the same then create a new filename otherwise don't | ||
if (exists && typeof exists === 'string') { | ||
throw new Error('File already exists') | ||
} else if (exists) { | ||
i = i + 1 | ||
return this.generateUnique(file, dir, name, ext, i) | ||
} else { | ||
return path.posix.join(dir, filename) | ||
} | ||
}) | ||
const exists = await this.exists(file, dir) | ||
// if the file exists but the sha is not the same then create a new filename otherwise don't | ||
if (exists && typeof exists === 'string') { | ||
throw new Error('File already exists') | ||
} else if (exists) { | ||
i = i + 1 | ||
return await this.generateUnique(file, dir, i) | ||
} else { | ||
return path.posix.join(dir, file.name) | ||
} | ||
} | ||
getUniqueFileName(file, targetDir) { | ||
let ext = path.extname(file.name) | ||
ext = ext === '' ? undefined : ext | ||
const name = this.getSanitizedFileName(path.basename(file.name, ext)) | ||
return this.generateUnique(file, targetDir, name, ext, 0) | ||
async getUniqueFileName(file, targetDir) { | ||
const sanitizedName = this.getSanitizedFileName(file.name, file.ext === '' ? undefined : file.ext) | ||
const uniqueName = await this.generateUnique({ ...file, name: sanitizedName }, targetDir, 0) | ||
return uniqueName | ||
} | ||
getSanitizedFileName(fileName) { | ||
getSanitizedFileName(fileName, fileExt) { | ||
const basename = path.basename(fileName, fileExt) | ||
// below only matches ascii characters, @, and . | ||
// unicode filenames like город.zip would therefore resolve to ----.zip | ||
return fileName.replace(/[^\w@.]/gi, '-') | ||
return `${basename.replace(/[^\w@.]/gi, '-')}${fileExt}` | ||
} | ||
@@ -61,0 +59,0 @@ } |
@@ -100,2 +100,6 @@ const https = require('https') | ||
if (!/image/.test(mime)) { | ||
// return non image files as-is | ||
return { buffer: buffer.toString('base64') } | ||
} | ||
if (/image/.test(mime) && mime.match(/image\/(.*)/)[1] !== this.imageFormat) { | ||
@@ -102,0 +106,0 @@ const originalExt = file.ext |
{ | ||
"name": "@667/ghost-storage-github", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Save Ghost Blog Content to Github", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
16491
243