forked from mixmix/ferment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-audio-window.js
329 lines (295 loc) · 9.69 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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')
var processImage = require('./lib/process-image')
var util = require('util')
// TODO: rewrite all of this with less callback hell. Maybe pull streams?
module.exports = function (client, config, edit) {
var context = {
config,
api: require('./api')(client, config),
background: require('./models/background-remote')(config)
}
var announce = config.webtorrent.announceList
var mediaPath = config.mediaPath
var artworkUrl = 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()
title.select()
}, 50)
var audioInfo = Value()
var waitingToSave = Value(false)
var publishing = Value(false)
var processing = computed(audioInfo, (info) => info && info.processing)
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. Please check that the file is a supported format.',
detail: ffmpegError(err)
})
audioInfo.set({error: err})
console.log(err)
electron.remote.getGlobal('console').log(util.inspect(err))
}
audioInfo.set(info)
if (waitingToSave()) {
save()
}
})
}
}
artworkInput.onchange = function () {
var file = artworkInput.files[0]
if (file) {
processImage(file.path, {
width: 500, height: 500, type: 'jpeg'
}, (err, url) => {
if (err) throw err
artworkUrl.set(url)
})
}
}
var defaultImage = null
var editOverride = null
if (edit && edit.item) {
description.value = edit.item.description
title.value = edit.item.title
defaultImage = edit.item.artworkSrc && context.api.getBlobUrl(edit.item.artworkSrc)
editOverride = {
type: 'ferment/update',
update: edit.id
}
}
var overview = computed([audioInfo, edit], (info, edit) => {
if (info) {
return info.overview
} else if (edit && edit.item) {
return edit.item.overview
}
})
return h('Dialog', [
h('section AddAudioPost', [
h('div.artwork', {
style: {
'background-image': computed([artworkUrl, defaultImage], (url, defaultImage) => {
if (url || defaultImage) {
return `url('${url || defaultImage}')`
} else {
return ''
}
})
}
}, [
h('span', ['🖼 Choose Artwork...']), artworkInput
]),
h('div.main', [
h('div.info', [
title, description
]),
h('div.audio', {
classList: [
when(processing, '-processing')
]
}, [
AudioOverview(overview, 600, 100),
h('span', ['📂 Choose Audio File...']),
audioInput
])
])
]),
h('footer', [
h('div.info', [
h('strong', 'Tip: '),
`Wait for the 🍻 status `, h('em', `before`), ` closing LolaShare to `,
h('a', {
href: 'https://github.com/LolaShare/LolaShare/#publishing-audio'
}, `make sure other people get your file`), `.`
]),
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}, [editOverride ? '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 if (!editOverride) {
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)
var item = extend({
type: 'ferment/audio',
title: title.value,
description: description.value
}, editOverride)
if (audioInfo()) {
commitAndSeed(extend(audioInfo(), item), next)
} else {
next(null, item)
}
function next (err, item) {
if (err) throw err
if (artworkUrl()) {
context.api.addBlob(artworkUrl(), (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 baseName = sanitizeFileName(info.title.trim()) || 'audio'
var tempDir = info.tempDir
var tempFile = Path.join(tempDir, info.fileName)
var newFileName = `${baseName}.webm`
var renamed = Path.join(tempDir, newFileName)
var fallbackTempFile = Path.join(tempDir, info.fallbackFileName)
var newFallbackFileName = `${baseName}.mp3`
var fallbackRenamed = Path.join(tempDir, newFallbackFileName)
info = extend(info, {
fileName: newFileName,
fallbackFileName: newFallbackFileName
})
delete info.tempDir
console.log('renaming', tempFile, 'to', renamed)
fs.rename(tempFile, renamed, function (err) {
if (err) return cb(err)
fs.rename(fallbackTempFile, fallbackRenamed, function (err) {
if (err) return cb(err)
createTorrent([renamed, fallbackRenamed], { announce, name: baseName }, function (err, torrentFile) {
if (err) return cb(err)
var torrent = parseTorrentFile(torrentFile)
var torrentPath = Path.join(mediaPath, `${torrent.infoHash}.torrent`)
var containerPath = Path.join(mediaPath, `${torrent.infoHash}`)
var finalPath = Path.join(containerPath, baseName)
console.log('created torrent', torrentPath)
fs.mkdir(containerPath, (err) => {
if (err) return cb(err)
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)}.webm`
var toPath = Path.join(tempDir, fileName)
var fallbackFileName = `${Path.basename(path)}.mp3`
var toPathFallback = Path.join(tempDir, fallbackFileName)
convert(path, toPath, function (err) {
if (cancelled) return
if (err) return cb && cb(err)
console.log('converted to webm')
// create an mp3 fallback for mobile platforms and browsers that don't support the future
convert.mp3(path, toPathFallback, function (err) {
if (cancelled) return
if (err) return cb && cb(err)
console.log('created fallback mp3 version')
cb(null, extend(meta, {
fileName, fallbackFileName, tempDir
}))
})
})
})
})
return () => {
cancelled = true
}
}
}
function ffmpegError (err) {
if (err.message) {
var lines = err.message.trim().split('\n')
return lines.slice(-3).join('\n')
}
}