Skip to content

Commit

Permalink
内部ストレージ格納で拡張子を付けるように
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Nov 5, 2020
1 parent e6c8271 commit 6a82fba
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/services/drive/add-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ async function save(file: DriveFile, path: string, name: string, type: string, h

if (meta.useObjectStorage) {
//#region ObjectStorage params
let [ext] = (name.match(/\.([a-zA-Z0-9_-]+)$/) || ['']);

if (ext === '') {
if (type === 'image/jpeg') ext = '.jpg';
if (type === 'image/png') ext = '.png';
if (type === 'image/webp') ext = '.webp';
if (type === 'image/apng') ext = '.apng';
if (type === 'image/vnd.mozilla.apng') ext = '.apng';
}
const ext = getExt(name, type);

const baseUrl = meta.objectStorageBaseUrl
|| `${ meta.objectStorageUseSSL ? 'https' : 'http' }://${ meta.objectStorageEndpoint }${ meta.objectStoragePort ? `:${meta.objectStoragePort}` : '' }/${ meta.objectStorageBucket }`;
Expand Down Expand Up @@ -105,18 +97,21 @@ async function save(file: DriveFile, path: string, name: string, type: string, h
const thumbnailAccessKey = 'thumbnail-' + uuid();
const webpublicAccessKey = 'webpublic-' + uuid();

const url = InternalStorage.saveFromPath(accessKey, path);
let url = InternalStorage.saveFromPath(accessKey, path);
url += `/${accessKey}${getExt(name, type)}`;

let thumbnailUrl: string | null = null;
let webpublicUrl: string | null = null;

if (alts.thumbnail) {
thumbnailUrl = InternalStorage.saveFromBuffer(thumbnailAccessKey, alts.thumbnail.data);
thumbnailUrl += `/${thumbnailAccessKey}.jpg`;
logger.info(`thumbnail stored: ${thumbnailAccessKey}`);
}

if (alts.webpublic) {
webpublicUrl = InternalStorage.saveFromBuffer(webpublicAccessKey, alts.webpublic.data);
webpublicUrl += `/${webpublicAccessKey}${getExt(name, type)}`;
logger.info(`web stored: ${webpublicAccessKey}`);
}

Expand Down Expand Up @@ -244,6 +239,25 @@ async function deleteOldFile(user: IRemoteUser) {
}
}

function getExt(name?: string, type?: string) {
let ext = '';

if (name) {
[ext] = (name.match(/\.([a-zA-Z0-9_-]+)$/) || ['']);
}

if (ext === '') {
if (type === 'image/jpeg') ext = '.jpg';
if (type === 'image/png') ext = '.png';
if (type === 'image/webp') ext = '.webp';
if (type === 'image/apng') ext = '.apng';
if (type === 'image/vnd.mozilla.apng') ext = '.apng';
if (type === 'video/mp4') ext = '.mp4';
}

return ext;
}

/**
* Add file to drive
*
Expand Down

0 comments on commit 6a82fba

Please sign in to comment.