diff options
Diffstat (limited to 'src/server/DashUploadUtils.ts')
-rw-r--r-- | src/server/DashUploadUtils.ts | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/src/server/DashUploadUtils.ts b/src/server/DashUploadUtils.ts index 08cea1de5..8f012f783 100644 --- a/src/server/DashUploadUtils.ts +++ b/src/server/DashUploadUtils.ts @@ -1,6 +1,7 @@ import axios from 'axios'; +import { spawn, exec } from 'child_process'; import { green, red } from 'colors'; -import { ExifImage } from 'exif'; +import { ExifData, ExifImage } from 'exif'; import * as exifr from 'exifr'; import * as ffmpeg from 'fluent-ffmpeg'; import * as formidable from 'formidable'; @@ -18,13 +19,11 @@ import { Duplex, Stream } from 'stream'; import { Utils } from '../Utils'; import { createIfNotExists } from './ActionUtilities'; import { AzureManager } from './ApiManagers/AzureManager'; -import { ParsedPDF } from './PdfTypes'; import { AcceptableMedia, Upload } from './SharedMediaTypes'; import { Directory, clientPathToFile, filesDirectory, pathToDirectory, publicDirectory, serverPathToFile } from './SocketData'; import { resolvedServerUrl } from './server_Initialization'; -const { spawn } = require('child_process'); -const { exec } = require('child_process'); +// eslint-disable-next-line @typescript-eslint/no-var-requires const requestImageSize = require('../client/util/request-image-size'); export enum SizeSuffix { @@ -111,7 +110,7 @@ export namespace DashUploadUtils { // .outputOptions('-c copy') // .videoCodec("copy") .save(outputFilePath) - .on('error', (err: any) => { + .on('error', err => { console.log(err); reject(); }) @@ -130,8 +129,8 @@ export namespace DashUploadUtils { } function resolveExistingFile(name: string, pat: string, directory: Directory, mimetype?: string | null, duration?: number, rawText?: string): Upload.FileResponse<Upload.FileInformation> { - const data = { size: 0, filepath: pat, name, type: mimetype ?? '', originalFilename: name, newFilename: path.basename(pat), mimetype: mimetype || null, hashAlgorithm: false as any }; - const file = { ...data, toJSON: () => ({ ...data, length: 0, filename: data.filepath.replace(/.*\//, ''), mtime: new Date(), mimetype: mimetype || null, toJson: () => undefined as any }) }; + const data = { size: 0, filepath: pat, name, type: mimetype ?? '', originalFilename: name, newFilename: path.basename(pat), mimetype: mimetype || null, hashAlgorithm: false as falsetype }; + const file = { ...data, toJSON: () => ({ ...data, length: 0, filename: data.filepath.replace(/.*\//, ''), mtime: new Date(), mimetype: mimetype || null }) }; return { source: file || null, result: { @@ -184,11 +183,10 @@ export namespace DashUploadUtils { const parseExifData = async (source: string) => { const image = await request.get(source, { encoding: null }); - const { /* data, */ error } = await new Promise<{ data: any; error: any }>(resolve => { + const { /* data, */ error } = await new Promise<{ data: ExifData; error: string | undefined }>(resolve => { // eslint-disable-next-line no-new new ExifImage({ image }, (exifError, data) => { - const reason = (exifError as any)?.code; - resolve({ data, error: reason }); + resolve({ data, error: exifError?.message }); }); }); return error ? { data: undefined, error } : { data: await exifr.parse(image), error }; @@ -252,11 +250,12 @@ export namespace DashUploadUtils { }; // Use the request library to parse out file level image information in the headers - const { headers } = await new Promise<any>((resolve, reject) => { - request.head(resolvedUrl, (error, res) => (error ? reject(error) : resolve(res))); + const headerResult = await new Promise<{ headers: { [key: string]: string } }>((resolve, reject) => { + request.head(resolvedUrl, (error, res) => (error ? reject(error) : resolve(res as { headers: { [key: string]: string } }))); }).catch(e => { console.log('Error processing headers: ', e); }); + const { headers } = headerResult !== null && typeof headerResult === 'object' ? headerResult : { headers: {} as { [key: string]: string } }; try { // Compute the native width and height ofthe image with an npm module @@ -272,9 +271,9 @@ export namespace DashUploadUtils { filename, ...results, }; - } catch (e: any) { + } catch (e: unknown) { console.log(e); - return e; + return new Error(e ? e.toString?.() : 'unkown error'); } }; @@ -331,7 +330,7 @@ export namespace DashUploadUtils { )); // prettier-ignore return Jimp.read(imgBuffer) - .then(async (imgIn: any) => { + .then(async imgIn => { let img = imgIn; await Promise.all( sizes.filter(({ width }) => width).map(({ width, suffix }) => { img = img.resize(width, Jimp.AUTO).write(outputPath(suffix)); @@ -339,7 +338,7 @@ export namespace DashUploadUtils { } )); // prettier-ignore return writtenFiles; }) - .catch((e: any) => { + .catch(e => { console.log('ERROR' + e); return writtenFiles; }); @@ -432,15 +431,17 @@ export namespace DashUploadUtils { * 4) the content type of the image, i.e. image/(jpeg | png | ...) */ export const UploadImage = async (source: string, filename?: string, prefix: string = ''): Promise<Upload.ImageInformation | Error> => { - const metadata = await InspectImage(source); - if (metadata instanceof Error) { - return { name: metadata.name, message: metadata.message }; + const result = await InspectImage(source); + if (result instanceof Error) { + return { name: result.name, message: result.message }; } - const outputFile = filename || metadata.filename || ''; + const outputFile = filename || result.filename || ''; - return UploadInspectedImage(metadata, outputFile, prefix); + return UploadInspectedImage(result, outputFile, prefix); }; + type md5 = 'md5'; + type falsetype = false; export function uploadYoutube(videoId: string, overwriteId: string): Promise<Upload.FileResponse> { return new Promise<Upload.FileResponse<Upload.FileInformation>>(res => { const name = videoId; @@ -448,6 +449,7 @@ export namespace DashUploadUtils { const finalPath = serverPathToFile(Directory.videos, filepath); if (existsSync(finalPath)) { uploadProgress.set(overwriteId, 'computing duration'); + // eslint-disable-next-line @typescript-eslint/no-explicit-any exec(`yt-dlp -o ${finalPath} "https://www.youtube.com/watch?v=${videoId}" --get-duration`, (error: any, stdout: any /* , stderr: any */) => { const time = Array.from(stdout.trim().split(':')).reverse(); const duration = (time.length > 2 ? Number(time[2]) * 1000 * 60 : 0) + (time.length > 1 ? Number(time[1]) * 60 : 0) + (time.length > 0 ? Number(time[0]) : 0); @@ -457,14 +459,17 @@ export namespace DashUploadUtils { uploadProgress.set(overwriteId, 'starting download'); const ytdlp = spawn(`yt-dlp`, ['-o', filepath, `https://www.youtube.com/watch?v=${videoId}`, '--max-filesize', '100M', '-f', 'mp4']); + // eslint-disable-next-line @typescript-eslint/no-explicit-any ytdlp.stdout.on('data', (data: any) => uploadProgress.set(overwriteId, data.toString())); let errors = ''; + // eslint-disable-next-line @typescript-eslint/no-explicit-any ytdlp.stderr.on('data', (data: any) => { uploadProgress.set(overwriteId, 'error:' + data.toString()); errors = data.toString(); }); + // eslint-disable-next-line @typescript-eslint/no-explicit-any ytdlp.on('exit', (code: any) => { if (code) { res({ @@ -484,8 +489,8 @@ export namespace DashUploadUtils { exec(`yt-dlp-o ${filepath} "https://www.youtube.com/watch?v=${videoId}" --get-duration`, (/* error: any, stdout: any, stderr: any */) => { // const time = Array.from(stdout.trim().split(':')).reverse(); // const duration = (time.length > 2 ? Number(time[2]) * 1000 * 60 : 0) + (time.length > 1 ? Number(time[1]) * 60 : 0) + (time.length > 0 ? Number(time[0]) : 0); - const data = { size: 0, filepath, name, mimetype: 'video', originalFilename: name, newFilename: name, hashAlgorithm: 'md5' as 'md5', type: 'video/mp4' }; - const file = { ...data, toJSON: () => ({ ...data, length: 0, filename: data.filepath.replace(/.*\//, ''), mtime: new Date(), toJson: () => undefined as any }) }; + const data = { size: 0, filepath, name, mimetype: 'video', originalFilename: name, newFilename: name, hashAlgorithm: 'md5' as md5, type: 'video/mp4' }; + const file = { ...data, toJSON: () => ({ ...data, length: 0, filename: data.filepath.replace(/.*\//, ''), mtime: new Date() }) }; MoveParsedFile(file, Directory.videos).then(output => res(output)); }); } @@ -517,15 +522,15 @@ export namespace DashUploadUtils { }); } const dataBuffer = readFileSync(file.filepath); - const result: ParsedPDF | any = await parse(dataBuffer).catch((e: any) => e); - if (!result.code) { + const result: parse.Result = await parse(dataBuffer).catch(e => e); + if (result) { await new Promise<void>((resolve, reject) => { const writeStream = createWriteStream(serverPathToFile(Directory.text, textFilename)); writeStream.write(result?.text, error => (error ? reject(error) : resolve())); }); return MoveParsedFile(file, Directory.pdfs, undefined, result?.text, undefined, fileKey); } - return { source: file, result: { name: 'faile pdf pupload', message: `Could not upload (${file.originalFilename}).${result.message}` } }; + return { source: file, result: { name: 'faile pdf pupload', message: `Could not upload (${file.originalFilename}).${result}` } }; } async function UploadCsv(file: File) { @@ -563,7 +568,7 @@ export namespace DashUploadUtils { .videoCodec('copy') // this will copy the data instead of reencode it .save(vidFile.filepath.replace('.mkv', '.mp4')) .on('end', res) - .on('error', (e: any) => console.log(e)); + .on('error', console.log); }); vidFile.filepath = vidFile.filepath.replace('.mkv', '.mp4'); format = '.mp4'; @@ -571,8 +576,9 @@ export namespace DashUploadUtils { if (format.includes('quicktime')) { let abort = false; await new Promise<void>(res => { - ffmpeg.ffprobe(vidFile.filepath, (err: any, metadata: any) => { - if (metadata.streams.some((stream: any) => stream.codec_name === 'hevc')) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ffmpeg.ffprobe(vidFile.filepath, (err: any, metadata: ffmpeg.FfprobeData) => { + if (metadata.streams.some(stream => stream.codec_name === 'hevc')) { abort = true; } res(); |