2023-01-13 04:40:33 +00:00
|
|
|
import { URL } from "node:url";
|
2023-12-05 07:12:10 +00:00
|
|
|
import type { User } from "@/models/entities/user.js";
|
2023-01-13 04:40:33 +00:00
|
|
|
import { createTemp } from "@/misc/create-temp.js";
|
2023-08-18 09:51:41 +00:00
|
|
|
import { downloadUrl, isPrivateIp } from "@/misc/download-url.js";
|
2023-11-26 20:33:46 +00:00
|
|
|
import type { DriveFolder } from "@/models/entities/drive-folder.js";
|
2024-04-21 01:44:54 +00:00
|
|
|
import type {
|
|
|
|
DriveFile,
|
|
|
|
DriveFileUsageHint,
|
|
|
|
} from "@/models/entities/drive-file.js";
|
2023-01-13 04:40:33 +00:00
|
|
|
import { DriveFiles } from "@/models/index.js";
|
2023-11-26 20:33:46 +00:00
|
|
|
import { driveLogger } from "./logger.js";
|
2023-12-05 07:12:10 +00:00
|
|
|
import { addFile } from "./add-file.js";
|
2024-02-21 13:23:24 +00:00
|
|
|
import { inspect } from "node:util";
|
2018-05-25 11:19:14 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const logger = driveLogger.createSubLogger("downloader");
|
2018-03-27 07:51:12 +00:00
|
|
|
|
2022-01-23 13:52:35 +00:00
|
|
|
type Args = {
|
|
|
|
url: string;
|
2024-04-19 00:34:25 +00:00
|
|
|
user: {
|
|
|
|
id: User["id"];
|
|
|
|
host: User["host"];
|
|
|
|
driveCapacityOverrideMb: User["driveCapacityOverrideMb"];
|
|
|
|
} | null;
|
2023-01-13 04:40:33 +00:00
|
|
|
folderId?: DriveFolder["id"] | null;
|
2022-01-23 13:52:35 +00:00
|
|
|
uri?: string | null;
|
|
|
|
sensitive?: boolean;
|
|
|
|
force?: boolean;
|
|
|
|
isLink?: boolean;
|
|
|
|
comment?: string | null;
|
2022-07-02 06:12:11 +00:00
|
|
|
requestIp?: string | null;
|
|
|
|
requestHeaders?: Record<string, string> | null;
|
2024-04-19 04:54:11 +00:00
|
|
|
usageHint?: DriveFileUsageHint;
|
2022-01-23 13:52:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export async function uploadFromUrl({
|
|
|
|
url,
|
|
|
|
user,
|
|
|
|
folderId = null,
|
|
|
|
uri = null,
|
2018-11-07 03:12:43 +00:00
|
|
|
sensitive = false,
|
2018-11-07 10:43:21 +00:00
|
|
|
force = false,
|
2022-01-23 13:52:35 +00:00
|
|
|
isLink = false,
|
2022-04-03 06:33:22 +00:00
|
|
|
comment = null,
|
2022-07-02 06:12:11 +00:00
|
|
|
requestIp = null,
|
|
|
|
requestHeaders = null,
|
2024-04-19 04:54:11 +00:00
|
|
|
usageHint = null,
|
2022-01-23 13:52:35 +00:00
|
|
|
}: Args): Promise<DriveFile> {
|
2023-08-18 08:57:19 +00:00
|
|
|
const parsedUrl = new URL(url);
|
|
|
|
if (
|
|
|
|
process.env.NODE_ENV === "production" &&
|
2023-08-18 10:22:51 +00:00
|
|
|
isPrivateIp(parsedUrl.hostname.replaceAll(/(\[)|(\])/g, ""))
|
2023-08-18 08:57:19 +00:00
|
|
|
) {
|
|
|
|
throw new Error("Private IP is not allowed");
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = parsedUrl.pathname.split("/").pop() || null;
|
2019-04-12 16:43:22 +00:00
|
|
|
if (name == null || !DriveFiles.validateFileName(name)) {
|
2018-03-27 07:51:12 +00:00
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:38:09 +00:00
|
|
|
// If the comment is same as the name, skip comment
|
|
|
|
// (image.name is passed in when receiving attachment)
|
2024-02-08 19:12:47 +00:00
|
|
|
if (comment != null && name === comment) {
|
2021-05-28 00:38:09 +00:00
|
|
|
comment = null;
|
|
|
|
}
|
|
|
|
|
2018-03-27 07:51:12 +00:00
|
|
|
// Create temp file
|
2019-03-20 19:50:44 +00:00
|
|
|
const [path, cleanup] = await createTemp();
|
2018-03-27 07:51:12 +00:00
|
|
|
|
2018-04-09 19:11:52 +00:00
|
|
|
try {
|
2022-05-25 07:50:22 +00:00
|
|
|
// write content at URL to temp file
|
|
|
|
await downloadUrl(url, path);
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const driveFile = await addFile({
|
|
|
|
user,
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
comment,
|
|
|
|
folderId,
|
|
|
|
force,
|
|
|
|
isLink,
|
|
|
|
url,
|
|
|
|
uri,
|
|
|
|
sensitive,
|
|
|
|
requestIp,
|
|
|
|
requestHeaders,
|
2024-04-19 04:54:11 +00:00
|
|
|
usageHint,
|
2023-01-13 04:40:33 +00:00
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
logger.succ(`Got: ${driveFile.id}`);
|
2024-04-19 00:34:25 +00:00
|
|
|
return driveFile;
|
2018-04-09 19:11:52 +00:00
|
|
|
} catch (e) {
|
2024-02-21 13:23:24 +00:00
|
|
|
logger.error(`Failed to create drive file:\n${inspect(e)}`);
|
2022-05-25 07:50:22 +00:00
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
2018-04-09 19:11:52 +00:00
|
|
|
}
|
2022-01-23 13:52:35 +00:00
|
|
|
}
|