hippofish/src/misc/get-drive-file-url.ts

32 lines
935 B
TypeScript
Raw Normal View History

2018-10-31 14:35:02 +01:00
import { IDriveFile } from '../models/drive-file';
import config from '../config';
export default function(file: IDriveFile, thumbnail = false): string {
if (file == null) return null;
2019-02-01 13:37:34 +01:00
const isImage = file.contentType && file.contentType.startsWith('image/');
2018-10-31 14:35:02 +01:00
if (file.metadata.withoutChunks) {
if (thumbnail) {
2019-02-01 13:37:34 +01:00
return file.metadata.thumbnailUrl || file.metadata.webpublicUrl || (isImage ? file.metadata.url : null);
2018-10-31 14:35:02 +01:00
} else {
return file.metadata.webpublicUrl || file.metadata.url;
2018-10-31 14:35:02 +01:00
}
} else {
if (thumbnail) {
2019-02-24 04:53:22 +01:00
return `${config.driveUrl}/${file._id}?thumbnail`;
2018-10-31 14:35:02 +01:00
} else {
2019-02-24 04:53:22 +01:00
return `${config.driveUrl}/${file._id}?web`;
2018-10-31 14:35:02 +01:00
}
}
}
export function getOriginalUrl(file: IDriveFile) {
if (file.metadata && file.metadata.url) {
return file.metadata.url;
}
const accessKey = file.metadata ? file.metadata.accessKey : null;
2019-02-24 04:53:22 +01:00
return `${config.driveUrl}/${file._id}${accessKey ? '?original=' + accessKey : ''}`;
}