mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
importAttachments
This commit is contained in:
committed by
Junyoung Choi
parent
1be208d96b
commit
85cb94d99d
@@ -895,43 +895,50 @@ class NoteList extends React.Component {
|
|||||||
if (!location.pathname.match(/\/trashed/)) this.addNotesFromFiles(filepaths)
|
if (!location.pathname.match(/\/trashed/)) this.addNotesFromFiles(filepaths)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add notes to the current folder
|
// Add notes to the current folder
|
||||||
addNotesFromFiles (filepaths) {
|
addNotesFromFiles (filepaths) {
|
||||||
const { dispatch, location } = this.props
|
const { dispatch, location } = this.props
|
||||||
const { storage, folder } = this.resolveTargetFolder()
|
const { storage, folder } = this.resolveTargetFolder()
|
||||||
|
|
||||||
if (filepaths === undefined) return
|
if (filepaths === undefined) return
|
||||||
filepaths.forEach((filepath) => {
|
filepaths.forEach((filepath) => {
|
||||||
fs.readFile(filepath, (err, data) => {
|
fs.readFile(filepath, (err, data) => {
|
||||||
if (err) throw Error('File reading error: ', err)
|
if (err) throw Error('File reading error: ', err)
|
||||||
|
|
||||||
fs.stat(filepath, (err, {mtime, birthtime}) => {
|
fs.stat(filepath, (err, {mtime, birthtime}) => {
|
||||||
if (err) throw Error('File stat reading error: ', err)
|
if (err) throw Error('File stat reading error: ', err)
|
||||||
|
|
||||||
|
const content = data.toString()
|
||||||
|
const newNote = {
|
||||||
|
content: content,
|
||||||
|
folder: folder.key,
|
||||||
|
title: path.basename(filepath, path.extname(filepath)),
|
||||||
|
type: 'MARKDOWN_NOTE',
|
||||||
|
createdAt: birthtime,
|
||||||
|
updatedAt: mtime
|
||||||
|
}
|
||||||
|
dataApi.createNote(storage.key, newNote)
|
||||||
|
.then((note) => {
|
||||||
|
|
||||||
|
attachmentManagement.importAttachments(note.content, filepath, storage.key, note.key)
|
||||||
|
.then((newcontent) => {
|
||||||
|
note.content = newcontent;
|
||||||
|
|
||||||
|
dispatch({
|
||||||
|
type: 'UPDATE_NOTE',
|
||||||
|
note: note
|
||||||
|
})
|
||||||
|
hashHistory.push({
|
||||||
|
pathname: location.pathname,
|
||||||
|
query: {key: getNoteKey(note)}
|
||||||
|
})
|
||||||
|
|
||||||
const content = data.toString()
|
|
||||||
const newNote = {
|
|
||||||
content: content,
|
|
||||||
folder: folder.key,
|
|
||||||
title: path.basename(filepath, path.extname(filepath)),
|
|
||||||
type: 'MARKDOWN_NOTE',
|
|
||||||
createdAt: birthtime,
|
|
||||||
updatedAt: mtime
|
|
||||||
}
|
|
||||||
dataApi.createNote(storage.key, newNote)
|
|
||||||
.then((note) => {
|
|
||||||
dispatch({
|
|
||||||
type: 'UPDATE_NOTE',
|
|
||||||
note: note
|
|
||||||
})
|
|
||||||
hashHistory.push({
|
|
||||||
pathname: location.pathname,
|
|
||||||
query: {key: getNoteKey(note)}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
})
|
||||||
|
}
|
||||||
|
|
||||||
getTargetIndex () {
|
getTargetIndex () {
|
||||||
const { location } = this.props
|
const { location } = this.props
|
||||||
|
|||||||
@@ -449,6 +449,45 @@ function getAbsolutePathsOfAttachmentsInContent (markdownContent, storagePath) {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description Copies the attachments to the storage folder and returns the mardown content it should be replaced with
|
||||||
|
* @param {String} markDownContent content in which the attachment paths should be found
|
||||||
|
* @param {String} filepath The path of the file with attachments to import
|
||||||
|
* @param {String} storageKey Storage key of the destination storage
|
||||||
|
* @param {String} noteKey Key of the current note. Will be used as subfolder in :storage
|
||||||
|
*/
|
||||||
|
function importAttachments (markDownContent, filepath, storageKey, noteKey) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let attach_names = markDownContent.match(/!\[.+?]\((?:.+?\..+?\))/g);
|
||||||
|
|
||||||
|
if (!attach_names)
|
||||||
|
resolve(markDownContent);
|
||||||
|
|
||||||
|
else {
|
||||||
|
let promise_array = [];
|
||||||
|
let end_path = []
|
||||||
|
|
||||||
|
for (let i = 0; i < attach_names.length; i++){
|
||||||
|
end_path[i] = attach_names[i].match(/(?<=]\()(?:.+?\..+?)(?=\))/)[0];
|
||||||
|
let begin_path = filepath.match(/\/.+\//)[0];
|
||||||
|
let final_path = path.join(begin_path, end_path[i]);
|
||||||
|
|
||||||
|
promise_array.push(this.copyAttachment(final_path, storageKey, noteKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise.all(promise_array).then((file_names) => {
|
||||||
|
|
||||||
|
for (let j = 0; j < file_names.length; j++) {
|
||||||
|
let new_path = path.join(STORAGE_FOLDER_PLACEHOLDER, noteKey, file_names[j]);
|
||||||
|
markDownContent = markDownContent.replace(end_path[j], new_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(markDownContent);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Moves the attachments of the current note to the new location.
|
* @description Moves the attachments of the current note to the new location.
|
||||||
* Returns a modified version of the given content so that the links to the attachments point to the new note key.
|
* Returns a modified version of the given content so that the links to the attachments point to the new note key.
|
||||||
@@ -656,6 +695,7 @@ module.exports = {
|
|||||||
handlePasteNativeImage,
|
handlePasteNativeImage,
|
||||||
getAttachmentsInMarkdownContent,
|
getAttachmentsInMarkdownContent,
|
||||||
getAbsolutePathsOfAttachmentsInContent,
|
getAbsolutePathsOfAttachmentsInContent,
|
||||||
|
importAttachments,
|
||||||
removeStorageAndNoteReferences,
|
removeStorageAndNoteReferences,
|
||||||
deleteAttachmentFolder,
|
deleteAttachmentFolder,
|
||||||
deleteAttachmentsNotPresentInNote,
|
deleteAttachmentsNotPresentInNote,
|
||||||
|
|||||||
Reference in New Issue
Block a user