Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/space/ drwxrwxrwx | |
| Viewing file: Select action/file-type: // Imports your configured client and any necessary S3 commands.
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "./client.js";
import express from 'express';
import sharp from 'sharp';
var app = express();
app.use(express.json());
import multer from 'multer';
import ShortUniqueId from 'short-unique-id';
//Instantiate
const uid = new ShortUniqueId({ length: 10 });
app.listen(3007, () => {
console.log("Server running on port 3007");
});
// const multer = require('multer')
const storage = multer.memoryStorage()
const upload = multer({ storage: storage })
// app.put("/images",async(req,res) => {
// console.log(req.body.vendor);
// res.send("hello world");
// });
// app.get("/images",async(req,res)=>{
// res.send("hello world");
// });
// Specifies a path within your Space and the file to upload.
let bucketParams;
var bucket="jiff";
function uploadNewImage(no_of_files,folder,files) {
var file_location=[];
return new Promise(function(resolve, reject) {
for (let i = 0; i < no_of_files; i++) {
var ud=uid();
let file_name=folder+"/images/"+ud+".jpg";
sharp(files[i].buffer).resize({ width:1920}).toBuffer()
.then( async function(newFileInfo){
console.log("Image Resized");
bucketParams = {
Bucket: bucket,
Key: file_name,
Body: newFileInfo,
ContentType: "image/jpeg",
ACL: "public-read",
Preview:newFileInfo,
};
await insert(bucketParams);
})
.catch(function(err){
console.log("Got Error");
});
file_location.push(ud);
if (no_of_files==file_location.length) {
resolve(file_location);
}
}
});
}
function chnageImage(no_of_files,folder,files,files_name) {
return new Promise(function(resolve, reject) {
for (let i = 0; i < no_of_files; i++) {
let file_name=folder+"/images/"+files_name[i]+".jpg";
sharp(files[i].buffer).resize({ width:1920}).toBuffer()
.then( async function(newFileInfo){
console.log("Image Resized");
bucketParams = {
Bucket: bucket,
Key: file_name,
Body: newFileInfo,
ContentType: "image/jpeg",
ACL: "public-read",
Preview:newFileInfo,
};
await insert(bucketParams);
})
.catch(function(err){
console.log("Got Error");
});
if (no_of_files==i+1) {
resolve();
}
}
});
}
// run();
// insert images
app.post("/images",upload.array('images'), async (req, res, next) => {
// console.log(req.files);
let no_of_files=req.files.length;
var folder=req.body.vendor;
var files=req.files;
// console.log(abc);
uploadNewImage(no_of_files,folder,files).then(function(file_location){
console.log(file_location);
res.send({"data":file_location});
});
});
// delete images
app.post("/delete_images", async (req, res, next) => {
// var abc=req.body.vendor;
var images=req.body.images;
console.log(images);
if (images!=='') {
images=images.split(',');
}else{
images=[];
}
for (let i = 0; i < images.length; i++) {
let file_name=req.body.vendor+"/images/"+images[i]+".jpg";
bucketParams = {
Bucket: bucket,
Key: file_name,
};
await delete_image(bucketParams);
}
res.send({"message":"deleted successfully"});
});
// change images
app.put("/images",upload.array('images'), async (req, res, next) => {
let no_of_files=req.files.length;
var files_name=req.body.files_name;
var files=req.files;
var folder=req.body.vendor;
console.log(files_name);
if (files_name!=='') {
files_name=files_name.split(',');
}else{
// files_name=[];
res.send({"Error":"No of files and file name are not equal"});
}
chnageImage(no_of_files,folder,files,files_name).then(function(){
// console.log(file_location);
res.send({"message":"Change Succesfully"});
});
});
// all in one
app.put("/change_images",upload.array('images'), async (req, res, next) => {
let no_of_files=req.files.length;
var files_name=req.body.files_name;
if (files_name!=='') {
files_name=files_name.split(',');
}else{
files_name=[];
}
var file_location=[];
var folder=req.body.vendor;
// when no of new file is equal change or delete file name length
if (no_of_files===files_name.length) {
chnageImage(no_of_files,folder,req.files,files_name).then(function(){
// console.log(file_location);
res.send({"message":"Change Succesfully"});
});
// when no of new file is less than change or delete file name length
}else if (no_of_files<files_name.length) {
chnageImage(no_of_files,folder,req.files,files_name).then(async function(){
if (no_of_files<files_name.length) {
for (let j = no_of_files; j < files_name.length; j++) {
let file_name=folder+"/images/"+files_name[j]+".jpg";
bucketParams = {
Bucket: bucket,
Key: file_name,
};
await delete_image(bucketParams);
file_location.push(files_name[j]);
if (j+1==files_name.length) {
res.send({"data":file_location});
}
}
}
});
// when no of new file is greater than change or delete file name length
}else if (no_of_files>files_name.length) {
var c=0;
for (let i = 0; i < no_of_files; i++) {
c=c+1;
// console.log(files_name.length);
// console.log(c);
if (c>files_name.length) {
console.log("new "+c);
var ud=uid();
let file_name=folder+"/images/"+ud+".jpg";
sharp(req.files[i].buffer).resize({ width:1920}).toBuffer()
.then( async function(newFileInfo){
console.log("Image Resized");
bucketParams = {
Bucket: bucket,
Key: file_name,
Body: newFileInfo,
ContentType: "image/jpeg",
ACL: "public-read",
Preview: newFileInfo,
};
await insert(bucketParams);
})
.catch(function(err){
console.log("Got Error");
});
file_location.push(ud);
}else{
let file_name=folder+"/images/"+files_name[i]+".jpg";
sharp(req.files[i].buffer).resize({ width:1920}).toBuffer()
.then( async function(newFileInfo){
bucketParams = {
Bucket: bucket,
Key: file_name,
Body: newFileInfo,
ContentType: "image/jpeg",
ACL: "public-read",
Preview: newFileInfo,
};
await insert(bucketParams);
})
.catch(function(err){
res.send({"error":"Something is Wrong"});
});
}
}
res.send({"data":file_location});
}else{
res.send({"error":"something is wrong "});
}
// for (let i = 0; i < no_of_files; i++) {
// let file_name=req.body.vendor+"/images/"+files_name[i]+".jpg";
// bucketParams = {
// Bucket: "jiff",
// Key: file_name,
// Body: req.files[i].buffer,
// ContentType: "image/jpeg",
// ACL: "public-read",
// Preview:req.files[i].buffer,
// };
// await insert(bucketParams);
// }
// res.send("Change Succesfully");
});
// Uploads the specified file to the chosen path.
export const insert = async (bucketParams) => {
try {
const data = await s3Client.send(new PutObjectCommand(bucketParams));
console.log(
"Successfully uploaded object: " +
bucketParams.Bucket +
"/" +
bucketParams.Key
);
return data;
} catch (err) {
console.log("Error", err);
}
};
export const delete_image = async (bucketParams) => {
try {
const data = await s3Client.deleteObject(bucketParams);
return data;
} catch (err) {
console.log("Error", err);
}
};
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.005 ]-- |