backend: fix lints in services/drive

This commit is contained in:
Norm 2022-10-16 18:20:20 -04:00
parent f2f547172e
commit e814fdc7d1
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE
6 changed files with 23 additions and 19 deletions

View file

@ -16,6 +16,7 @@ import { IRemoteUser, User } from '@/models/entities/user.js';
import { driveChart, perUserDriveChart, instanceChart } from '@/services/chart/index.js'; import { driveChart, perUserDriveChart, instanceChart } from '@/services/chart/index.js';
import { genId } from '@/misc/gen-id.js'; import { genId } from '@/misc/gen-id.js';
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js'; import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
import { DriveFolder } from '@/models/entities/drive-folder.js';
import { deleteFile } from './delete-file.js'; import { deleteFile } from './delete-file.js';
import { GenerateVideoThumbnail } from './generate-video-thumbnail.js'; import { GenerateVideoThumbnail } from './generate-video-thumbnail.js';
import { driveLogger } from './logger.js'; import { driveLogger } from './logger.js';
@ -153,7 +154,10 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
* @param type Content-Type for original * @param type Content-Type for original
* @param generateWeb Generate webpublic or not * @param generateWeb Generate webpublic or not
*/ */
export async function generateAlts(path: string, type: string, generateWeb: boolean) { export async function generateAlts(path: string, type: string, generateWeb: boolean): Promise<{
webpublic: IImage | null;
thumbnail: IImage | null;
}> {
if (type.startsWith('video/')) { if (type.startsWith('video/')) {
try { try {
const thumbnail = await GenerateVideoThumbnail(path); const thumbnail = await GenerateVideoThumbnail(path);
@ -256,7 +260,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool
/** /**
* Upload to ObjectStorage * Upload to ObjectStorage
*/ */
async function upload(key: string, stream: fs.ReadStream | Buffer, _type: string, filename?: string) { async function upload(key: string, stream: fs.ReadStream | Buffer, _type: string, filename?: string): Promise<void> {
const type = (_type === 'image/apng') const type = (_type === 'image/apng')
? 'image/png' ? 'image/png'
: (FILE_TYPE_BROWSERSAFE.includes(_type)) : (FILE_TYPE_BROWSERSAFE.includes(_type))
@ -286,7 +290,7 @@ async function upload(key: string, stream: fs.ReadStream | Buffer, _type: string
if (result) logger.debug(`Uploaded: ${result.Bucket}/${result.Key} => ${result.Location}`); if (result) logger.debug(`Uploaded: ${result.Bucket}/${result.Key} => ${result.Location}`);
} }
async function deleteOldFile(user: IRemoteUser) { async function deleteOldFile(user: IRemoteUser): Promise<void> {
const q = DriveFiles.createQueryBuilder('file') const q = DriveFiles.createQueryBuilder('file')
.where('file.userId = :userId', { userId: user.id }) .where('file.userId = :userId', { userId: user.id })
.andWhere('file.isLink = FALSE'); .andWhere('file.isLink = FALSE');
@ -387,7 +391,7 @@ export async function addFile({
} }
//#endregion //#endregion
const fetchFolder = async () => { const fetchFolder = async (): Promise<DriveFolder | null> => {
if (!folderId) { if (!folderId) {
return null; return null;
} }
@ -425,7 +429,7 @@ export async function addFile({
file.createdAt = new Date(); file.createdAt = new Date();
file.userId = user ? user.id : null; file.userId = user ? user.id : null;
file.userHost = user ? user.host : null; file.userHost = user ? user.host : null;
file.folderId = folder?.id; file.folderId = folder?.id ?? null;
file.comment = comment; file.comment = comment;
file.properties = properties; file.properties = properties;
file.blurhash = info.blurhash || null; file.blurhash = info.blurhash || null;

View file

@ -7,7 +7,7 @@ import { fetchMeta } from '@/misc/fetch-meta.js';
import { InternalStorage } from './internal-storage.js'; import { InternalStorage } from './internal-storage.js';
import { getS3 } from './s3.js'; import { getS3 } from './s3.js';
export async function deleteFile(file: DriveFile, isExpired = false) { export async function deleteFile(file: DriveFile, isExpired = false): Promise<void> {
if (file.storedInternal) { if (file.storedInternal) {
InternalStorage.del(file.accessKey!); InternalStorage.del(file.accessKey!);
@ -33,7 +33,7 @@ export async function deleteFile(file: DriveFile, isExpired = false) {
postProcess(file, isExpired); postProcess(file, isExpired);
} }
export async function deleteFileSync(file: DriveFile, isExpired = false) { export async function deleteFileSync(file: DriveFile, isExpired = false): Promise<void> {
if (file.storedInternal) { if (file.storedInternal) {
InternalStorage.del(file.accessKey!); InternalStorage.del(file.accessKey!);
@ -63,7 +63,7 @@ export async function deleteFileSync(file: DriveFile, isExpired = false) {
postProcess(file, isExpired); postProcess(file, isExpired);
} }
async function postProcess(file: DriveFile, isExpired = false) { async function postProcess(file: DriveFile, isExpired = false): Promise<void> {
// リモートファイル期限切れ削除後は直リンクにする // リモートファイル期限切れ削除後は直リンクにする
if (isExpired && file.userHost !== null && file.uri != null) { if (isExpired && file.userHost !== null && file.uri != null) {
DriveFiles.update(file.id, { DriveFiles.update(file.id, {
@ -89,7 +89,7 @@ async function postProcess(file: DriveFile, isExpired = false) {
} }
} }
export async function deleteObjectStorageFile(key: string) { export async function deleteObjectStorageFile(key: string): Promise<void> {
const meta = await fetchMeta(); const meta = await fetchMeta();
const s3 = getS3(meta); const s3 = getS3(meta);

View file

@ -11,7 +11,7 @@ export type IImage = {
* with resize, remove metadata, resolve orientation, stop animation * with resize, remove metadata, resolve orientation, stop animation
*/ */
export async function convertToJpeg(path: string, width: number, height: number): Promise<IImage> { export async function convertToJpeg(path: string, width: number, height: number): Promise<IImage> {
return convertSharpToJpeg(await sharp(path), width, height); return convertSharpToJpeg(sharp(path), width, height);
} }
export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> { export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> {
@ -39,7 +39,7 @@ export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, heig
* with resize, remove metadata, resolve orientation, stop animation * with resize, remove metadata, resolve orientation, stop animation
*/ */
export async function convertToWebp(path: string, width: number, height: number, quality = 85): Promise<IImage> { export async function convertToWebp(path: string, width: number, height: number, quality = 85): Promise<IImage> {
return convertSharpToWebp(await sharp(path), width, height, quality); return convertSharpToWebp(sharp(path), width, height, quality);
} }
export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, quality = 85): Promise<IImage> { export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, quality = 85): Promise<IImage> {
@ -66,7 +66,7 @@ export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, heig
* with resize, remove metadata, resolve orientation, stop animation * with resize, remove metadata, resolve orientation, stop animation
*/ */
export async function convertToPng(path: string, width: number, height: number): Promise<IImage> { export async function convertToPng(path: string, width: number, height: number): Promise<IImage> {
return convertSharpToPng(await sharp(path), width, height); return convertSharpToPng(sharp(path), width, height);
} }
export async function convertSharpToPng(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> { export async function convertSharpToPng(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> {

View file

@ -10,25 +10,25 @@ const _dirname = dirname(_filename);
export class InternalStorage { export class InternalStorage {
private static readonly path = config.internalStoragePath || Path.resolve(_dirname, '../../../../../files'); private static readonly path = config.internalStoragePath || Path.resolve(_dirname, '../../../../../files');
public static resolvePath = (key: string) => Path.resolve(InternalStorage.path, key); public static resolvePath = (key: string): string => Path.resolve(InternalStorage.path, key);
public static read(key: string) { public static read(key: string): fs.ReadStream {
return fs.createReadStream(InternalStorage.resolvePath(key)); return fs.createReadStream(InternalStorage.resolvePath(key));
} }
public static saveFromPath(key: string, srcPath: string) { public static saveFromPath(key: string, srcPath: string): string {
fs.mkdirSync(InternalStorage.path, { recursive: true }); fs.mkdirSync(InternalStorage.path, { recursive: true });
fs.copyFileSync(srcPath, InternalStorage.resolvePath(key)); fs.copyFileSync(srcPath, InternalStorage.resolvePath(key));
return `${config.url}/files/${key}`; return `${config.url}/files/${key}`;
} }
public static saveFromBuffer(key: string, data: Buffer) { public static saveFromBuffer(key: string, data: Buffer): string {
fs.mkdirSync(InternalStorage.path, { recursive: true }); fs.mkdirSync(InternalStorage.path, { recursive: true });
fs.writeFileSync(InternalStorage.resolvePath(key), data); fs.writeFileSync(InternalStorage.resolvePath(key), data);
return `${config.url}/files/${key}`; return `${config.url}/files/${key}`;
} }
public static del(key: string) { public static del(key: string): void {
fs.unlink(InternalStorage.resolvePath(key), () => {}); fs.unlink(InternalStorage.resolvePath(key), () => {});
} }
} }

View file

@ -3,7 +3,7 @@ import S3 from 'aws-sdk/clients/s3.js';
import { Meta } from '@/models/entities/meta.js'; import { Meta } from '@/models/entities/meta.js';
import { getAgentByUrl } from '@/misc/fetch.js'; import { getAgentByUrl } from '@/misc/fetch.js';
export function getS3(meta: Meta) { export function getS3(meta: Meta): S3 {
const u = meta.objectStorageEndpoint != null const u = meta.objectStorageEndpoint != null
? `${meta.objectStorageUseSSL ? 'https://' : 'http://'}${meta.objectStorageEndpoint}` ? `${meta.objectStorageUseSSL ? 'https://' : 'http://'}${meta.objectStorageEndpoint}`
: `${meta.objectStorageUseSSL ? 'https://' : 'http://'}example.net`; : `${meta.objectStorageUseSSL ? 'https://' : 'http://'}example.net`;

View file

@ -58,7 +58,7 @@ export async function uploadFromUrl({
sensitive, sensitive,
}); });
logger.succ(`Got: ${driveFile.id}`); logger.succ(`Got: ${driveFile.id}`);
return driveFile!; return driveFile;
} catch (e) { } catch (e) {
logger.error(`Failed to create drive file: ${e}`, { logger.error(`Failed to create drive file: ${e}`, {
url, url,