diff --git a/addon/moch/alldebrid.js b/addon/moch/alldebrid.js index 2999ca7..9a6f50a 100644 --- a/addon/moch/alldebrid.js +++ b/addon/moch/alldebrid.js @@ -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))) { diff --git a/addon/moch/mochHelper.js b/addon/moch/mochHelper.js index b3e16b0..410dba9 100644 --- a/addon/moch/mochHelper.js +++ b/addon/moch/mochHelper.js @@ -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] !== '�') { diff --git a/addon/moch/offcloud.js b/addon/moch/offcloud.js index 2036e68..cf9beae 100644 --- a/addon/moch/offcloud.js +++ b/addon/moch/offcloud.js @@ -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)}`); diff --git a/addon/moch/premiumize.js b/addon/moch/premiumize.js index f2f2611..78c7385 100644 --- a/addon/moch/premiumize.js +++ b/addon/moch/premiumize.js @@ -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}`) diff --git a/addon/moch/putio.js b/addon/moch/putio.js index 62e0f25..7591eba 100644 --- a/addon/moch/putio.js +++ b/addon/moch/putio.js @@ -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) {