1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Merge pull request #667 from asmsuechan/randomize-image-name

Randomize the image name when it's dropped
This commit is contained in:
SuenagaRyota
2017-07-02 22:22:23 +09:00
committed by GitHub
2 changed files with 6 additions and 4 deletions

View File

@@ -193,7 +193,7 @@ export default class CodeEditor extends React.Component {
const filename = path.basename(imagePath) const filename = path.basename(imagePath)
copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => { copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => {
const imageMd = `![${encodeURI(filename)}](${imagePathInTheStorage})` const imageMd = `![${filename}](${imagePathInTheStorage})`
this.insertImageMd(imageMd) this.insertImageMd(imageMd)
}) })
} }

View File

@@ -19,10 +19,12 @@ function copyImage (filePath, storageKey) {
const targetStorage = storage const targetStorage = storage
const inputImage = fs.createReadStream(filePath) const inputImage = fs.createReadStream(filePath)
const imageName = path.basename(filePath) const imageExt = path.extname(filePath)
const outputImage = fs.createWriteStream(path.join(targetStorage.path, 'images', imageName)) const imageName = Math.random().toString(36).slice(-16)
const basename = `${imageName}${imageExt}`
const outputImage = fs.createWriteStream(path.join(targetStorage.path, 'images', basename))
inputImage.pipe(outputImage) inputImage.pipe(outputImage)
resolve(`${encodeURI(targetStorage.path)}/images/${encodeURI(imageName)}`) resolve(`${targetStorage.path}/images/${basename}`)
} catch (e) { } catch (e) {
return reject(e) return reject(e)
} }