DBSchema: Add fileUrl to media_upload table

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-02-27 11:24:41 +01:00
parent e2b2059bde
commit eb7e6b55eb
2 changed files with 30 additions and 10 deletions

View file

@ -135,6 +135,7 @@ entity "media_upload" {
*noteId : uuid <<FK note>> *noteId : uuid <<FK note>>
*userId : uuid <<FK user>> *userId : uuid <<FK user>>
*backendType: text *backendType: text
*fileUrl: text
backendData: text backendData: text
*createdAt : date *createdAt : date
} }

View file

@ -10,10 +10,17 @@ import { ConsoleLoggerService } from '../../logger/console-logger.service';
import { MediaBackend } from '../media-backend.interface'; import { MediaBackend } from '../media-backend.interface';
import { BackendData } from '../media-upload.entity'; import { BackendData } from '../media-upload.entity';
import { MediaConfig } from '../../config/media.config'; import { MediaConfig } from '../../config/media.config';
import fetch from 'node-fetch'; import fetch, { Response } from 'node-fetch';
import { URLSearchParams } from 'url'; import { URLSearchParams } from 'url';
import { MediaBackendError } from '../../errors/errors'; import { MediaBackendError } from '../../errors/errors';
type UploadResult = {
data: {
link: string;
deletehash: string;
};
};
@Injectable() @Injectable()
export class ImgurBackend implements MediaBackend { export class ImgurBackend implements MediaBackend {
private config: MediaConfig['backend']['imgur']; private config: MediaConfig['backend']['imgur'];
@ -35,44 +42,56 @@ export class ImgurBackend implements MediaBackend {
params.append('image', buffer.toString('base64')); params.append('image', buffer.toString('base64'));
params.append('type', 'base64'); params.append('type', 'base64');
try { try {
const result = await fetch('https://api.imgur.com/3/image', { const result = (await fetch('https://api.imgur.com/3/image', {
method: 'POST', method: 'POST',
body: params, body: params,
// eslint-disable-next-line @typescript-eslint/naming-convention
headers: { Authorization: `Client-ID ${this.config.clientID}` }, headers: { Authorization: `Client-ID ${this.config.clientID}` },
}) })
.then(ImgurBackend.checkStatus) .then((res) => ImgurBackend.checkStatus(res))
.then((res) => res.json()); .then((res) => res.json())) as UploadResult;
this.logger.debug(`Response: ${JSON.stringify(result)}`, 'saveFile'); this.logger.debug(`Response: ${JSON.stringify(result)}`, 'saveFile');
this.logger.log(`Uploaded ${fileName}`, 'saveFile'); this.logger.log(`Uploaded ${fileName}`, 'saveFile');
return [result.data.link, result.data.deletehash]; return [result.data.link, result.data.deletehash];
} catch (e) { } catch (e) {
this.logger.error(`error: ${e.message}`, e.stack, 'saveFile'); this.logger.error(
`error: ${(e as Error).message}`,
(e as Error).stack,
'saveFile',
);
throw new MediaBackendError(`Could not save '${fileName}' on imgur`); throw new MediaBackendError(`Could not save '${fileName}' on imgur`);
} }
} }
async deleteFile(fileName: string, backendData: BackendData): Promise<void> { async deleteFile(fileName: string, backendData: BackendData): Promise<void> {
if (backendData === null) { if (backendData === null) {
throw new Error(); throw new MediaBackendError(
`We don't have any delete tokens for '${fileName}' and therefore can't delete this image on imgur`,
);
} }
try { try {
const result = await fetch( const result = await fetch(
`https://api.imgur.com/3/image/${backendData}`, `https://api.imgur.com/3/image/${backendData}`,
{ {
method: 'POST', method: 'POST',
// eslint-disable-next-line @typescript-eslint/naming-convention
headers: { Authorization: `Client-ID ${this.config.clientID}` }, headers: { Authorization: `Client-ID ${this.config.clientID}` },
}, },
).then(ImgurBackend.checkStatus); ).then((res) => ImgurBackend.checkStatus(res));
this.logger.debug(`Response: ${result}`, 'saveFile'); this.logger.debug(`Response: ${result.toString()}`, 'saveFile');
this.logger.log(`Deleted ${fileName}`, 'deleteFile'); this.logger.log(`Deleted ${fileName}`, 'deleteFile');
return; return;
} catch (e) { } catch (e) {
this.logger.error(`error: ${e.message}`, e.stack, 'deleteFile'); this.logger.error(
`error: ${(e as Error).message}`,
(e as Error).stack,
'deleteFile',
);
throw new MediaBackendError(`Could not delete '${fileName}' on imgur`); throw new MediaBackendError(`Could not delete '${fileName}' on imgur`);
} }
} }
private static checkStatus(res) { private static checkStatus(res: Response): Response {
if (res.ok) { if (res.ok) {
// res.status >= 200 && res.status < 300 // res.status >= 200 && res.status < 300
return res; return res;