use custom fn to detect same filename in debrids

This commit is contained in:
TheBeastLT
2023-12-16 19:04:53 +02:00
parent 2c4da0679f
commit afa1458144
5 changed files with 13 additions and 11 deletions

View File

@@ -3,7 +3,7 @@ import { Type } from '../lib/types.js';
import { isVideo, isArchive } from '../lib/extension.js';
import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js';
import { BadTokenError, AccessDeniedError } from './mochHelper.js';
import { BadTokenError, AccessDeniedError, sameFilename } from './mochHelper.js';
const KEY = 'alldebrid';
const AGENT = 'torrentio';
@@ -144,7 +144,7 @@ async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) {
const targetFileName = decodeURIComponent(encodedFileName);
const videos = torrent.links.filter(link => isVideo(link.filename));
const targetVideo = Number.isInteger(fileIndex)
? videos.find(video => targetFileName.includes(video.filename))
? videos.find(video => sameFilename(targetFileName, video.filename))
: videos.sort((a, b) => b.size - a.size)[0];
if (!targetVideo && torrent.links.every(link => isArchive(link.filename))) {

View File

@@ -48,7 +48,7 @@ export async function enrichMeta(itemMeta) {
return itemMeta
}
function sameFilename(filename, expectedFilename) {
export function sameFilename(filename, expectedFilename) {
const offset = filename.length - expectedFilename.length;
for (let i = 0; i < expectedFilename.length; i++) {
if (filename[offset + i] !== expectedFilename[i] && expectedFilename[i] !== '<27>') {

View File

@@ -4,7 +4,7 @@ import { Type } from '../lib/types.js';
import { isVideo } from '../lib/extension.js';
import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js';
import { chunkArray, BadTokenError } from './mochHelper.js';
import { chunkArray, BadTokenError, sameFilename } from './mochHelper.js';
const KEY = 'offcloud';
@@ -125,10 +125,11 @@ async function _createTorrent(OC, infoHash) {
}
async function _unrestrictLink(OC, infoHash, torrent, cachedEntryInfo, fileIndex) {
const targetFileName = decodeURIComponent(cachedEntryInfo);
const files = await _getFileUrls(OC, torrent)
const targetFile = Number.isInteger(fileIndex)
? files.find(file => file.includes(`/${torrent.requestId}/${fileIndex}/`))
: files.find(file => isVideo(file));
? files.find(file => sameFilename(targetFileName, file.split('/').pop()))
: files.find(file => isVideo(file)) || files.pop();
if (!targetFile) {
return Promise.reject(`No Offcloud links found for index ${fileIndex} in: ${JSON.stringify(torrent)}`);

View File

@@ -4,7 +4,7 @@ import { Type } from '../lib/types.js';
import { isVideo, isArchive } from '../lib/extension.js';
import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js';
import { BadTokenError, chunkArray } from './mochHelper.js';
import { BadTokenError, chunkArray, sameFilename } from './mochHelper.js';
const KEY = 'premiumize';
@@ -126,7 +126,7 @@ async function _getCachedLink(PM, infoHash, encodedFileName, fileIndex, ip, isBr
const targetFileName = decodeURIComponent(encodedFileName);
const videos = cachedTorrent.content.filter(file => isVideo(file.path));
const targetVideo = Number.isInteger(fileIndex)
? videos.find(video => video.path.includes(targetFileName))
? videos.find(video => sameFilename(video.path, targetFileName))
: videos.sort((a, b) => b.size - a.size)[0];
if (!targetVideo && videos.every(video => isArchive(video.path))) {
console.log(`Only Premiumize archive is available for [${infoHash}] ${fileIndex}`)

View File

@@ -5,6 +5,7 @@ import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js';
import { Type } from "../lib/types.js";
import { decode } from "magnet-uri";
import { sameFilename } from "./mochHelper.js";
const PutioAPI = PutioClient.default;
const KEY = 'putio';
@@ -172,14 +173,14 @@ async function _getTargetFile(Putio, torrent, encodedFileName, fileIndex) {
// when specific file index is defined search by filename
// when it's not defined find all videos and take the largest one
targetFile = Number.isInteger(fileIndex)
? videos.find(video => targetFileName.includes(video.name))
: !folders.length && videos.sort((a, b) => b.size - a.size)[0];
? videos.find(video => sameFilename(targetFileName, video.name))
: !folders.length && videos.toSorted((a, b) => b.size - a.size)[0];
files = !targetFile
? await Promise.all(folders.map(folder => _getFiles(Putio, folder.id)))
.then(results => results.reduce((a, b) => a.concat(b), []))
: [];
}
return targetFile ? targetFile : Promise.reject(`No target file found for Putio [${torrent.hash}] ${targetFileName}`);
return targetFile || Promise.reject(`No target file found for Putio [${torrent.hash}] ${targetFileName}`);
}
async function _getFiles(Putio, fileId) {