api: torrents: simplify contents API by returning an Array of TorrentContent

This commit is contained in:
Jesse Chan
2020-10-15 13:50:26 +08:00
parent 1f76f320c5
commit dcfcce3456
17 changed files with 321 additions and 331 deletions
-20
View File
@@ -2,8 +2,6 @@ import fs from 'fs';
import {homedir} from 'os';
import path from 'path';
import {TorrentContent, TorrentContentTree} from '@shared/types/TorrentContent';
import config from '../../config';
export const accessDeniedError = () => {
@@ -50,24 +48,6 @@ export const createDirectory = (directoryPath: string) => {
}
};
export const findFilesByIndices = (indices: Array<number>, fileTree: TorrentContentTree): TorrentContent[] => {
const {directories, files = []} = fileTree;
let selectedFiles = files.filter((file) => indices.includes(file.index));
if (directories != null) {
selectedFiles = selectedFiles.concat(
Object.keys(directories).reduce(
(accumulator: TorrentContent[], directory) =>
accumulator.concat(findFilesByIndices(indices, directories[directory])),
[],
),
);
}
return selectedFiles;
};
export const getDirectoryList = async (inputPath: string) => {
if (typeof inputPath !== 'string') {
throw fileNotFoundError();