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

re-organize attachment functions and updated comments doc

This commit is contained in:
Nguyễn Việt Hưng
2019-08-28 21:23:52 +12:00
committed by Junyoung Choi
parent 8355e1e006
commit 0b84a372f6
3 changed files with 49 additions and 36 deletions

View File

@@ -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 <StorageItem
@@ -174,7 +147,8 @@ class StoragesTab extends React.Component {
<p styleName='list-attachment-label'>
Total attachments size: {humanFileSize(totalAttachmentsSize)} ({totalAttachments} items)
</p>
<button styleName='list-attachement-clear-button' onClick={() => this.removeAllAttachments(unusedAttachments)}>
<button styleName='list-attachement-clear-button'
onClick={() => this.handleRemoveUnusedAttachments(unusedAttachmentPaths)}>
{i18n.__('Clear unused attachments')}
</button>
</div>