mirror of
https://git.joinfirefish.org/firefish/firefish.git
synced 2024-05-20 02:31:12 +02:00
Compare commits
2 commits
38cd4bafde
...
98cc23557f
Author | SHA1 | Date | |
---|---|---|---|
98cc23557f | |||
e5bac649c8 |
|
@ -7,20 +7,16 @@ import probeImageSize from "probe-image-size";
|
||||||
import isSvg from "is-svg";
|
import isSvg from "is-svg";
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
import { encode } from "blurhash";
|
import { encode } from "blurhash";
|
||||||
import { inspect } from "node:util";
|
|
||||||
|
|
||||||
export type FileInfo = {
|
type FileInfo = {
|
||||||
size: number;
|
size: number;
|
||||||
md5: string;
|
md5: string;
|
||||||
type: {
|
mime: string;
|
||||||
mime: string;
|
fileExtension: string | null;
|
||||||
ext: string | null;
|
|
||||||
};
|
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
orientation?: number;
|
orientation?: number;
|
||||||
blurhash?: string;
|
blurhash?: string;
|
||||||
warnings: string[];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const TYPE_OCTET_STREAM = {
|
const TYPE_OCTET_STREAM = {
|
||||||
|
@ -37,8 +33,6 @@ const TYPE_SVG = {
|
||||||
* Get file information
|
* Get file information
|
||||||
*/
|
*/
|
||||||
export async function getFileInfo(path: string): Promise<FileInfo> {
|
export async function getFileInfo(path: string): Promise<FileInfo> {
|
||||||
const warnings = [] as string[];
|
|
||||||
|
|
||||||
const size = await getFileSize(path);
|
const size = await getFileSize(path);
|
||||||
const md5 = await calcHash(path);
|
const md5 = await calcHash(path);
|
||||||
|
|
||||||
|
@ -63,14 +57,12 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
|
||||||
"image/avif",
|
"image/avif",
|
||||||
].includes(type.mime)
|
].includes(type.mime)
|
||||||
) {
|
) {
|
||||||
const imageSize = await detectImageSize(path).catch((e) => {
|
const imageSize = await detectImageSize(path).catch((_) => {
|
||||||
warnings.push(`detectImageSize failed:\n${inspect(e)}`);
|
|
||||||
return undefined;
|
return undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
// うまく判定できない画像は octet-stream にする
|
// うまく判定できない画像は octet-stream にする
|
||||||
if (!imageSize) {
|
if (!imageSize) {
|
||||||
warnings.push("cannot detect image dimensions");
|
|
||||||
type = TYPE_OCTET_STREAM;
|
type = TYPE_OCTET_STREAM;
|
||||||
} else if (imageSize.wUnits === "px") {
|
} else if (imageSize.wUnits === "px") {
|
||||||
width = imageSize.width;
|
width = imageSize.width;
|
||||||
|
@ -79,11 +71,8 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
|
||||||
|
|
||||||
// 制限を超えている画像は octet-stream にする
|
// 制限を超えている画像は octet-stream にする
|
||||||
if (imageSize.width > 16383 || imageSize.height > 16383) {
|
if (imageSize.width > 16383 || imageSize.height > 16383) {
|
||||||
warnings.push("image dimensions exceeds limits");
|
|
||||||
type = TYPE_OCTET_STREAM;
|
type = TYPE_OCTET_STREAM;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
warnings.push(`unsupported unit type: ${imageSize.wUnits}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,8 +89,7 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
|
||||||
"image/avif",
|
"image/avif",
|
||||||
].includes(type.mime)
|
].includes(type.mime)
|
||||||
) {
|
) {
|
||||||
blurhash = await getBlurhash(path).catch((e) => {
|
blurhash = await getBlurhash(path).catch((_) => {
|
||||||
warnings.push(`getBlurhash failed:\n${inspect(e)}`);
|
|
||||||
return undefined;
|
return undefined;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -109,12 +97,12 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
|
||||||
return {
|
return {
|
||||||
size,
|
size,
|
||||||
md5,
|
md5,
|
||||||
type,
|
mime: type.mime,
|
||||||
|
fileExtension: type.ext,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
orientation,
|
orientation,
|
||||||
blurhash,
|
blurhash,
|
||||||
warnings,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -477,17 +477,20 @@ export async function addFile({
|
||||||
requestHeaders = null,
|
requestHeaders = null,
|
||||||
usageHint = null,
|
usageHint = null,
|
||||||
}: AddFileArgs): Promise<DriveFile> {
|
}: AddFileArgs): Promise<DriveFile> {
|
||||||
const info = await getFileInfo(path);
|
const fileInfo = await getFileInfo(path);
|
||||||
logger.info(`${JSON.stringify(info)}`);
|
logger.info(`${JSON.stringify(fileInfo)}`);
|
||||||
|
|
||||||
// detect name
|
// detect name
|
||||||
const detectedName =
|
const detectedName =
|
||||||
name || (info.type.ext ? `untitled.${info.type.ext}` : "untitled");
|
name ||
|
||||||
|
(fileInfo.fileExtension
|
||||||
|
? `untitled.${fileInfo.fileExtension}`
|
||||||
|
: "untitled");
|
||||||
|
|
||||||
if (user && !force) {
|
if (user && !force) {
|
||||||
// Check if there is a file with the same hash
|
// Check if there is a file with the same hash
|
||||||
const much = await DriveFiles.findOneBy({
|
const much = await DriveFiles.findOneBy({
|
||||||
md5: info.md5,
|
md5: fileInfo.md5,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -515,7 +518,7 @@ export async function addFile({
|
||||||
logger.debug("drive capacity override applied");
|
logger.debug("drive capacity override applied");
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`overrideCap: ${driveCapacity}bytes, usage: ${usage}bytes, u+s: ${
|
`overrideCap: ${driveCapacity}bytes, usage: ${usage}bytes, u+s: ${
|
||||||
usage + info.size
|
usage + fileInfo.size
|
||||||
}bytes`,
|
}bytes`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -523,7 +526,7 @@ export async function addFile({
|
||||||
logger.debug(`drive usage is ${usage} (max: ${driveCapacity})`);
|
logger.debug(`drive usage is ${usage} (max: ${driveCapacity})`);
|
||||||
|
|
||||||
// If usage limit exceeded
|
// If usage limit exceeded
|
||||||
if (usage + info.size > driveCapacity) {
|
if (usage + fileInfo.size > driveCapacity) {
|
||||||
if (Users.isLocalUser(user)) {
|
if (Users.isLocalUser(user)) {
|
||||||
throw new IdentifiableError(
|
throw new IdentifiableError(
|
||||||
"c6244ed2-a39a-4e1c-bf93-f0fbd7764fa6",
|
"c6244ed2-a39a-4e1c-bf93-f0fbd7764fa6",
|
||||||
|
@ -533,7 +536,7 @@ export async function addFile({
|
||||||
// (アバターまたはバナーを含まず)最も古いファイルを削除する
|
// (アバターまたはバナーを含まず)最も古いファイルを削除する
|
||||||
expireOldFile(
|
expireOldFile(
|
||||||
(await Users.findOneByOrFail({ id: user.id })) as IRemoteUser,
|
(await Users.findOneByOrFail({ id: user.id })) as IRemoteUser,
|
||||||
driveCapacity - info.size,
|
driveCapacity - fileInfo.size,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -561,12 +564,12 @@ export async function addFile({
|
||||||
orientation?: number;
|
orientation?: number;
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
if (info.width) {
|
if (fileInfo.width != null && fileInfo.height != null) {
|
||||||
properties["width"] = info.width;
|
properties.width = fileInfo.width;
|
||||||
properties["height"] = info.height;
|
properties.height = fileInfo.height;
|
||||||
}
|
}
|
||||||
if (info.orientation != null) {
|
if (fileInfo.orientation != null) {
|
||||||
properties["orientation"] = info.orientation;
|
properties.orientation = fileInfo.orientation;
|
||||||
}
|
}
|
||||||
|
|
||||||
const profile = user
|
const profile = user
|
||||||
|
@ -584,7 +587,7 @@ export async function addFile({
|
||||||
file.folderId = folder != null ? folder.id : null;
|
file.folderId = folder != null ? folder.id : null;
|
||||||
file.comment = comment;
|
file.comment = comment;
|
||||||
file.properties = properties;
|
file.properties = properties;
|
||||||
file.blurhash = info.blurhash || null;
|
file.blurhash = fileInfo.blurhash ?? null;
|
||||||
file.isLink = isLink;
|
file.isLink = isLink;
|
||||||
file.requestIp = requestIp;
|
file.requestIp = requestIp;
|
||||||
file.requestHeaders = requestHeaders;
|
file.requestHeaders = requestHeaders;
|
||||||
|
@ -617,9 +620,9 @@ export async function addFile({
|
||||||
if (isLink) {
|
if (isLink) {
|
||||||
try {
|
try {
|
||||||
file.size = 0;
|
file.size = 0;
|
||||||
file.md5 = info.md5;
|
file.md5 = fileInfo.md5;
|
||||||
file.name = detectedName;
|
file.name = detectedName;
|
||||||
file.type = info.type.mime;
|
file.type = fileInfo.mime;
|
||||||
file.storedInternal = false;
|
file.storedInternal = false;
|
||||||
|
|
||||||
file = await DriveFiles.insert(file).then((x) =>
|
file = await DriveFiles.insert(file).then((x) =>
|
||||||
|
@ -644,9 +647,9 @@ export async function addFile({
|
||||||
file,
|
file,
|
||||||
path,
|
path,
|
||||||
detectedName,
|
detectedName,
|
||||||
info.type.mime,
|
fileInfo.mime,
|
||||||
info.md5,
|
fileInfo.md5,
|
||||||
info.size,
|
fileInfo.size,
|
||||||
usageHint,
|
usageHint,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue