2022-02-27 04:07:39 +02:00
|
|
|
import * as fs from 'node:fs';
|
2017-11-16 15:49:58 +02:00
|
|
|
|
2019-08-18 06:42:58 +03:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2017-11-16 15:49:58 +02:00
|
|
|
|
2022-02-27 04:07:39 +02:00
|
|
|
import { publishMainStream, publishDriveStream } from '@/services/stream.js';
|
|
|
|
import { deleteFile } from './delete-file.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { GenerateVideoThumbnail } from './generate-video-thumbnail.js';
|
|
|
|
import { driveLogger } from './logger.js';
|
2022-04-28 05:14:03 +03:00
|
|
|
import { IImage, convertSharpToJpeg, convertSharpToWebp, convertSharpToPng } from './image-processor.js';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { contentDisposition } from '@/misc/content-disposition.js';
|
|
|
|
import { getFileInfo } from '@/misc/get-file-info.js';
|
|
|
|
import { DriveFiles, DriveFolders, Users, Instances, UserProfiles } from '@/models/index.js';
|
|
|
|
import { InternalStorage } from './internal-storage.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { IRemoteUser, User } from '@/models/entities/user.js';
|
|
|
|
import { driveChart, perUserDriveChart, instanceChart } from '@/services/chart/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
2022-02-28 20:34:40 +02:00
|
|
|
import S3 from 'aws-sdk/clients/s3.js';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { getS3 } from './s3.js';
|
|
|
|
import sharp from 'sharp';
|
|
|
|
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
|
2022-03-26 08:34:00 +02:00
|
|
|
import { IsNull } from 'typeorm';
|
2017-02-06 14:11:09 +02:00
|
|
|
|
2019-02-03 09:45:13 +02:00
|
|
|
const logger = driveLogger.createSubLogger('register', 'yellow');
|
|
|
|
|
2018-11-25 21:25:48 +02:00
|
|
|
/***
|
|
|
|
* Save file
|
|
|
|
* @param path Path for original
|
|
|
|
* @param name Name for original
|
|
|
|
* @param type Content-Type for original
|
|
|
|
* @param hash Hash for original
|
|
|
|
* @param size Size for original
|
|
|
|
*/
|
2019-04-07 15:50:36 +03:00
|
|
|
async function save(file: DriveFile, path: string, name: string, type: string, hash: string, size: number): Promise<DriveFile> {
|
2019-02-04 20:01:36 +02:00
|
|
|
// thunbnail, webpublic を必要なら生成
|
2019-04-07 15:50:36 +03:00
|
|
|
const alts = await generateAlts(path, type, !file.uri);
|
2018-08-16 00:27:35 +03:00
|
|
|
|
2019-05-15 19:07:32 +03:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
|
|
|
if (meta.useObjectStorage) {
|
2019-02-04 20:01:36 +02:00
|
|
|
//#region ObjectStorage params
|
2018-10-22 22:37:37 +03:00
|
|
|
let [ext] = (name.match(/\.([a-zA-Z0-9_-]+)$/) || ['']);
|
|
|
|
|
|
|
|
if (ext === '') {
|
|
|
|
if (type === 'image/jpeg') ext = '.jpg';
|
|
|
|
if (type === 'image/png') ext = '.png';
|
|
|
|
if (type === 'image/webp') ext = '.webp';
|
2019-07-04 08:45:28 +03:00
|
|
|
if (type === 'image/apng') ext = '.apng';
|
|
|
|
if (type === 'image/vnd.mozilla.apng') ext = '.apng';
|
2018-10-22 22:37:37 +03:00
|
|
|
}
|
2018-10-16 14:59:36 +03:00
|
|
|
|
2022-01-03 00:35:02 +02:00
|
|
|
// 拡張子からContent-Typeを設定してそうな挙動を示すオブジェクトストレージ (upcloud?) も存在するので、
|
|
|
|
// 許可されているファイル形式でしか拡張子をつけない
|
|
|
|
if (!FILE_TYPE_BROWSERSAFE.includes(type)) {
|
|
|
|
ext = '';
|
|
|
|
}
|
|
|
|
|
2019-05-15 19:07:32 +03:00
|
|
|
const baseUrl = meta.objectStorageBaseUrl
|
|
|
|
|| `${ meta.objectStorageUseSSL ? 'https' : 'http' }://${ meta.objectStorageEndpoint }${ meta.objectStoragePort ? `:${meta.objectStoragePort}` : '' }/${ meta.objectStorageBucket }`;
|
2019-02-04 20:01:36 +02:00
|
|
|
|
|
|
|
// for original
|
2019-08-18 06:42:58 +03:00
|
|
|
const key = `${meta.objectStoragePrefix}/${uuid()}${ext}`;
|
2019-02-04 20:01:36 +02:00
|
|
|
const url = `${ baseUrl }/${ key }`;
|
2018-07-26 21:21:50 +03:00
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
// for alts
|
2019-04-12 19:43:22 +03:00
|
|
|
let webpublicKey: string | null = null;
|
|
|
|
let webpublicUrl: string | null = null;
|
|
|
|
let thumbnailKey: string | null = null;
|
|
|
|
let thumbnailUrl: string | null = null;
|
2019-02-04 20:01:36 +02:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Uploads
|
2019-02-03 09:45:13 +02:00
|
|
|
logger.info(`uploading original: ${key}`);
|
2018-11-25 21:25:48 +02:00
|
|
|
const uploads = [
|
2021-12-09 16:58:30 +02:00
|
|
|
upload(key, fs.createReadStream(path), type, name),
|
2018-11-25 21:25:48 +02:00
|
|
|
];
|
2018-07-26 21:21:50 +03:00
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
if (alts.webpublic) {
|
2019-08-18 06:42:58 +03:00
|
|
|
webpublicKey = `${meta.objectStoragePrefix}/webpublic-${uuid()}.${alts.webpublic.ext}`;
|
2019-02-04 20:01:36 +02:00
|
|
|
webpublicUrl = `${ baseUrl }/${ webpublicKey }`;
|
|
|
|
|
2019-02-03 09:45:13 +02:00
|
|
|
logger.info(`uploading webpublic: ${webpublicKey}`);
|
2019-03-18 08:23:45 +02:00
|
|
|
uploads.push(upload(webpublicKey, alts.webpublic.data, alts.webpublic.type, name));
|
2018-11-25 21:25:48 +02:00
|
|
|
}
|
2018-07-23 19:58:11 +03:00
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
if (alts.thumbnail) {
|
2019-08-18 06:42:58 +03:00
|
|
|
thumbnailKey = `${meta.objectStoragePrefix}/thumbnail-${uuid()}.${alts.thumbnail.ext}`;
|
2019-02-04 20:01:36 +02:00
|
|
|
thumbnailUrl = `${ baseUrl }/${ thumbnailKey }`;
|
|
|
|
|
2019-02-03 09:45:13 +02:00
|
|
|
logger.info(`uploading thumbnail: ${thumbnailKey}`);
|
2019-02-04 20:01:36 +02:00
|
|
|
uploads.push(upload(thumbnailKey, alts.thumbnail.data, alts.thumbnail.type));
|
2018-08-16 00:27:35 +03:00
|
|
|
}
|
|
|
|
|
2018-11-25 21:25:48 +02:00
|
|
|
await Promise.all(uploads);
|
2019-02-04 20:01:36 +02:00
|
|
|
//#endregion
|
2018-11-25 21:25:48 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
file.url = url;
|
|
|
|
file.thumbnailUrl = thumbnailUrl;
|
|
|
|
file.webpublicUrl = webpublicUrl;
|
|
|
|
file.accessKey = key;
|
|
|
|
file.thumbnailAccessKey = thumbnailKey;
|
|
|
|
file.webpublicAccessKey = webpublicKey;
|
2022-01-19 19:40:13 +02:00
|
|
|
file.webpublicType = alts.webpublic?.type ?? null;
|
2019-04-07 15:50:36 +03:00
|
|
|
file.name = name;
|
|
|
|
file.type = type;
|
|
|
|
file.md5 = hash;
|
|
|
|
file.size = size;
|
|
|
|
file.storedInternal = false;
|
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
return await DriveFiles.insert(file).then(x => DriveFiles.findOneByOrFail(x.identifiers[0]));
|
2019-04-07 15:50:36 +03:00
|
|
|
} else { // use internal storage
|
2019-08-18 06:42:58 +03:00
|
|
|
const accessKey = uuid();
|
|
|
|
const thumbnailAccessKey = 'thumbnail-' + uuid();
|
|
|
|
const webpublicAccessKey = 'webpublic-' + uuid();
|
2019-04-07 15:50:36 +03:00
|
|
|
|
|
|
|
const url = InternalStorage.saveFromPath(accessKey, path);
|
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
let thumbnailUrl: string | null = null;
|
|
|
|
let webpublicUrl: string | null = null;
|
2018-08-16 00:27:35 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (alts.thumbnail) {
|
|
|
|
thumbnailUrl = InternalStorage.saveFromBuffer(thumbnailAccessKey, alts.thumbnail.data);
|
|
|
|
logger.info(`thumbnail stored: ${thumbnailAccessKey}`);
|
2018-11-25 21:25:48 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (alts.webpublic) {
|
|
|
|
webpublicUrl = InternalStorage.saveFromBuffer(webpublicAccessKey, alts.webpublic.data);
|
|
|
|
logger.info(`web stored: ${webpublicAccessKey}`);
|
2019-02-04 20:01:36 +02:00
|
|
|
}
|
2018-08-16 00:27:35 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
file.storedInternal = true;
|
|
|
|
file.url = url;
|
|
|
|
file.thumbnailUrl = thumbnailUrl;
|
|
|
|
file.webpublicUrl = webpublicUrl;
|
|
|
|
file.accessKey = accessKey;
|
|
|
|
file.thumbnailAccessKey = thumbnailAccessKey;
|
|
|
|
file.webpublicAccessKey = webpublicAccessKey;
|
2022-01-19 19:40:13 +02:00
|
|
|
file.webpublicType = alts.webpublic?.type ?? null;
|
2019-04-07 15:50:36 +03:00
|
|
|
file.name = name;
|
|
|
|
file.type = type;
|
|
|
|
file.md5 = hash;
|
|
|
|
file.size = size;
|
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
return await DriveFiles.insert(file).then(x => DriveFiles.findOneByOrFail(x.identifiers[0]));
|
2019-02-04 20:01:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 00:27:35 +03:00
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
/**
|
|
|
|
* Generate webpublic, thumbnail, etc
|
|
|
|
* @param path Path for original
|
|
|
|
* @param type Content-Type for original
|
|
|
|
* @param generateWeb Generate webpublic or not
|
|
|
|
*/
|
|
|
|
export async function generateAlts(path: string, type: string, generateWeb: boolean) {
|
2020-08-18 16:48:52 +03:00
|
|
|
if (type.startsWith('video/')) {
|
|
|
|
try {
|
|
|
|
const thumbnail = await GenerateVideoThumbnail(path);
|
|
|
|
return {
|
|
|
|
webpublic: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
thumbnail,
|
2020-08-18 16:48:52 +03:00
|
|
|
};
|
2022-02-03 14:38:57 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`GenerateVideoThumbnail failed: ${err}`);
|
2020-08-18 16:48:52 +03:00
|
|
|
return {
|
|
|
|
webpublic: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
thumbnail: null,
|
2020-08-18 16:48:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-01 11:00:53 +02:00
|
|
|
if (!['image/jpeg', 'image/png', 'image/webp', 'image/svg+xml'].includes(type)) {
|
2020-08-21 22:25:25 +03:00
|
|
|
logger.debug(`web image and thumbnail not created (not an required file)`);
|
|
|
|
return {
|
|
|
|
webpublic: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
thumbnail: null,
|
2020-08-21 22:25:25 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let img: sharp.Sharp | null = null;
|
2022-04-28 05:14:03 +03:00
|
|
|
let satisfyWebpublic: boolean;
|
2020-08-21 22:25:25 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
img = sharp(path);
|
|
|
|
const metadata = await img.metadata();
|
|
|
|
const isAnimated = metadata.pages && metadata.pages > 1;
|
2020-08-18 16:48:52 +03:00
|
|
|
|
2020-08-21 22:25:25 +03:00
|
|
|
// skip animated
|
|
|
|
if (isAnimated) {
|
|
|
|
return {
|
|
|
|
webpublic: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
thumbnail: null,
|
2020-08-21 22:25:25 +03:00
|
|
|
};
|
|
|
|
}
|
2022-04-28 05:14:03 +03:00
|
|
|
|
|
|
|
satisfyWebpublic = !!(
|
|
|
|
type !== 'image/svg+xml' && type !== 'image/webp' &&
|
|
|
|
!(metadata.exif || metadata.iptc || metadata.xmp || metadata.tifftagPhotoshop) &&
|
|
|
|
metadata.width && metadata.width <= 2048 &&
|
|
|
|
metadata.height && metadata.height <= 2048
|
|
|
|
);
|
2022-02-03 14:38:57 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`sharp failed: ${err}`);
|
2020-08-18 16:48:52 +03:00
|
|
|
return {
|
|
|
|
webpublic: null,
|
2021-12-09 16:58:30 +02:00
|
|
|
thumbnail: null,
|
2020-08-18 16:48:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
// #region webpublic
|
2019-04-12 19:43:22 +03:00
|
|
|
let webpublic: IImage | null = null;
|
2019-02-04 20:01:36 +02:00
|
|
|
|
2022-04-28 05:14:03 +03:00
|
|
|
if (generateWeb && !satisfyWebpublic) {
|
2019-02-04 20:01:36 +02:00
|
|
|
logger.info(`creating web image`);
|
2018-11-25 21:25:48 +02:00
|
|
|
|
2019-06-13 16:59:15 +03:00
|
|
|
try {
|
2022-04-28 05:14:03 +03:00
|
|
|
if (['image/jpeg', 'image/webp'].includes(type)) {
|
2020-08-18 16:48:52 +03:00
|
|
|
webpublic = await convertSharpToJpeg(img, 2048, 2048);
|
2022-04-28 05:14:03 +03:00
|
|
|
} else if (['image/png'].includes(type)) {
|
|
|
|
webpublic = await convertSharpToPng(img, 2048, 2048);
|
|
|
|
} else if (['image/svg+xml'].includes(type)) {
|
2020-08-18 16:48:52 +03:00
|
|
|
webpublic = await convertSharpToPng(img, 2048, 2048);
|
2020-01-04 00:20:41 +02:00
|
|
|
} else {
|
|
|
|
logger.debug(`web image not created (not an required image)`);
|
2019-06-13 16:59:15 +03:00
|
|
|
}
|
2022-02-03 14:38:57 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`web image not created (an error occured)`, err as Error);
|
2018-08-16 00:27:35 +03:00
|
|
|
}
|
2019-02-04 20:01:36 +02:00
|
|
|
} else {
|
2022-04-28 05:14:03 +03:00
|
|
|
if (satisfyWebpublic) logger.info(`web image not created (original satisfies webpublic)`);
|
|
|
|
else logger.info(`web image not created (from remote)`);
|
2019-02-04 20:01:36 +02:00
|
|
|
}
|
|
|
|
// #endregion webpublic
|
2018-08-16 00:27:35 +03:00
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
// #region thumbnail
|
2019-04-12 19:43:22 +03:00
|
|
|
let thumbnail: IImage | null = null;
|
2019-02-04 20:01:36 +02:00
|
|
|
|
2019-06-13 16:59:15 +03:00
|
|
|
try {
|
2022-04-28 05:14:03 +03:00
|
|
|
if (['image/jpeg', 'image/webp', 'image/png', 'image/svg+xml'].includes(type)) {
|
|
|
|
thumbnail = await convertSharpToWebp(img, 498, 280);
|
2020-01-04 00:20:41 +02:00
|
|
|
} else {
|
|
|
|
logger.debug(`thumbnail not created (not an required file)`);
|
2019-02-04 20:01:36 +02:00
|
|
|
}
|
2022-02-03 14:38:57 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`thumbnail not created (an error occured)`, err as Error);
|
2018-07-23 19:58:11 +03:00
|
|
|
}
|
2019-02-04 20:01:36 +02:00
|
|
|
// #endregion thumbnail
|
|
|
|
|
|
|
|
return {
|
|
|
|
webpublic,
|
|
|
|
thumbnail,
|
|
|
|
};
|
2018-07-23 19:58:11 +03:00
|
|
|
}
|
2018-05-03 14:03:14 +03:00
|
|
|
|
2019-02-04 20:01:36 +02:00
|
|
|
/**
|
|
|
|
* Upload to ObjectStorage
|
|
|
|
*/
|
2019-03-18 08:23:45 +02:00
|
|
|
async function upload(key: string, stream: fs.ReadStream | Buffer, type: string, filename?: string) {
|
2019-07-05 11:44:23 +03:00
|
|
|
if (type === 'image/apng') type = 'image/png';
|
2022-01-01 15:25:30 +02:00
|
|
|
if (!FILE_TYPE_BROWSERSAFE.includes(type)) type = 'application/octet-stream';
|
2019-07-05 11:44:23 +03:00
|
|
|
|
2019-05-15 19:07:32 +03:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-07-28 03:49:02 +03:00
|
|
|
const params = {
|
|
|
|
Bucket: meta.objectStorageBucket,
|
|
|
|
Key: key,
|
|
|
|
Body: stream,
|
|
|
|
ContentType: type,
|
|
|
|
CacheControl: 'max-age=31536000, immutable',
|
|
|
|
} as S3.PutObjectRequest;
|
2018-11-25 21:25:48 +02:00
|
|
|
|
2019-07-28 03:49:02 +03:00
|
|
|
if (filename) params.ContentDisposition = contentDisposition('inline', filename);
|
2020-08-13 14:05:01 +03:00
|
|
|
if (meta.objectStorageSetPublicRead) params.ACL = 'public-read';
|
2019-03-18 08:23:45 +02:00
|
|
|
|
2019-07-28 03:49:02 +03:00
|
|
|
const s3 = getS3(meta);
|
2019-03-18 08:23:45 +02:00
|
|
|
|
2020-08-13 18:54:33 +03:00
|
|
|
const upload = s3.upload(params, {
|
2021-12-09 16:58:30 +02:00
|
|
|
partSize: s3.endpoint?.hostname === 'storage.googleapis.com' ? 500 * 1024 * 1024 : 8 * 1024 * 1024,
|
2020-08-13 18:54:33 +03:00
|
|
|
});
|
2019-07-28 03:49:02 +03:00
|
|
|
|
2020-03-14 04:33:19 +02:00
|
|
|
const result = await upload.promise();
|
|
|
|
if (result) logger.debug(`Uploaded: ${result.Bucket}/${result.Key} => ${result.Location}`);
|
2018-11-25 21:25:48 +02:00
|
|
|
}
|
|
|
|
|
2018-05-25 12:41:49 +03:00
|
|
|
async function deleteOldFile(user: IRemoteUser) {
|
2019-04-14 05:50:31 +03:00
|
|
|
const q = DriveFiles.createQueryBuilder('file')
|
2021-05-30 07:48:23 +03:00
|
|
|
.where('file.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere('file.isLink = FALSE');
|
2019-04-14 05:50:31 +03:00
|
|
|
|
|
|
|
if (user.avatarId) {
|
|
|
|
q.andWhere('file.id != :avatarId', { avatarId: user.avatarId });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.bannerId) {
|
2019-04-15 13:05:35 +03:00
|
|
|
q.andWhere('file.id != :bannerId', { bannerId: user.bannerId });
|
2019-04-14 05:50:31 +03:00
|
|
|
}
|
|
|
|
|
2019-04-15 17:00:39 +03:00
|
|
|
q.orderBy('file.id', 'ASC');
|
2019-04-14 05:50:31 +03:00
|
|
|
|
|
|
|
const oldFile = await q.getOne();
|
2018-05-25 12:41:49 +03:00
|
|
|
|
|
|
|
if (oldFile) {
|
2019-05-27 10:54:47 +03:00
|
|
|
deleteFile(oldFile, true);
|
2018-05-25 12:41:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 15:52:35 +02:00
|
|
|
type AddFileArgs = {
|
|
|
|
/** User who wish to add file */
|
|
|
|
user: { id: User['id']; host: User['host'] } | null;
|
|
|
|
/** File path */
|
|
|
|
path: string;
|
|
|
|
/** Name */
|
|
|
|
name?: string | null;
|
|
|
|
/** Comment */
|
|
|
|
comment?: string | null;
|
|
|
|
/** Folder ID */
|
|
|
|
folderId?: any;
|
|
|
|
/** If set to true, forcibly upload the file even if there is a file with the same hash. */
|
|
|
|
force?: boolean;
|
|
|
|
/** Do not save file to local */
|
|
|
|
isLink?: boolean;
|
|
|
|
/** URL of source (URLからアップロードされた場合(ローカル/リモート)の元URL) */
|
|
|
|
url?: string | null;
|
|
|
|
/** URL of source (リモートインスタンスのURLからアップロードされた場合の元URL) */
|
|
|
|
uri?: string | null;
|
|
|
|
/** Mark file as sensitive */
|
|
|
|
sensitive?: boolean | null;
|
|
|
|
};
|
|
|
|
|
2018-05-25 12:41:49 +03:00
|
|
|
/**
|
|
|
|
* Add file to drive
|
|
|
|
*
|
|
|
|
*/
|
2022-01-23 15:52:35 +02:00
|
|
|
export async function addFile({
|
|
|
|
user,
|
|
|
|
path,
|
|
|
|
name = null,
|
|
|
|
comment = null,
|
|
|
|
folderId = null,
|
|
|
|
force = false,
|
|
|
|
isLink = false,
|
|
|
|
url = null,
|
|
|
|
uri = null,
|
|
|
|
sensitive = null
|
|
|
|
}: AddFileArgs): Promise<DriveFile> {
|
2020-01-12 09:40:58 +02:00
|
|
|
const info = await getFileInfo(path);
|
|
|
|
logger.info(`${JSON.stringify(info)}`);
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2017-11-13 21:54:47 +02:00
|
|
|
// detect name
|
2020-01-12 09:40:58 +02:00
|
|
|
const detectedName = name || (info.type.ext ? `untitled.${info.type.ext}` : 'untitled');
|
2017-11-13 21:54:47 +02:00
|
|
|
|
2020-01-29 23:06:50 +02:00
|
|
|
if (user && !force) {
|
2017-11-13 21:54:47 +02:00
|
|
|
// Check if there is a file with the same hash
|
2022-03-26 08:34:00 +02:00
|
|
|
const much = await DriveFiles.findOneBy({
|
2020-01-12 09:40:58 +02:00
|
|
|
md5: info.md5,
|
2019-04-07 15:50:36 +03:00
|
|
|
userId: user.id,
|
2017-11-13 21:54:47 +02:00
|
|
|
});
|
|
|
|
|
2018-06-08 16:03:14 +03:00
|
|
|
if (much) {
|
2019-04-07 15:50:36 +03:00
|
|
|
logger.info(`file with same hash is found: ${much.id}`);
|
2017-11-13 21:54:47 +02:00
|
|
|
return much;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 12:41:49 +03:00
|
|
|
//#region Check drive usage
|
2020-01-29 23:06:50 +02:00
|
|
|
if (user && !isLink) {
|
2020-11-25 14:31:34 +02:00
|
|
|
const usage = await DriveFiles.calcDriveUsageOf(user);
|
2017-11-16 15:49:58 +02:00
|
|
|
|
2018-11-06 00:14:43 +02:00
|
|
|
const instance = await fetchMeta();
|
2019-04-07 15:50:36 +03:00
|
|
|
const driveCapacity = 1024 * 1024 * (Users.isLocalUser(user) ? instance.localDriveCapacityMb : instance.remoteDriveCapacityMb);
|
|
|
|
|
|
|
|
logger.debug(`drive usage is ${usage} (max: ${driveCapacity})`);
|
2018-07-21 13:17:15 +03:00
|
|
|
|
2018-05-25 14:19:14 +03:00
|
|
|
// If usage limit exceeded
|
2020-01-12 09:40:58 +02:00
|
|
|
if (usage + info.size > driveCapacity) {
|
2019-04-07 15:50:36 +03:00
|
|
|
if (Users.isLocalUser(user)) {
|
2019-04-13 22:17:24 +03:00
|
|
|
throw new Error('no-free-space');
|
2018-05-25 14:19:14 +03:00
|
|
|
} else {
|
|
|
|
// (アバターまたはバナーを含まず)最も古いファイルを削除する
|
2022-03-26 08:34:00 +02:00
|
|
|
deleteOldFile(await Users.findOneByOrFail({ id: user.id }) as IRemoteUser);
|
2018-05-25 14:19:14 +03:00
|
|
|
}
|
2018-05-25 12:41:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
const fetchFolder = async () => {
|
|
|
|
if (!folderId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
const driveFolder = await DriveFolders.findOneBy({
|
2019-04-07 15:50:36 +03:00
|
|
|
id: folderId,
|
2022-03-26 08:34:00 +02:00
|
|
|
userId: user ? user.id : IsNull(),
|
2018-05-25 12:41:49 +03:00
|
|
|
});
|
|
|
|
|
2019-04-13 22:17:24 +03:00
|
|
|
if (driveFolder == null) throw new Error('folder-not-found');
|
2018-05-25 12:41:49 +03:00
|
|
|
|
|
|
|
return driveFolder;
|
|
|
|
};
|
|
|
|
|
2020-01-12 09:40:58 +02:00
|
|
|
const properties: {
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
2021-12-03 04:19:28 +02:00
|
|
|
orientation?: number;
|
2020-01-12 09:40:58 +02:00
|
|
|
} = {};
|
2017-12-11 06:33:33 +02:00
|
|
|
|
2020-01-12 09:40:58 +02:00
|
|
|
if (info.width) {
|
|
|
|
properties['width'] = info.width;
|
|
|
|
properties['height'] = info.height;
|
|
|
|
}
|
2021-12-03 04:19:28 +02:00
|
|
|
if (info.orientation != null) {
|
|
|
|
properties['orientation'] = info.orientation;
|
|
|
|
}
|
2017-12-08 12:42:02 +02:00
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
const profile = user ? await UserProfiles.findOneBy({ userId: user.id }) : null;
|
2019-04-10 09:04:27 +03:00
|
|
|
|
2020-01-12 09:40:58 +02:00
|
|
|
const folder = await fetchFolder();
|
2018-05-25 12:41:49 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
let file = new DriveFile();
|
|
|
|
file.id = genId();
|
|
|
|
file.createdAt = new Date();
|
2020-01-29 23:06:50 +02:00
|
|
|
file.userId = user ? user.id : null;
|
|
|
|
file.userHost = user ? user.host : null;
|
2019-04-07 15:50:36 +03:00
|
|
|
file.folderId = folder !== null ? folder.id : null;
|
|
|
|
file.comment = comment;
|
|
|
|
file.properties = properties;
|
2020-07-18 18:24:07 +03:00
|
|
|
file.blurhash = info.blurhash || null;
|
2019-04-09 17:07:08 +03:00
|
|
|
file.isLink = isLink;
|
2020-01-29 23:06:50 +02:00
|
|
|
file.isSensitive = user
|
|
|
|
? Users.isLocalUser(user) && profile!.alwaysMarkNsfw ? true :
|
|
|
|
(sensitive !== null && sensitive !== undefined)
|
|
|
|
? sensitive
|
|
|
|
: false
|
|
|
|
: false;
|
2018-04-03 17:45:13 +03:00
|
|
|
|
2018-05-04 12:32:03 +03:00
|
|
|
if (url !== null) {
|
2019-04-07 15:50:36 +03:00
|
|
|
file.src = url;
|
2018-07-23 23:04:43 +03:00
|
|
|
|
|
|
|
if (isLink) {
|
2019-04-07 15:50:36 +03:00
|
|
|
file.url = url;
|
2019-12-31 10:23:47 +02:00
|
|
|
// ローカルプロキシ用
|
|
|
|
file.accessKey = uuid();
|
|
|
|
file.thumbnailAccessKey = 'thumbnail-' + uuid();
|
|
|
|
file.webpublicAccessKey = 'webpublic-' + uuid();
|
2018-07-23 23:04:43 +03:00
|
|
|
}
|
2018-05-04 12:32:03 +03:00
|
|
|
}
|
|
|
|
|
2018-04-03 17:45:13 +03:00
|
|
|
if (uri !== null) {
|
2019-04-07 15:50:36 +03:00
|
|
|
file.uri = uri;
|
2018-04-03 17:45:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 08:33:34 +03:00
|
|
|
if (isLink) {
|
|
|
|
try {
|
2019-04-07 15:50:36 +03:00
|
|
|
file.size = 0;
|
2020-01-12 09:40:58 +02:00
|
|
|
file.md5 = info.md5;
|
2019-04-07 15:50:36 +03:00
|
|
|
file.name = detectedName;
|
2020-01-12 09:40:58 +02:00
|
|
|
file.type = info.type.mime;
|
2019-05-02 11:15:14 +03:00
|
|
|
file.storedInternal = false;
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
file = await DriveFiles.insert(file).then(x => DriveFiles.findOneByOrFail(x.identifiers[0]));
|
2022-02-03 14:38:57 +02:00
|
|
|
} catch (err) {
|
2018-08-10 08:33:34 +03:00
|
|
|
// duplicate key error (when already registered)
|
2022-02-03 14:38:57 +02:00
|
|
|
if (isDuplicateKeyValueError(err)) {
|
2019-04-07 15:50:36 +03:00
|
|
|
logger.info(`already registered ${file.uri}`);
|
2018-08-10 08:33:34 +03:00
|
|
|
|
2022-03-26 08:34:00 +02:00
|
|
|
file = await DriveFiles.findOneBy({
|
|
|
|
uri: file.uri!,
|
|
|
|
userId: user ? user.id : IsNull(),
|
2019-04-12 19:43:22 +03:00
|
|
|
}) as DriveFile;
|
2018-08-10 08:33:34 +03:00
|
|
|
} else {
|
2022-02-03 14:38:57 +02:00
|
|
|
logger.error(err as Error);
|
|
|
|
throw err;
|
2018-08-10 08:33:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-12 09:40:58 +02:00
|
|
|
file = await (save(file, path, detectedName, info.type.mime, info.md5, info.size));
|
2018-08-10 08:33:34 +03:00
|
|
|
}
|
2018-05-25 12:41:49 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
logger.succ(`drive file has been created ${file.id}`);
|
2018-05-25 12:41:49 +03:00
|
|
|
|
2020-01-29 23:06:50 +02:00
|
|
|
if (user) {
|
|
|
|
DriveFiles.pack(file, { self: true }).then(packedFile => {
|
|
|
|
// Publish driveFileCreated event
|
|
|
|
publishMainStream(user.id, 'driveFileCreated', packedFile);
|
|
|
|
publishDriveStream(user.id, 'fileCreated', packedFile);
|
|
|
|
});
|
|
|
|
}
|
2018-05-03 14:03:14 +03:00
|
|
|
|
2018-08-18 17:56:44 +03:00
|
|
|
// 統計を更新
|
2019-04-07 15:50:36 +03:00
|
|
|
driveChart.update(file, true);
|
|
|
|
perUserDriveChart.update(file, true);
|
|
|
|
if (file.userHost !== null) {
|
|
|
|
instanceChart.updateDrive(file, true);
|
2019-02-08 09:58:57 +02:00
|
|
|
}
|
2018-05-03 14:03:14 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
return file;
|
2018-05-25 12:41:49 +03:00
|
|
|
}
|