1
0
forked from mirror/misskey
misskey/src/services/drive/upload-from-url.ts
syuilo 987168b863
strictNullChecks (#4666)
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip
2019-04-13 01:43:22 +09:00

55 lines
1.3 KiB
TypeScript

import create from './add-file';
import { User } from '../../models/entities/user';
import { driveLogger } from './logger';
import { createTemp } from '../../misc/create-temp';
import { downloadUrl } from '../../misc/donwload-url';
import { DriveFolder } from '../../models/entities/drive-folder';
import { DriveFile } from '../../models/entities/drive-file';
import { DriveFiles } from '../../models';
const logger = driveLogger.createSubLogger('downloader');
export default async (
url: string,
user: User,
folderId: DriveFolder['id'] | null = null,
uri: string | null = null,
sensitive = false,
force = false,
link = false
): Promise<DriveFile> => {
let name = new URL(url).pathname.split('/').pop() || null;
if (name == null || !DriveFiles.validateFileName(name)) {
name = null;
}
// Create temp file
const [path, cleanup] = await createTemp();
// write content at URL to temp file
await downloadUrl(url, path);
let driveFile: DriveFile;
let error;
try {
driveFile = await create(user, path, name, null, folderId, force, link, url, uri, sensitive);
logger.succ(`Got: ${driveFile.id}`);
} catch (e) {
error = e;
logger.error(`Failed to create drive file: ${e}`, {
url: url,
e: e
});
}
// clean-up
cleanup();
if (error) {
throw error;
} else {
return driveFile!;
}
};