135 lines
3.4 KiB
JavaScript
135 lines
3.4 KiB
JavaScript
const catchAsync = require('../../globalErrorHandler/catchAsync')
|
|
const fileService = require('./fileService')
|
|
|
|
exports.bucketSingleUpload = catchAsync(async (req, res, next) => {
|
|
if (!req.file) {
|
|
return res.status(400).json({
|
|
status: 'fail',
|
|
message: 'No file uploaded'
|
|
})
|
|
}
|
|
const fileData = await fileService.bucketSingleUpload(req.file, req.params.bucketName)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: fileData,
|
|
bucketName: req.params.bucketName,
|
|
})
|
|
})
|
|
|
|
exports.userUploadOrderDocument = catchAsync(async (req, res, next) => {
|
|
const files = await fileService.userUploadOrderDocument(req.files)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
results: files.length,
|
|
data: files,
|
|
})
|
|
})
|
|
|
|
exports.bucketMultiUpload = catchAsync(async (req, res, next) => {
|
|
const files = await fileService.bucketMultiUpload(req.files, req.params.bucketName)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
results: files.length,
|
|
data: files,
|
|
bucketName: req.params.bucketName,
|
|
})
|
|
})
|
|
|
|
exports.bucketGetFiles = catchAsync(async (req, res, next) => {
|
|
const files = await fileService.bucketGetFiles()
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
results: files.length,
|
|
data: files,
|
|
})
|
|
})
|
|
|
|
exports.getSignedUrl = catchAsync(async (req, res, next) => {
|
|
const url = await fileService.getSignedUrl(req.body.fileName)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: url,
|
|
})
|
|
})
|
|
|
|
exports.getImage = catchAsync(async (req, res, next) => {
|
|
const { bucketName, fileName } = req.params;
|
|
|
|
const imageData = await fileService.getImage(bucketName, fileName);
|
|
|
|
|
|
if (!imageData) {
|
|
return res.status(404).json({
|
|
status: 'fail',
|
|
message: 'Image not found'
|
|
});
|
|
}
|
|
|
|
// Set appropriate headers for the image response
|
|
res.set('Content-Type', imageData.contentType);
|
|
|
|
res.send(imageData.buffer);
|
|
});
|
|
|
|
exports.bucketDeleteFile = catchAsync(async (req, res, next) => {
|
|
const rawTarget = typeof req.body === 'object' && req.body !== null
|
|
? (req.body.fileName || req.body.url || req.body.data || '')
|
|
: (req.body || '');
|
|
const { fileName } = await fileService.bucketDeleteFile(rawTarget, req.params.bucketName)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: fileName,
|
|
})
|
|
})
|
|
|
|
exports.getAllFiles = catchAsync(async (req, res, next) => {
|
|
const files = await fileService.getAllFiles()
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
results: files.length,
|
|
data: files,
|
|
})
|
|
})
|
|
|
|
exports.getFile = catchAsync(async (req, res, next) => {
|
|
const file = await fileService.getFile(req.body.id)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: file,
|
|
})
|
|
})
|
|
|
|
exports.createFile = catchAsync(async (req, res, next) => {
|
|
const file = await fileService.createFile(req.body)
|
|
|
|
res.status(201).json({
|
|
status: 'success',
|
|
data: file,
|
|
})
|
|
})
|
|
|
|
exports.updateFile = catchAsync(async (req, res, next) => {
|
|
const file = await fileService.updateFile(req.body)
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: file,
|
|
})
|
|
})
|
|
|
|
exports.deleteFile = catchAsync(async (req, res, next) => {
|
|
await fileService.deleteFile(req.body.id)
|
|
|
|
res.status(204).json({
|
|
status: 'success',
|
|
data: null,
|
|
})
|
|
})
|