Skip to content

Commit

Permalink
fix:replace firebase with minio (#964)
Browse files Browse the repository at this point in the history
* fix

* Update upload.ts

* Update config.ts

* format fix
  • Loading branch information
RiXelanya authored Mar 29, 2024
1 parent 1a361e2 commit 8d58fac
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .env-template
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ TWITTER_API_KEY=

# COIN MARKET CAP
COIN_MARKET_CAP_API_KEY=

MINIO_ACCESS_KEY=
MINIO_SECRET_KEY=
MINIO_ENDPOINT=
MINIO_BUCKET_NAME=
MINIO_PORT=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"loopback-connector-mongodb": "^6.1.0",
"loopback-connector-rest": "^4.0.2",
"loopback4-ratelimiter": "^4.1.2",
"minio": "^7.1.3",
"multer": "^1.4.5-lts.1",
"near-api-js": "^1.1.0",
"near-seed-phrase": "^0.2.0",
Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ export const config = {
TWITTER_API_KEY: process.env.TWITTER_API_KEY ?? '',

COIN_MARKET_CAP_API_KEY: process.env.COIN_MARKET_CAP_API_KEY ?? '',

MINIO_ENDPOINT: process.env.MINIO_ENDPOINT ?? '103.28.14.18',
MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY ?? '',
MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY ?? '',
MINIO_PORT: process.env.MINIO_PORT ? parseInt(process.env.MINIO_PORT) : 9000,
MINIO_BUCKET_NAME: process.env.MINIO_BUCKET_NAME ?? '',
};
48 changes: 36 additions & 12 deletions src/utils/upload.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import {AnyObject} from '@loopback/repository';
import * as firebaseAdmin from 'firebase-admin';
import fs, {existsSync} from 'fs';
import os from 'os';
import path from 'path';
import sharp, {FormatEnum} from 'sharp';
import {v4 as uuid} from 'uuid';
import {config} from '../config';
import {Client as MinioClient} from 'minio';

const minioClient = new MinioClient({
endPoint: config.MINIO_ENDPOINT,
port: config.MINIO_PORT,
useSSL: false,
accessKey: config.MINIO_ACCESS_KEY,
secretKey: config.MINIO_SECRET_KEY,
});

export enum UploadType {
IMAGE = 'image',
Expand All @@ -19,10 +27,6 @@ export async function upload(
) {
if (!filePath) return '';

const bucket = config.FIREBASE_STORAGE_BUCKET
? firebaseAdmin.storage().bucket()
: undefined;

const tmpDir = os.tmpdir();
const baseName = path.parse(filePath).name;
const extension = path.parse(filePath).ext;
Expand Down Expand Up @@ -60,14 +64,34 @@ export async function upload(
}
}

if (bucket) {
const [imageFile] = await bucket.upload(formattedFilePath, {
resumable: false,
public: true,
destination: uploadFilePath,
});
if (minioClient) {
try {
const bucketName = config.MINIO_BUCKET_NAME;
const objectName = formattedFilePath;
await minioClient.fPutObject(bucketName, objectName, formattedFilePath);
const url = `${config.MINIO_ENDPOINT}:${config.MINIO_PORT}/${config.MINIO_BUCKET_NAME}/${objectName}`;
result = url;
} catch (error) {
console.error(error);
if (!config.DOMAIN) {
fs.unlinkSync(filePath);
fs.unlinkSync(formattedFilePath);
throw new Error('Storage not found');
}

result = imageFile.publicUrl();
const folderPath = `../../storages`;
const tmpSubFolderPath = `${folderPath}/${targetDir}`;
const tmpUpdatedFilePath = `${folderPath}/${uploadFilePath}`;
const subfolderPath = path.join(__dirname, tmpSubFolderPath);
const updatedFilePath = path.join(__dirname, tmpUpdatedFilePath);
if (!fs.existsSync(subfolderPath)) {
fs.mkdirSync(subfolderPath, {recursive: true});
}

fs.copyFileSync(formattedFilePath, updatedFilePath);

result = `https://${config.DOMAIN}/storages/${uploadFilePath}`;
}
} else {
if (!config.DOMAIN) {
fs.unlinkSync(filePath);
Expand Down
Loading

0 comments on commit 8d58fac

Please sign in to comment.