Skip to content

Commit 6a8f3a9

Browse files
committed
fix: queue url arguments, check for existing db entry, pre meta check update result to dbFile
1 parent cc09f4a commit 6a8f3a9

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/main/stores/queue-database.helpers.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ function findDownloadByExactUrl(url: string) {
3939
.get()
4040
}
4141
function updateDownload(id: SelectDownload['id'], item: SelectDownload) {
42-
return db.update(downloads).set(omit(item, 'id')).where(eq(downloads.id, id)).returning()
42+
return db
43+
.update(downloads)
44+
.set(omit(item, 'id'))
45+
.where(eq(downloads.id, id))
46+
.returning()
47+
.then(([s]) => s)
4348
}
4449
function deleteDownload(id: SelectDownload['id']) {
4550
return db.delete(downloads).where(eq(downloads.id, id))

src/main/trpc/ytdlp.api.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { db } from '@main/stores/queue-database'
33
import { queries, SelectDownload } from '@main/stores/queue-database.helpers'
44
import { downloads } from '@main/stores/queue-database.schema'
55
import { logger } from '@shared/logger'
6+
import { queuePromiseStack } from '@shared/promises/helper'
67
import { TRPCError } from '@trpc/server'
78
import { observable } from '@trpc/server/observable'
89
import { desc } from 'drizzle-orm'
@@ -13,7 +14,12 @@ import { VideoInfo } from 'yt-dlp-wrap/types'
1314
import { YTDLDownloadStatus, YTDLItem, YTDLStatus } from 'ytdlp-desktop/types'
1415
import { z } from 'zod'
1516
import { publicProcedure, router } from './trpc'
16-
import { MAX_STREAM_CONCURRENT_FRAGMENTS, ytdl, YTDLP_CACHE_PATH } from './ytdlp.core'
17+
import {
18+
MAX_PARALLEL_DOWNLOADS,
19+
MAX_STREAM_CONCURRENT_FRAGMENTS,
20+
ytdl,
21+
YTDLP_CACHE_PATH
22+
} from './ytdlp.core'
1723
import { ytdlpDownloadQueue, ytdlpEvents } from './ytdlp.ee'
1824
const log = logger.child('ytdlp.api')
1925
export const ytdlpRouter = router({
@@ -26,9 +32,10 @@ export const ytdlpRouter = router({
2632
})
2733
)
2834
.mutation(async ({ input: { url }, ctx }) => {
29-
const files = await Promise.all(url.map((u) => queueYtdlMetaCheck(u).catch(() => null))).then(
30-
(files) => files.filter((s) => !!s)
31-
)
35+
const files = await queuePromiseStack(
36+
url.map((u) => () => queueYtdlMetaCheck(u).catch(() => null)),
37+
MAX_PARALLEL_DOWNLOADS
38+
).then((files) => files.filter((s) => !!s))
3239
const asyncResult = ytdlpDownloadQueue.addAll(
3340
files.map(
3441
(f) => () =>
@@ -228,7 +235,7 @@ const queueYtdlMetaCheck = async (
228235
): Promise<{ dbFile: SelectDownload; videoInfo: VideoInfo }> => {
229236
if (typeof url !== 'string' || !/^https/gi.test(url)) throw new Error('Invalid url format')
230237
ytdlpEvents.emit('status', { action: 'getVideoInfo', state: 'progressing' })
231-
log.debug("meta", `added url ${url}`)
238+
log.debug('meta', `added url ${url}`)
232239
const existingDbFile = await queries.downloads.findDownloadByExactUrl(url)
233240
let [dbFile] = await queries.downloads.createDownload({
234241
metaId: existingDbFile?.metaId ?? '',
@@ -244,8 +251,9 @@ const queueYtdlMetaCheck = async (
244251
retryCount: 0
245252
})
246253
ytdlpEvents.emit('list', [dbFile])
247-
if (!dbFile.meta) {
254+
if (!dbFile.meta?.filename) {
248255
const { value: videoInfo, error: videoInfoError } = await ytdl.getVideoInfo(url)
256+
log.debug('metadata', omit(videoInfo, 'thumbnails', 'formats'))
249257
if (videoInfoError || !videoInfo) {
250258
if (videoInfoError) log.error('getVideoInfo', videoInfoError)
251259
await deleteDownloadItem(dbFile)
@@ -254,6 +262,9 @@ const queueYtdlMetaCheck = async (
254262
message: 'URL not supported or video not found'
255263
})
256264
}
265+
dbFile.meta = omit(videoInfo, 'formats', 'thumbnails') as VideoInfo
266+
dbFile.metaId = videoInfo.id
267+
dbFile = await queries.downloads.updateDownload(dbFile.id, dbFile)
257268
return { dbFile, videoInfo }
258269
}
259270
return { dbFile, videoInfo: dbFile.meta }
@@ -267,7 +278,7 @@ const queueYtdlDownload = async (dbFile: SelectDownload, videoInfo: VideoInfo) =
267278
const controller = new AbortController()
268279
const deleteEntry = () => deleteDownloadItem(dbFile)
269280
const updateEntry = () =>
270-
queries.downloads.updateDownload(dbFile.id, dbFile).then(([s]) => {
281+
queries.downloads.updateDownload(dbFile.id, dbFile).then((s) => {
271282
ytdlpEvents.emit('list', [s])
272283
dbFile = s
273284
return s
@@ -300,7 +311,7 @@ const queueYtdlDownload = async (dbFile: SelectDownload, videoInfo: VideoInfo) =
300311
]
301312
if (settings.flags?.nomtime) execArgs.push('--no-mtime')
302313
if (settings.flags?.custom) execArgs.push(...settings.flags.custom.split(' '))
303-
const stream = ytdl.exec(uniq(execArgs), {}, controller.signal)
314+
const stream = ytdl.ytdlp.exec(uniq(execArgs), {}, controller.signal)
304315

305316
async function cancel(id: any) {
306317
if (id && id === dbFile.id) {

0 commit comments

Comments
 (0)