forked from ipfs/ipfs-desktop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdownload-cid.js
158 lines (133 loc) · 3.79 KB
/
download-cid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const { join } = require('path')
const fs = require('fs-extra')
const i18n = require('i18next')
const isIPFS = require('is-ipfs')
const all = require('it-all')
const concat = require('it-concat')
const { clipboard, app, shell } = require('electron')
const logger = require('./common/logger')
const { IS_MAC } = require('./common/consts')
const setupGlobalShortcut = require('./utils/setup-global-shortcut')
const dock = require('./utils/dock')
const { showDialog, showPrompt, selectDirectory } = require('./dialogs')
const CONFIG_KEY = 'downloadHashShortcut'
const SHORTCUT = IS_MAC
? 'Command+Control+H'
: 'CommandOrControl+Alt+D'
async function saveFile (dir, file) {
const location = join(dir, file.path)
await fs.outputFile(location, file.content)
}
async function get (ipfs, cid) {
return all((async function * () {
for await (let { path, content } of ipfs.get(cid)) {
content = content ? (await concat(content)).toString() : null
yield { path, content }
}
})())
}
async function getCID () {
const text = clipboard.readText().trim()
const { button, input } = await showPrompt({
title: i18n.t('downloadCidContentDialog.title'),
message: i18n.t('downloadCidContentDialog.message'),
defaultValue: (isIPFS.cid(text) || isIPFS.path(text)) ? text : '',
buttons: [
i18n.t('downloadCidContentDialog.action'),
i18n.t('cancel')
],
window: {
width: 500,
height: 120
}
})
if (button !== 0) {
return
}
return input
}
async function downloadCid (ctx) {
const cid = await getCID()
if (!cid) {
logger.info('[cid download] user canceled')
return
}
const { getIpfsd } = ctx
const ipfsd = await getIpfsd()
if (!ipfsd) {
return
}
let path
try {
path = await ipfsd.api.resolve(cid)
} catch (_) {
showDialog({
title: i18n.t('cantResolveCidDialog.title'),
message: i18n.t('cantResolveCidDialog.message', { path: cid }),
buttons: [i18n.t('close')]
})
return
}
const dir = await dock.run(() => selectDirectory({
defaultPath: app.getPath('downloads')
}))
if (!dir) {
logger.info(`[cid download] dropping ${path}: user didn't choose a path.`)
return
}
let files
try {
logger.info(`[cid download] downloading ${path}: started`, { withAnalytics: 'DOWNLOAD_HASH' })
files = await get(ipfsd.api, path)
logger.info(`[cid download] downloading ${path}: completed`)
} catch (err) {
logger.error(`[cid download] ${err.stack}`)
showDialog({
title: i18n.t('couldNotGetCidDialog.title'),
message: i18n.t('couldNotGetCidDialog.message', { path }),
buttons: [i18n.t('close')]
})
return
}
try {
await Promise.all(
files
.filter(file => !!file.content)
.map(file => saveFile(dir, file))
)
const opt = showDialog({
title: i18n.t('contentsSavedDialog.title'),
message: i18n.t('contentsSavedDialog.message', { path }),
buttons: [
i18n.t('contentsSavedDialog.action'),
i18n.t('close')
]
})
if (opt === 0) {
shell.showItemInFolder(join(dir, files[0].path))
}
} catch (err) {
logger.error(`[cid download] ${err.toString()}`)
showDialog({
title: i18n.t('couldNotSaveDialog.title'),
message: i18n.t('couldNotSaveDialog.message'),
buttons: [i18n.t('close')]
})
}
}
module.exports = function (ctx) {
setupGlobalShortcut({
confirmationDialog: {
title: i18n.t('enableGlobalDownloadShortcut.title'),
message: i18n.t('enableGlobalDownloadShortcut.message', { accelerator: SHORTCUT })
},
settingsOption: CONFIG_KEY,
accelerator: SHORTCUT,
action: () => {
downloadCid(ctx)
}
})
}
module.exports.downloadCid = downloadCid
module.exports.SHORTCUT = SHORTCUT
module.exports.CONFIG_KEY = CONFIG_KEY