123 lines
4.7 KiB
JavaScript
123 lines
4.7 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.minioClient = void 0;
|
|
exports.initializeMinio = initializeMinio;
|
|
exports.uploadImageFromUrl = uploadImageFromUrl;
|
|
exports.getImageUrl = getImageUrl;
|
|
exports.deleteImage = deleteImage;
|
|
const Minio = __importStar(require("minio"));
|
|
const axios_1 = __importDefault(require("axios"));
|
|
const uuid_1 = require("uuid");
|
|
const minioClient = new Minio.Client({
|
|
endPoint: process.env.MINIO_ENDPOINT || 'minio',
|
|
port: parseInt(process.env.MINIO_PORT || '9000'),
|
|
useSSL: process.env.MINIO_USE_SSL === 'true',
|
|
accessKey: process.env.MINIO_ACCESS_KEY || 'minioadmin',
|
|
secretKey: process.env.MINIO_SECRET_KEY || 'minioadmin',
|
|
});
|
|
exports.minioClient = minioClient;
|
|
const BUCKET_NAME = process.env.MINIO_BUCKET || 'dutchie';
|
|
async function initializeMinio() {
|
|
try {
|
|
// Check if bucket exists
|
|
const exists = await minioClient.bucketExists(BUCKET_NAME);
|
|
if (!exists) {
|
|
// Create bucket
|
|
await minioClient.makeBucket(BUCKET_NAME, 'us-east-1');
|
|
console.log(`✅ Minio bucket created: ${BUCKET_NAME}`);
|
|
// Set public read policy
|
|
const policy = {
|
|
Version: '2012-10-17',
|
|
Statement: [
|
|
{
|
|
Effect: 'Allow',
|
|
Principal: { AWS: ['*'] },
|
|
Action: ['s3:GetObject'],
|
|
Resource: [`arn:aws:s3:::${BUCKET_NAME}/*`],
|
|
},
|
|
],
|
|
};
|
|
await minioClient.setBucketPolicy(BUCKET_NAME, JSON.stringify(policy));
|
|
console.log(`✅ Bucket policy set to public read`);
|
|
}
|
|
else {
|
|
console.log(`✅ Minio bucket already exists: ${BUCKET_NAME}`);
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error('❌ Minio initialization error:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
async function uploadImageFromUrl(imageUrl, productId) {
|
|
try {
|
|
// Download image
|
|
const response = await axios_1.default.get(imageUrl, { responseType: 'arraybuffer' });
|
|
const buffer = Buffer.from(response.data);
|
|
// Generate unique filename
|
|
const ext = imageUrl.split('.').pop()?.split('?')[0] || 'jpg';
|
|
const filename = `products/${productId}-${(0, uuid_1.v4)()}.${ext}`;
|
|
// Get content type
|
|
const contentType = response.headers['content-type'] || 'image/jpeg';
|
|
// Upload to Minio
|
|
await minioClient.putObject(BUCKET_NAME, filename, buffer, buffer.length, {
|
|
'Content-Type': contentType,
|
|
});
|
|
// Return the path (URL will be constructed when serving)
|
|
return filename;
|
|
}
|
|
catch (error) {
|
|
console.error('Error uploading image:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
function getImageUrl(path) {
|
|
// Use localhost:9020 for browser access since Minio is exposed on host port 9020
|
|
const endpoint = process.env.MINIO_PUBLIC_ENDPOINT || 'http://localhost:9020';
|
|
return `${endpoint}/${BUCKET_NAME}/${path}`;
|
|
}
|
|
async function deleteImage(path) {
|
|
try {
|
|
await minioClient.removeObject(BUCKET_NAME, path);
|
|
}
|
|
catch (error) {
|
|
console.error('Error deleting image:', error);
|
|
}
|
|
}
|