Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:replace firebase with minio #964

Merged
merged 4 commits into from
Mar 29, 2024
Merged
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
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
Loading