Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,21 @@ export class IntegrationService {
timezone?: number,
customInstanceDetails?: string
) {
const uploadedPicture = picture
? picture?.indexOf('imagedelivery.net') > -1
? picture
: await this.storage.uploadSimple(picture)
: undefined;
let uploadedPicture: string | undefined;
try {
uploadedPicture = picture
? picture?.indexOf('imagedelivery.net') > -1
? picture
: await this.storage.uploadSimple(picture, {
headers: {
Authorization: `Bearer ${token}`,
},
})
: undefined;
} catch (e: any) {
console.warn(`Failed to upload profile picture: ${e.message}`);
uploadedPicture = undefined;
}

return this._integrationRepository.createOrUpdateIntegration(
additionalSettings,
Expand Down
12 changes: 9 additions & 3 deletions libraries/nestjs-libraries/src/upload/cloudflare.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import mime from 'mime-types';
// @ts-ignore
import { getExtension } from 'mime';
import { IUploadProvider } from './upload.interface';
import { IUploadProvider, UploadSimpleOptions } from './upload.interface';
import axios from 'axios';

class CloudflareStorage implements IUploadProvider {
Expand Down Expand Up @@ -57,8 +57,14 @@ class CloudflareStorage implements IUploadProvider {
);
}

async uploadSimple(path: string) {
const loadImage = await fetch(path);
async uploadSimple(path: string, options?: UploadSimpleOptions) {
const loadImage = await fetch(path, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Postiz/1.0)',
Accept: 'image/*',
...options?.headers,
},
});
const contentType =
loadImage?.headers?.get('content-type') ||
loadImage?.headers?.get('Content-Type');
Expand Down
13 changes: 10 additions & 3 deletions libraries/nestjs-libraries/src/upload/local.storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IUploadProvider } from './upload.interface';
import { IUploadProvider, UploadSimpleOptions } from './upload.interface';
import { mkdirSync, unlink, writeFileSync } from 'fs';
// @ts-ignore
import mime from 'mime';
Expand All @@ -8,8 +8,15 @@ import axios from 'axios';
export class LocalStorage implements IUploadProvider {
constructor(private uploadDirectory: string) {}

async uploadSimple(path: string) {
const loadImage = await axios.get(path, { responseType: 'arraybuffer' });
async uploadSimple(path: string, options?: UploadSimpleOptions) {
const loadImage = await axios.get(path, {
responseType: 'arraybuffer',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; Postiz/1.0)',
Accept: 'image/*',
...options?.headers,
},
});
const contentType =
loadImage?.headers?.['content-type'] ||
loadImage?.headers?.['Content-Type'];
Expand Down
6 changes: 5 additions & 1 deletion libraries/nestjs-libraries/src/upload/upload.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export interface UploadSimpleOptions {
headers?: Record<string, string>;
}

export interface IUploadProvider {
uploadSimple(path: string): Promise<string>;
uploadSimple(path: string, options?: UploadSimpleOptions): Promise<string>;
uploadFile(file: Express.Multer.File): Promise<any>;
removeFile(filePath: string): Promise<void>;
}
Loading