-
Notifications
You must be signed in to change notification settings - Fork 80
/
add-audio-window.js
249 lines (224 loc) · 7.04 KB
/
add-audio-window.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var h = require('./lib/h')
var electron = require('electron')
var Path = require('path')
var AudioOverview = require('./widgets/audio-overview')
var computed = require('@mmckegg/mutant/computed')
var when = require('@mmckegg/mutant/when')
var Value = require('@mmckegg/mutant/value')
var convert = require('./lib/convert')
var generateMeta = require('./lib/generate-meta')
var parseTorrentFile = require('parse-torrent-file')
var createTorrent = require('create-torrent')
var fs = require('fs')
var extend = require('xtend')
var sanitizeFileName = require('sanitize-filename')
module.exports = function (client, config) {
var context = {
config,
api: require('./api')(client, config),
background: require('./models/background-remote')(config)
}
var mediaPath = config.mediaPath
var artworkPath = Value()
var artworkInput = h('input', {type: 'file', accept: 'image/*'})
var audioInput = h('input', {type: 'file', accept: 'audio/*'})
var title = h('input -title', { placeholder: 'Choose a title' })
var description = h('textarea -description', {
rows: 5,
placeholder: 'Describe your audio'
})
setTimeout(() => title.focus(), 50)
var audioInfo = Value()
var waitingToSave = Value(false)
var publishing = Value(false)
var processing = computed(audioInfo, (info) => info && info.processing)
var overview = computed(audioInfo, (info) => info && info.overview)
var audioSvg = AudioOverview(overview, 600, 100)
var lastAutoTitle = title.value
var cancelLastImport = null
audioInput.onchange = function () {
var file = audioInput.files[0]
if (file) {
if (!title.value || title.value === lastAutoTitle) {
var fileName = file.name
var ext = Path.extname(fileName)
var base = Path.basename(fileName, ext)
title.value = base
lastAutoTitle = title.value
}
audioInfo.set({ processing: true })
cancelLastImport && cancelLastImport()
cancelLastImport = prepareAudio(file.path, function (err, info) {
if (err) {
electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), {
type: 'error',
title: 'Error',
buttons: ['OK'],
message: 'An error occured while processing audio file.'
})
audioInfo.set({error: err})
throw err
}
audioInfo.set(info)
if (waitingToSave()) {
save()
}
})
}
}
artworkInput.onchange = function () {
var file = artworkInput.files[0]
if (file) {
artworkPath.set(file.path)
}
}
return h('Dialog', [
h('section AddAudioPost', [
h('div.artwork', {
style: {
'background-image': computed(artworkPath, p => p ? `url('file://${p}')` : '')
}
}, [
h('span', ['🖼 Choose Artwork...']), artworkInput
]),
h('div.main', [
h('div.info', [
title, description
]),
h('div.audio', {
classList: [
when(processing, '-processing')
]
}, [
audioSvg,
h('span', ['📂 Choose Audio File...']),
audioInput
])
])
]),
h('footer', [
when(publishing,
h('button', {'disabled': true}, ['Publishing...']),
when(waitingToSave, [
h('button', {'disabled': true}, ['Processing, please wait...']),
h('button -stop', {'ev-click': cancelPost}, ['Cancel Post'])
], [
h('button -save', {'ev-click': save}, ['Publish Audio']),
h('button -cancel', {'ev-click': cancel}, ['Cancel'])
])
)
])
])
// scoped
function cancelPost () {
waitingToSave.set(false)
}
function save () {
if (audioInfo() && !audioInfo().error) {
if (audioInfo().processing) {
waitingToSave.set(true)
return
}
} else {
electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), {
type: 'info',
title: 'Cannot Publish',
buttons: ['OK'],
message: 'You need to choose an audio file before publishing.'
})
return
}
publishing.set(true)
commitAndSeed(extend(audioInfo(), {
type: 'ferment/audio',
title: title.value,
description: description.value
}), function (err, item) {
if (err) throw err
if (artworkPath()) {
context.api.addBlob(artworkPath(), (err, hash) => {
if (err) throw err
console.log('added blob', hash)
item.artworkSrc = `blobstore:${hash}`
publish(item)
})
} else {
publish(item)
}
})
}
function commitAndSeed (info, cb) {
var tempDir = info.tempDir
var tempFile = Path.join(tempDir, info.fileName)
var newFileName = getFileName(info)
var renamed = Path.join(tempDir, newFileName)
info = extend(info, {
fileName: newFileName
})
delete info.tempDir
console.log('renaming', tempFile, 'to', renamed)
fs.rename(tempFile, renamed, function (err) {
if (err) throw err
createTorrent(renamed, function (err, torrentFile) {
if (err) return cb(err)
var torrent = parseTorrentFile(torrentFile)
var torrentPath = Path.join(mediaPath, `${torrent.infoHash}.torrent`)
var finalPath = Path.join(mediaPath, `${torrent.infoHash}`)
console.log('created torrent', torrentPath)
fs.rename(audioInfo().tempDir, finalPath, (err) => {
if (err) return cb(err)
fs.writeFile(torrentPath, torrentFile, (err) => {
if (err) return cb(err)
context.background.seedTorrent(torrent.infoHash, function (err, magnetURI) {
if (err) throw err
console.log('seeding torrent', magnetURI)
info.audioSrc = magnetURI
cb(null, info)
})
})
})
})
})
}
function publish (item) {
console.log('publishing', item)
context.api.publish(item, function (err) {
if (err) throw err
var window = electron.remote.getCurrentWindow()
window.close()
})
}
function cancel () {
var window = electron.remote.getCurrentWindow()
window.close()
}
function prepareAudio (path, cb) {
var cancelled = false
generateMeta(path, function (err, meta) {
if (cancelled) return
if (err) return cb && cb(err)
console.log('generated meta', meta)
var tempDir = Path.join(mediaPath, `importing-${Date.now()}`)
fs.mkdir(tempDir, function (err) {
if (err) return cb && cb(err)
var fileName = `${Path.basename(path)}.ogg`
var toPath = Path.join(tempDir, fileName)
convert(path, toPath, function (err) {
if (cancelled) return
if (err) return cb && cb(err)
console.log('converted to ogg')
cb(null, extend(meta, {
fileName, tempDir
}))
})
})
})
return () => {
cancelled = true
}
}
}
function getFileName (audioInfo) {
var ext = Path.extname(audioInfo.fileName)
return `${sanitizeFileName(audioInfo.title.trim()) || 'audio'}${ext}`
}