diff --git a/config.js b/config.js index 598dd4c..2a389d5 100644 --- a/config.js +++ b/config.js @@ -67,8 +67,10 @@ const defaultConfig = { forwardSeekTime: 30, enableUnsafeRoutes: false, offloadMedia: false, - offloadStreamPath: '/media/stream/', - offloadDownloadPath: '/media/download/' + offloadStreamPath: '/media/stream/', // /media/stream/RJ123456/subdirs/track.mp3 + offloadDownloadPath: '/media/download/', // /media/download/RJ123456/subdirs/track.mp3 + mediaStreamBaseUrl: '/api/media/stream/', // /api/media/stream/123456/2 This setting will be ignored if offloadMedia = true + mediaDownloadBaseUrl: '/api/media/download/', // /api/media/download/123456/2 This setting will be ignored if offloadMedia = true }; const initConfig = () => { diff --git a/filesystem/utils.js b/filesystem/utils.js index 09f7489..a96b898 100644 --- a/filesystem/utils.js +++ b/filesystem/utils.js @@ -54,7 +54,7 @@ const getTrackList = (id, dir) => recursiveReaddir(dir) * @param {Array} tracks * @param {String} workTitle */ -const toTree = (tracks, workTitle) => { +const toTree = (tracks, workTitle, workDir, rootFolder) => { const tree = []; // 插入文件夹 @@ -73,35 +73,63 @@ const toTree = (tracks, workTitle) => { fatherFolder = fatherFolder.find(item => item.type === 'folder' && item.title === folderName).children; }); }); - + // 插入文件 tracks.forEach(track => { let fatherFolder = tree; - const path = track.subtitle ? track.subtitle.split('\\') : []; - path.forEach(folderName => { + const paths = track.subtitle ? track.subtitle.split('\\') : []; + paths.forEach(folderName => { fatherFolder = fatherFolder.find(item => item.type === 'folder' && item.title === folderName).children; }); + // Path controlled by config.offloadMedia, config.offloadStreamPath and config.offloadDownloadPath + // By default: + // /media/stream/RJ123456/subdirs/track.mp3 + // /media/download/RJ123456/subdirs/track.mp3 + // + // If the folder is deeper: + // /media/stream/second/RJ123456/subdirs/track.mp3 + // /media/download/second/RJ123456/subdirs/track.mp3 + // console.log(rootFolder.name, workDir, track.subtitle, track.title) + let offloadStreamUrl = path.join(config.offloadStreamPath, rootFolder.name, workDir, track.subtitle || '', track.title); + let offloadDownloadUrl = path.join(config.offloadDownloadPath, rootFolder.name, workDir, track.subtitle || '', track.title); + if (process.platform === 'win32') { + offloadStreamUrl = offloadStreamUrl.replace(/\\/g, '/'); + offloadDownloadUrl = offloadDownloadUrl.replace(/\\/g, '/'); + } + + const textBaseUrl = '/api/media/stream/' + const textStreamBaseUrl = textBaseUrl + track.hash; // Handle charset detection internally with jschardet + const textDownloadBaseUrl = config.offloadMedia ? offloadDownloadUrl : config.mediaDownloadBaseUrl + track.hash; + const mediaStreamUrl = config.offloadMedia ? offloadStreamUrl : config.mediaStreamBaseUrl + track.hash; + const mediaDownloadUrl = config.offloadMedia ? offloadDownloadUrl : config.mediaDownloadBaseUrl + track.hash; + if (track.ext === '.txt' || track.ext === '.lrc' || track.ext === '.srt' || track.ext === '.ass') { fatherFolder.push({ type: 'text', hash: track.hash, title: track.title, - workTitle + workTitle, + mediaStreamUrl: textStreamBaseUrl, + mediaDownloadUrl: textDownloadBaseUrl }); } else if (track.ext === '.jpg' || track.ext === '.jpeg' || track.ext === '.png' || track.ext === '.webp' ) { fatherFolder.push({ type: 'image', hash: track.hash, title: track.title, - workTitle + workTitle, + mediaStreamUrl, + mediaDownloadUrl }); } else { fatherFolder.push({ type: 'file', hash: track.hash, title: track.title, - workTitle + workTitle, + mediaStreamUrl, + mediaDownloadUrl }); } }); diff --git a/routes/metadata.js b/routes/metadata.js index b51e4e4..2d10777 100644 --- a/routes/metadata.js +++ b/routes/metadata.js @@ -43,7 +43,7 @@ router.get('/tracks/:id', (req, res, next) => { const rootFolder = config.rootFolders.find(rootFolder => rootFolder.name === work.root_folder); if (rootFolder) { getTrackList(req.params.id, path.join(rootFolder.path, work.dir)) - .then(tracks => res.send(toTree(tracks, work.title))) + .then(tracks => res.send(toTree(tracks, work.title, work.dir, rootFolder))) .catch(() => res.status(500).send({error: '获取文件列表失败,请检查文件是否存在或重新扫描清理'})); } else { res.status(500).send({error: `找不到文件夹: "${work.root_folder}",请尝试重启服务器或重新扫描.`});