From 0b84a372f61bae942749c8ae6aa2c002f0835f6a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nguy=E1=BB=85n=20Vi=E1=BB=87t=20H=C6=B0ng?=
Date: Wed, 28 Aug 2019 21:23:52 +1200
Subject: [PATCH] re-organize attachment functions and updated comments doc
---
browser/lib/utils.js | 17 +++++++-
.../main/lib/dataApi/attachmentManagement.js | 26 +++++++++++-
.../modals/PreferencesModal/StoragesTab.js | 42 ++++---------------
3 files changed, 49 insertions(+), 36 deletions(-)
diff --git a/browser/lib/utils.js b/browser/lib/utils.js
index 4bcc9698..9f6f1425 100644
--- a/browser/lib/utils.js
+++ b/browser/lib/utils.js
@@ -136,9 +136,24 @@ export function isMarkdownTitleURL (str) {
return /(^#{1,6}\s)(?:\w+:|^)\/\/(?:[^\s\.]+\.\S{2}|localhost[\:?\d]*)/.test(str)
}
+export function humanFileSize (bytes) {
+ const threshold = 1000
+ if (Math.abs(bytes) < threshold) {
+ return bytes + ' B'
+ }
+ var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
+ var u = -1
+ do {
+ bytes /= threshold
+ ++u
+ } while (Math.abs(bytes) >= threshold && u < units.length - 1)
+ return bytes.toFixed(1) + ' ' + units[u]
+}
+
export default {
lastFindInArray,
escapeHtmlCharacters,
isObjectEqual,
- isMarkdownTitleURL
+ isMarkdownTitleURL,
+ humanFileSize
}
diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js
index ff961433..f443c7ca 100644
--- a/browser/main/lib/dataApi/attachmentManagement.js
+++ b/browser/main/lib/dataApi/attachmentManagement.js
@@ -627,9 +627,9 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
/**
* @description Get all existing attachments related to a specific note
including their status (in use or not) and their path. Return null if there're no attachment related to note or specified parametters are invalid
+ * @param markdownContent markdownContent of the current note
* @param storageKey StorageKey of the current note
* @param noteKey NoteKey of the currentNote
- * @param linkText Text that was pasted
* @return {Promise>} Promise returning the
list of attachments with their properties */
function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) {
@@ -671,6 +671,29 @@ function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) {
}
}
+/**
+ * @description Remove all specified attachment paths
+ * @param attachments attachment paths
+ * @return {Promise} Promise after all attachments are removed */
+function removeAttachmentsByPaths (attachments) {
+ const promises = []
+ for (const attachment of attachments) {
+ const promise = new Promise((resolve, reject) => {
+ fs.unlink(attachment, (err) => {
+ if (err) {
+ console.error('Could not delete "%s"', attachment)
+ console.error(err)
+ reject(err)
+ return
+ }
+ resolve()
+ })
+ })
+ promises.push(promise)
+ }
+ return Promise.all(promises)
+}
+
/**
* Clones the attachments of a given note.
* Copies the attachments to their new destination and updates the content of the new note so that the attachment-links again point to the correct destination.
@@ -773,6 +796,7 @@ module.exports = {
getAbsolutePathsOfAttachmentsInContent,
importAttachments,
removeStorageAndNoteReferences,
+ removeAttachmentsByPaths,
deleteAttachmentFolder,
deleteAttachmentsNotPresentInNote,
getAttachmentsPathAndStatus,
diff --git a/browser/main/modals/PreferencesModal/StoragesTab.js b/browser/main/modals/PreferencesModal/StoragesTab.js
index f1b186eb..e84fa88c 100644
--- a/browser/main/modals/PreferencesModal/StoragesTab.js
+++ b/browser/main/modals/PreferencesModal/StoragesTab.js
@@ -6,6 +6,7 @@ import dataApi from 'browser/main/lib/dataApi'
import attachmentManagement from 'browser/main/lib/dataApi/attachmentManagement'
import StorageItem from './StorageItem'
import i18n from 'browser/lib/i18n'
+import { humanFileSize } from 'browser/lib/utils'
import fs from 'fs'
const electron = require('electron')
@@ -27,20 +28,6 @@ function browseFolder () {
})
}
-function humanFileSize (bytes) {
- const threshold = 1000
- if (Math.abs(bytes) < threshold) {
- return bytes + ' B'
- }
- var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
- var u = -1
- do {
- bytes /= threshold
- ++u
- } while (Math.abs(bytes) >= threshold && u < units.length - 1)
- return bytes.toFixed(1) + ' ' + units[u]
-}
-
class StoragesTab extends React.Component {
constructor (props) {
super(props)
@@ -94,25 +81,8 @@ class StoragesTab extends React.Component {
e.preventDefault()
}
- removeAllAttachments (attachments) {
- const promises = []
- for (const attachment of attachments) {
- for (const file of attachment) {
- const promise = new Promise((resolve, reject) => {
- fs.unlink(file, (err) => {
- if (err) {
- console.error('Could not delete "%s"', file)
- console.error(err)
- reject(err)
- return
- }
- resolve()
- })
- })
- promises.push(promise)
- }
- }
- Promise.all(promises)
+ handleRemoveUnusedAttachments (attachments) {
+ attachmentManagement.removeAttachmentsByPaths(attachments)
.then(() => this.loadAttachmentStorage())
.catch(console.error)
}
@@ -142,6 +112,9 @@ class StoragesTab extends React.Component {
}, 0)
const totalAttachmentsSize = totalUnusedAttachmentsSize + totalInuseAttachmentsSize
+ const unusedAttachmentPaths = unusedAttachments
+ .reduce((acc, curr) => acc.concat(curr.path), [])
+
if (!boundingBox) { return null }
const storageList = data.storageMap.map((storage) => {
return
Total attachments size: {humanFileSize(totalAttachmentsSize)} ({totalAttachments} items)
-