save files to specific dirs based on filename prefixes

This commit is contained in:
teddit 2020-12-16 18:52:20 +01:00
parent d32a263e49
commit a34425a944
1 changed files with 15 additions and 9 deletions

View File

@ -27,6 +27,7 @@ module.exports = function(tools) {
let pathname = temp_url.pathname let pathname = temp_url.pathname
let file_ext let file_ext
let has_extension = true let has_extension = true
let dir = ''
if(gifmp4) { if(gifmp4) {
file_ext = 'mp4' file_ext = 'mp4'
@ -43,14 +44,19 @@ module.exports = function(tools) {
} }
} }
if(file_prefix === 'thumb_')
dir = 'thumbs/'
if(file_prefix === 'flair')
dir = 'flairs/'
if(valid_video_extensions.includes(file_ext) || gifmp4) { if(valid_video_extensions.includes(file_ext) || gifmp4) {
/* Is video. */ /* Is video. */
if(!config.video_enabled) { if(!config.video_enabled) {
resolve('') resolve('')
} else { } else {
let filename = `${temp_url.pathname.substr(1).split('/')[0]}.${file_ext}` let filename = `${temp_url.pathname.substr(1).split('/')[0]}.${file_ext}`
let path = `./dist/vids/${filename}` let path = `./dist/vids/${dir}${filename}`
let temp_path = `./dist/vids/temp_${filename}` let temp_path = `./dist/vids/${dir}temp_${filename}`
if(!fs.existsSync(path)) { if(!fs.existsSync(path)) {
const download = await downloadFile(cleanUrl(url)) const download = await downloadFile(cleanUrl(url))
if(download.success === true) { if(download.success === true) {
@ -65,13 +71,13 @@ module.exports = function(tools) {
} }
const download_audio = await downloadFile(cleanUrl(audio_url)) const download_audio = await downloadFile(cleanUrl(audio_url))
if(download_audio.success === true) { if(download_audio.success === true) {
let audio_path = `./dist/vids/temp_audio_${filename}` let audio_path = `./dist/vids/${dir}temp_audio_${filename}`
const write_audio = await writeToDisk(download_audio.data, audio_path) const write_audio = await writeToDisk(download_audio.data, audio_path)
if(write_audio.success === true) { if(write_audio.success === true) {
let processVideo = spawn('ffmpeg', ['-y', '-i', temp_path, '-i', audio_path, '-c', 'copy', path]) let processVideo = spawn('ffmpeg', ['-y', '-i', temp_path, '-i', audio_path, '-c', 'copy', path])
processVideo.on('exit', (code) => { processVideo.on('exit', (code) => {
if(code === 0) { if(code === 0) {
let final_url = `/vids/${filename}` let final_url = `/vids/${dir}${filename}`
let temp_files = [temp_path, audio_path] let temp_files = [temp_path, audio_path]
deleteFiles(temp_files, (error) => { deleteFiles(temp_files, (error) => {
if(error) { if(error) {
@ -97,7 +103,7 @@ module.exports = function(tools) {
if(error) { if(error) {
console.log(`Error while renaming the temp video file: ${temp_path} => ${path}.`, error) console.log(`Error while renaming the temp video file: ${temp_path} => ${path}.`, error)
} else { } else {
let final_url = `/vids/${filename}` let final_url = `/vids/${dir}${filename}`
resolve(final_url) resolve(final_url)
} }
}) })
@ -111,7 +117,7 @@ module.exports = function(tools) {
resolve('') resolve('')
} }
} else { } else {
resolve(`/vids/${filename}`) resolve(`/vids/${dir}${filename}`)
} }
} }
} else { } else {
@ -126,13 +132,13 @@ module.exports = function(tools) {
} }
filename = `${file_prefix}w:${temp_url.searchParams.get('width')}_${temp_url.pathname.split('/').slice(-1)}` filename = `${file_prefix}w:${temp_url.searchParams.get('width')}_${temp_url.pathname.split('/').slice(-1)}`
} }
path = `./dist/pics/${filename}` path = `./dist/pics/${dir}${filename}`
if(!fs.existsSync(path)) { if(!fs.existsSync(path)) {
const download = await downloadFile(cleanUrl(url)) const download = await downloadFile(cleanUrl(url))
if(download.success === true) { if(download.success === true) {
const write = await writeToDisk(download.data, path) const write = await writeToDisk(download.data, path)
if(write.success === true) { if(write.success === true) {
let final_url = `/pics/${filename}` let final_url = `/pics/${dir}${filename}`
resolve(final_url) resolve(final_url)
} else { } else {
console.log(`Error while writing image file.`, write) console.log(`Error while writing image file.`, write)
@ -143,7 +149,7 @@ module.exports = function(tools) {
resolve('') resolve('')
} }
} else { } else {
resolve(`/pics/${filename}`) resolve(`/pics/${dir}${filename}`)
} }
} }
} else { } else {