From dbf08e127658b58e2003e19f631892462535d5d4 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Sat, 26 Dec 2020 15:51:13 -0500 Subject: [PATCH] Fixed bug where audio files that had a stale webm extension in the metadata file path would fail to register --- backend/app.js | 20 +++++++------------- backend/db.js | 2 +- backend/utils.js | 7 +++++++ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/backend/app.js b/backend/app.js index 8730f36..189492d 100644 --- a/backend/app.js +++ b/backend/app.js @@ -1188,7 +1188,7 @@ async function downloadFileByURL_exec(url, type, options, sessionID = null) { } // get filepath with no extension - const filepath_no_extension = removeFileExtension(output_json['_filename']); + const filepath_no_extension = utils.removeFileExtension(output_json['_filename']); var full_file_path = filepath_no_extension + ext; var file_name = filepath_no_extension.substring(fileFolderPath.length, filepath_no_extension.length); @@ -1310,7 +1310,7 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) { video.on('info', function(info) { video_info = info; file_size = video_info.size; - const json_path = removeFileExtension(video_info._filename) + '.info.json'; + const json_path = utils.removeFileExtension(video_info._filename) + '.info.json'; fs.ensureFileSync(json_path); fs.writeJSONSync(json_path, video_info); video.pipe(fs.createWriteStream(video_info._filename, { flags: 'w' })) @@ -1336,14 +1336,14 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) { let difference = (new_date - date)/1000; logger.debug(`Video download delay: ${difference} seconds.`); download['timestamp_end'] = Date.now(); - download['fileNames'] = [removeFileExtension(video_info._filename) + ext]; + download['fileNames'] = [utils.removeFileExtension(video_info._filename) + ext]; download['complete'] = true; updateDownloads(); // audio-only cleanup if (is_audio) { // filename fix - video_info['_filename'] = removeFileExtension(video_info['_filename']) + '.mp3'; + video_info['_filename'] = utils.removeFileExtension(video_info['_filename']) + '.mp3'; // ID3 tagging let tags = { @@ -1353,8 +1353,8 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) { let success = NodeID3.write(tags, video_info._filename); if (!success) logger.error('Failed to apply ID3 tag to audio file ' + video_info._filename); - const possible_webm_path = removeFileExtension(video_info['_filename']) + '.webm'; - const possible_mp4_path = removeFileExtension(video_info['_filename']) + '.mp4'; + const possible_webm_path = utils.removeFileExtension(video_info['_filename']) + '.webm'; + const possible_mp4_path = utils.removeFileExtension(video_info['_filename']) + '.mp4'; // check if audio file is webm if (fs.existsSync(possible_webm_path)) await convertFileToMp3(possible_webm_path, video_info['_filename']); else if (fs.existsSync(possible_mp4_path)) await convertFileToMp3(possible_mp4_path, video_info['_filename']); @@ -1371,7 +1371,7 @@ async function downloadFileByURL_normal(url, type, options, sessionID = null) { fs.appendFileSync(archive_path, diff); } - videopathEncoded = encodeURIComponent(removeFileExtension(base_file_name)); + videopathEncoded = encodeURIComponent(utils.removeFileExtension(base_file_name)); resolve({ [is_audio ? 'audiopathEncoded' : 'videopathEncoded']: videopathEncoded, @@ -1778,12 +1778,6 @@ async function checkExistsWithTimeout(filePath, timeout) { }); } -function removeFileExtension(filename) { - const filename_parts = filename.split('.'); - filename_parts.splice(filename_parts.length - 1) - return filename_parts.join('.'); -} - app.use(function(req, res, next) { res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"); res.header("Access-Control-Allow-Origin", getOrigin()); diff --git a/backend/db.js b/backend/db.js index 38dfdcb..6d5cdb6 100644 --- a/backend/db.js +++ b/backend/db.js @@ -17,7 +17,7 @@ function initialize(input_db, input_users_db, input_logger) { function registerFileDB(file_path, type, multiUserMode = null, sub = null, customPath = null, category = null) { let db_path = null; - const file_id = file_path.substring(0, file_path.length-4); + const file_id = utils.removeFileExtension(file_path); const file_object = generateFileObject(file_id, type, customPath || multiUserMode && multiUserMode.file_path, sub); if (!file_object) { logger.error(`Could not find associated JSON file for ${type} file ${file_id}`); diff --git a/backend/utils.js b/backend/utils.js index b18ed6a..3524041 100644 --- a/backend/utils.js +++ b/backend/utils.js @@ -181,6 +181,12 @@ async function recFindByExt(base,ext,files,result) return result } +function removeFileExtension(filename) { + const filename_parts = filename.split('.'); + filename_parts.splice(filename_parts.length - 1); + return filename_parts.join('.'); +} + // objects function File(id, title, thumbnailURL, isAudio, duration, url, uploader, size, path, upload_date, description, view_count, height, abr) { @@ -210,5 +216,6 @@ module.exports = { deleteJSONFile: deleteJSONFile, getDownloadedFilesByType: getDownloadedFilesByType, recFindByExt: recFindByExt, + removeFileExtension: removeFileExtension, File: File }