Skip to content

Commit

Permalink
Merge pull request #45 from danial117/server_dev_37
Browse files Browse the repository at this point in the history
inital commit
  • Loading branch information
danial117 authored Sep 20, 2024
2 parents 330c5e2 + 669acd3 commit 6fe53af
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 34 deletions.
64 changes: 37 additions & 27 deletions controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,61 +413,71 @@ export const AdminUploadProductImagesFolder=async (req,res,next)=>{

try {
const files = req.files;
console.log(req.files)

const matchedFiles = [];
const productsDir = path.join(__dirname, '../public/products/large');

// Iterate over the uploaded files
for (let file of files) {

const product = await Product.findOne({ productImage: file.originalname });

// console.log(product)

if (product) {
const newFilename = `${Date.now()}-${file.originalname.replace(/ /g, '_').replace('.png', '_large.png')}`;

const newPath = path.join(productsDir, newFilename);

if (!fs.existsSync(productsDir)) {
fs.mkdirSync(productsDir, { recursive: true });
}



// Move the file to the new location with the new name
fs.rename(file.path, newPath, async(err) => {
if (err) {
console.error(`Failed to rename and move file: ${err.message}`);
} else {
console.log('fix')
const {outputMediumFileName,outputSmallFileName}=await compressAndSaveProductImage(newFilename)
product.productImage={
large:newFilename,
medium:outputMediumFileName,
small:outputSmallFileName
}
try {
// Synchronously rename (move) the file
fs.renameSync(file.path, newPath);

await product.save()






}
});



// Call the async function to compress and save the product image
const { outputMediumFileName, outputSmallFileName } = await compressAndSaveProductImage(newFilename);

// Update the product object with the new image paths
product.productImage = {
large: newFilename,
medium: outputMediumFileName,
small: outputSmallFileName
};

// Save the product document
await product.save();



} catch (err) {

}


matchedFiles.push({ ...file, newFilename });

} else {
console.log('delete')
// If no match, delete the file
fs.unlinkSync(file.path);
}
}

// If no files matched, send a response indicating no files were saved
if (matchedFiles.length === 0) {
return res.status(200).json({ message: 'No files matched with any product images.' });
res.status(200).json({ message: 'Product does not exist.' });
}else{
for (let file of matchedFiles){

// console.log(matchedFiles)
fs.unlinkSync(file.path);
res.status(200).json({ message: 'Product does not exist.' });
}

}
Expand All @@ -477,7 +487,7 @@ export const AdminUploadProductImagesFolder=async (req,res,next)=>{

} catch (err) {

next(new CustomError(err.message, 500));
next(new CustomError(err.message, 200));
}
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const GetProduct=async(req,res)=>{
}
}

console.log(query)

// Find orders based on the filter, sort, skip, and limit
const products = await Product.find(query).sort(sortObject).skip(skip).limit(limit);
const totalProducts = await Product.countDocuments(query);
Expand Down
455 changes: 455 additions & 0 deletions error.log

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion middlewares/fileModified.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const modifiedProductFile = async (req, res,next) => {



} catch (error) {
} catch (err) {

next(new CustomError(err.message, 500));
}
Expand Down
2 changes: 1 addition & 1 deletion middlewares/multerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const fileFilter = (req, file, cb) => {


else {
cb(new CustomError('Extension unallowed', 500));
cb(new CustomError('Extension unallowed', 415));
}
} catch (err) {

Expand Down
8 changes: 5 additions & 3 deletions middlewares/multerMultiAdminUplaod.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const storage = multer.diskStorage({
}
},
filename: function (req, file, cb) {
cb(null, `${Date.now()}-${file.originalname.replace(/ /g, '_')}`);
const decodedFilename = decodeURIComponent(file.originalname);
file.originalname=decodedFilename
cb(null, `${Date.now()}-${decodedFilename.replace(/ /g, '_')}`);
}
});

Expand All @@ -37,7 +39,7 @@ export const multiUpload = multer({
// Check File Type
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /|png|/;
const filetypes = /png/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
Expand All @@ -46,6 +48,6 @@ function checkFileType(file, cb) {
if (mimetype && extname) {
return cb(null, true);
} else {
cb(new CustomError('Png allowed only', 500));
cb(new CustomError('Png allowed only', 415));
}
}
2 changes: 1 addition & 1 deletion models/ProductModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const SchemaTypes = mongoose.Schema.Types;
function getCosts(value) {
if (value && typeof value.toString === 'function') {
// Convert Decimal128 to string and return it
console.log(value.toString())

return value.toString();
}
return value; // Return value as-is if it's undefined or null
Expand Down

0 comments on commit 6fe53af

Please sign in to comment.