@@ -6,6 +6,7 @@ const mdurl = require('mdurl')
6
6
const fse = require ( 'fs-extra' )
7
7
const escapeStringRegexp = require ( 'escape-string-regexp' )
8
8
const sander = require ( 'sander' )
9
+ const url = require ( 'url' )
9
10
import i18n from 'browser/lib/i18n'
10
11
11
12
const STORAGE_FOLDER_PLACEHOLDER = ':storage'
@@ -159,16 +160,21 @@ function copyAttachment (sourceFilePath, storageKey, noteKey, useRandomName = tr
159
160
160
161
try {
161
162
const isBase64 = typeof sourceFilePath === 'object' && sourceFilePath . type === 'base64'
162
- if ( ! fs . existsSync ( sourceFilePath ) && ! isBase64 ) {
163
+ if ( ! isBase64 && ! fs . existsSync ( sourceFilePath ) ) {
163
164
return reject ( 'source file does not exist' )
164
165
}
165
- const targetStorage = findStorage . findStorage ( storageKey )
166
+
167
+ const sourcePath = sourceFilePath . sourceFilePath || sourceFilePath
168
+ const sourceURL = url . parse ( / ^ \w + : \/ \/ / . test ( sourcePath ) ? sourcePath : 'file:///' + sourcePath )
169
+
166
170
let destinationName
167
171
if ( useRandomName ) {
168
- destinationName = `${ uniqueSlug ( ) } ${ path . extname ( sourceFilePath . sourceFilePath || sourceFilePath ) } `
172
+ destinationName = `${ uniqueSlug ( ) } ${ path . extname ( sourceURL . pathname ) || '.png' } `
169
173
} else {
170
- destinationName = path . basename ( sourceFilePath . sourceFilePath || sourceFilePath )
174
+ destinationName = path . basename ( sourceURL . pathname )
171
175
}
176
+
177
+ const targetStorage = findStorage . findStorage ( storageKey )
172
178
const destinationDir = path . join ( targetStorage . path , DESTINATION_FOLDER , noteKey )
173
179
createAttachmentDestinationFolder ( targetStorage . path , noteKey )
174
180
const outputFile = fs . createWriteStream ( path . join ( destinationDir , destinationName ) )
0 commit comments