Skip to content

Commit

Permalink
Send media urls to front end
Browse files Browse the repository at this point in the history
  • Loading branch information
mason1900 committed Mar 6, 2021
1 parent e1c453c commit 159af84
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
6 changes: 4 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
42 changes: 35 additions & 7 deletions filesystem/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

// 插入文件夹
Expand All @@ -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
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion routes/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}",请尝试重启服务器或重新扫描.`});
Expand Down

0 comments on commit 159af84

Please sign in to comment.