FoundKey/src/services/drive/upload-from-url.ts
nullobsi ffb9646ce9
Add image description support (#7518)
* recieve image descriptions under the name property

* fix other components

* use comment for alt and title

* allow editing of file comment

* allow editing of file comment in note dialog

* federate note comments

* use file instead of this

* backend should accept comment on update

* update now actually accepts comment

* allow multiline descriptions

* image should also have description attached

* Update locales/ja-JP.yml

Co-authored-by: rinsuki <428rinsuki+git@gmail.com>

* Use custom component with side-by-side image

* improve usability on mobile devices

* revert changes

* Update post-form-attaches.vue

* Update drive.file.vue

* Update media-caption.vue

Co-authored-by: rinsuki <428rinsuki+git@gmail.com>
Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
2021-05-28 09:38:09 +09:00

63 lines
1.5 KiB
TypeScript

import { URL } from 'url';
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/download-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: { id: User['id']; host: User['host'] } | null,
folderId: DriveFolder['id'] | null = null,
uri: string | null = null,
sensitive = false,
force = false,
link = false,
comment = null
): Promise<DriveFile> => {
let name = new URL(url).pathname.split('/').pop() || null;
if (name == null || !DriveFiles.validateFileName(name)) {
name = null;
}
// If the comment is same as the name, skip comment
// (image.name is passed in when receiving attachment)
if (comment !== null && name == comment) {
comment = 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, comment, 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!;
}
};