wip????
This commit is contained in:
parent
ca0025b499
commit
12afc294f7
4 changed files with 48 additions and 41 deletions
|
|
@ -14,10 +14,13 @@ import { StatusError } from '@/misc/status-error.js';
|
|||
import { LoggerService } from '@/core/LoggerService.js';
|
||||
import type Logger from '@/logger.js';
|
||||
import { buildConnector } from 'undici';
|
||||
import type { Response } from 'undici';
|
||||
|
||||
const pipeline = util.promisify(stream.pipeline);
|
||||
import { bindThis } from '@/decorators.js';
|
||||
|
||||
type NonNullBodyResponse = Response & { body: ReadableStream };
|
||||
|
||||
@Injectable()
|
||||
export class DownloadService {
|
||||
private logger: Logger;
|
||||
|
|
@ -52,7 +55,7 @@ export class DownloadService {
|
|||
}
|
||||
|
||||
@bindThis
|
||||
public fetchUrl(url: string): any {
|
||||
public async fetchUrl(url: string): Promise<NonNullBodyResponse> {
|
||||
this.logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
||||
|
||||
const timeout = 30 * 1000;
|
||||
|
|
@ -75,11 +78,16 @@ export class DownloadService {
|
|||
|
||||
this.logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
||||
|
||||
return response;
|
||||
return response as NonNullBodyResponse;
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public async pipeRequestToFile(response: any, path: string): Promise<void> {
|
||||
public async pipeRequestToFile(_response: Response, path: string): Promise<void> {
|
||||
const response = _response.clone();
|
||||
if (response.body === null) {
|
||||
throw new StatusError('No body', 400, 'No body');
|
||||
}
|
||||
|
||||
try {
|
||||
this.logger.info(`Saving File to ${chalk.cyanBright(path)} from downloading ...`);
|
||||
await pipeline(stream.Readable.fromWeb(response.body), fs.createWriteStream(path));
|
||||
|
|
@ -94,7 +102,7 @@ export class DownloadService {
|
|||
|
||||
@bindThis
|
||||
public async downloadUrl(url: string, path: string): Promise<void> {
|
||||
await this.pipeRequestToFile(this.fetchUrl(url), path);
|
||||
await this.pipeRequestToFile(await this.fetchUrl(url), path);
|
||||
this.logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ import { encode } from 'blurhash';
|
|||
import { createTempDir } from '@/misc/create-temp.js';
|
||||
import { AiService } from '@/core/AiService.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import type { Request } from 'got';
|
||||
import { Response } from 'undici';
|
||||
import { StatusError } from '@/misc/status-error.js';
|
||||
|
||||
const pipeline = util.promisify(stream.pipeline);
|
||||
|
||||
|
|
@ -343,18 +344,18 @@ export class FileInfoService {
|
|||
* Detect MIME Type and extension by stream for performance (this cannot detect SVG)
|
||||
*/
|
||||
@bindThis
|
||||
public async detectRequestType(request: Request): Promise<{
|
||||
public async detectRequestType(_response: Response): Promise<{
|
||||
mime: string;
|
||||
ext: string | null;
|
||||
}> {
|
||||
const response = _response.clone();
|
||||
|
||||
// Check 0 byte
|
||||
if ((request.response?.complete || request.closed) && !request.response?.rawBody?.length) {
|
||||
return TYPE_OCTET_STREAM;
|
||||
if (!response.body) {
|
||||
throw new StatusError('No Body', 400, 'No Body');
|
||||
}
|
||||
|
||||
const copied = request.pipe(new stream.PassThrough());
|
||||
|
||||
const type = await fileTypeFromStream(copied);
|
||||
const type = await fileTypeFromStream(stream.Readable.fromWeb(response.body));
|
||||
|
||||
if (type) {
|
||||
return {
|
||||
|
|
@ -375,7 +376,7 @@ export class FileInfoService {
|
|||
try {
|
||||
const size = await this.getFileSize(path);
|
||||
if (size > 1 * 1024 * 1024) return false;
|
||||
return isSvg(await fs.promises.readFile(target));
|
||||
return isSvg(await fs.promises.readFile(path));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue