Skip to content

Commit 0b84a37

Browse files
ZeroX-DGRokt33r
authored andcommitted
re-organize attachment functions and updated comments doc
1 parent 8355e1e commit 0b84a37

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

browser/lib/utils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,24 @@ export function isMarkdownTitleURL (str) {
136136
return /(^#{1,6}\s)(?:\w+:|^)\/\/(?:[^\s\.]+\.\S{2}|localhost[\:?\d]*)/.test(str)
137137
}
138138

139+
export function humanFileSize (bytes) {
140+
const threshold = 1000
141+
if (Math.abs(bytes) < threshold) {
142+
return bytes + ' B'
143+
}
144+
var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
145+
var u = -1
146+
do {
147+
bytes /= threshold
148+
++u
149+
} while (Math.abs(bytes) >= threshold && u < units.length - 1)
150+
return bytes.toFixed(1) + ' ' + units[u]
151+
}
152+
139153
export default {
140154
lastFindInArray,
141155
escapeHtmlCharacters,
142156
isObjectEqual,
143-
isMarkdownTitleURL
157+
isMarkdownTitleURL,
158+
humanFileSize
144159
}

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
627627
/**
628628
* @description Get all existing attachments related to a specific note
629629
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
630+
* @param markdownContent markdownContent of the current note
630631
* @param storageKey StorageKey of the current note
631632
* @param noteKey NoteKey of the currentNote
632-
* @param linkText Text that was pasted
633633
* @return {Promise<Array<{path: String, isInUse: bool}>>} Promise returning the
634634
list of attachments with their properties */
635635
function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) {
@@ -671,6 +671,29 @@ function getAttachmentsPathAndStatus (markdownContent, storageKey, noteKey) {
671671
}
672672
}
673673

674+
/**
675+
* @description Remove all specified attachment paths
676+
* @param attachments attachment paths
677+
* @return {Promise} Promise after all attachments are removed */
678+
function removeAttachmentsByPaths (attachments) {
679+
const promises = []
680+
for (const attachment of attachments) {
681+
const promise = new Promise((resolve, reject) => {
682+
fs.unlink(attachment, (err) => {
683+
if (err) {
684+
console.error('Could not delete "%s"', attachment)
685+
console.error(err)
686+
reject(err)
687+
return
688+
}
689+
resolve()
690+
})
691+
})
692+
promises.push(promise)
693+
}
694+
return Promise.all(promises)
695+
}
696+
674697
/**
675698
* Clones the attachments of a given note.
676699
* 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 = {
773796
getAbsolutePathsOfAttachmentsInContent,
774797
importAttachments,
775798
removeStorageAndNoteReferences,
799+
removeAttachmentsByPaths,
776800
deleteAttachmentFolder,
777801
deleteAttachmentsNotPresentInNote,
778802
getAttachmentsPathAndStatus,

browser/main/modals/PreferencesModal/StoragesTab.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dataApi from 'browser/main/lib/dataApi'
66
import attachmentManagement from 'browser/main/lib/dataApi/attachmentManagement'
77
import StorageItem from './StorageItem'
88
import i18n from 'browser/lib/i18n'
9+
import { humanFileSize } from 'browser/lib/utils'
910
import fs from 'fs'
1011

1112
const electron = require('electron')
@@ -27,20 +28,6 @@ function browseFolder () {
2728
})
2829
}
2930

30-
function humanFileSize (bytes) {
31-
const threshold = 1000
32-
if (Math.abs(bytes) < threshold) {
33-
return bytes + ' B'
34-
}
35-
var units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
36-
var u = -1
37-
do {
38-
bytes /= threshold
39-
++u
40-
} while (Math.abs(bytes) >= threshold && u < units.length - 1)
41-
return bytes.toFixed(1) + ' ' + units[u]
42-
}
43-
4431
class StoragesTab extends React.Component {
4532
constructor (props) {
4633
super(props)
@@ -94,25 +81,8 @@ class StoragesTab extends React.Component {
9481
e.preventDefault()
9582
}
9683

97-
removeAllAttachments (attachments) {
98-
const promises = []
99-
for (const attachment of attachments) {
100-
for (const file of attachment) {
101-
const promise = new Promise((resolve, reject) => {
102-
fs.unlink(file, (err) => {
103-
if (err) {
104-
console.error('Could not delete "%s"', file)
105-
console.error(err)
106-
reject(err)
107-
return
108-
}
109-
resolve()
110-
})
111-
})
112-
promises.push(promise)
113-
}
114-
}
115-
Promise.all(promises)
84+
handleRemoveUnusedAttachments (attachments) {
85+
attachmentManagement.removeAttachmentsByPaths(attachments)
11686
.then(() => this.loadAttachmentStorage())
11787
.catch(console.error)
11888
}
@@ -142,6 +112,9 @@ class StoragesTab extends React.Component {
142112
}, 0)
143113
const totalAttachmentsSize = totalUnusedAttachmentsSize + totalInuseAttachmentsSize
144114

115+
const unusedAttachmentPaths = unusedAttachments
116+
.reduce((acc, curr) => acc.concat(curr.path), [])
117+
145118
if (!boundingBox) { return null }
146119
const storageList = data.storageMap.map((storage) => {
147120
return <StorageItem
@@ -174,7 +147,8 @@ class StoragesTab extends React.Component {
174147
<p styleName='list-attachment-label'>
175148
Total attachments size: {humanFileSize(totalAttachmentsSize)} ({totalAttachments} items)
176149
</p>
177-
<button styleName='list-attachement-clear-button' onClick={() => this.removeAllAttachments(unusedAttachments)}>
150+
<button styleName='list-attachement-clear-button'
151+
onClick={() => this.handleRemoveUnusedAttachments(unusedAttachmentPaths)}>
178152
{i18n.__('Clear unused attachments')}
179153
</button>
180154
</div>

0 commit comments

Comments
 (0)