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 ,ListObjectsV2Command,DeleteObjectsCommand} from "@aws-sdk/client-s3";
import { s3Client } from "./client.js";
import express from 'express';
import sharp from 'sharp';
import axios from 'axios';
import bodyParser from 'body-parser';
var app = express();
app.use(express.json());
// Set maximum request body size (file size)
app.use(bodyParser.json({ limit: '15mb' }));
app.use(bodyParser.urlencoded({ limit: '15mb', extended: true }));
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 })
let bucketParams;
var bucket="jiffp";
app.use((req, res, next) => {
// execution time start
const start = Date.now();
res.on('finish', async() => {
var body=res.locals.data;
//total execution time
const duration = Date.now() - start;
var token="xaat-af523ddf-b600-46aa-a804-cab5d82bcadd";
const headers = {
'Authorization' : 'Bearer ' + token,
'Accept' : 'application/json',
'X-Axiom-Org-ID' : "jiff-7ijc",
};
const data = [
{
'duration_ms':duration,
'server' : "Image Proxy",
'url':req.headers.host,
'path': req.url,
'http_method': req.method,
'request_body' :JSON.stringify(req.body),
'response_code' :res.statusCode,
'response_body' : body
}
];
axios.post('https://api.axiom.co/v1/datasets/node_server/ingest', data, { headers })
.then((response) => {
// console.log(response.data);
})
.catch((error) => {
console.error(error);
});
// console.log(`[${new Date().toISOString()}] ${req.method} ${req.url} ${res.statusCode} - Response sent in ${duration}ms Response content: ${body}`);
});
next();
});
import { router } from './routes/kyc.js';
app.use(router);
import { invoice } from './routes/invoice.js';
app.use(invoice);
import { bill } from './routes/bill.js';
app.use(bill);
// app.get("/images",async(req,res,next)=>{
// try {
// res.status(200).json({"message":"hello world"});
// } catch (err) {
// res.locals.data={"error":"true","data":err.message};
// // res.status(500).json(res.locals.data);
// next({"error":"true", "message":"Something is wrong, Please try again"});
// }
// });
// Specifies a path within your Space and the file to upload.
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++) {
let ud=uid()+".webp";
let file_name=folder+"/"+ud;
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/png",
ACL: "public-read",
Preview:newFileInfo,
};
var re=await insert(bucketParams);
if (re=="error") {
resolve({"error":"true" ,"message":"Image Insertion error"});
}else{
file_location.push(ud);
if (no_of_files==file_location.length) {
resolve(file_location);
}
}
})
.catch(function(err){
// console.log("error "+err.message);
resolve({"error":"true" , "message": err.message});
});
}
});
}
function changeImage(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+"/"+files_name[i];
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/png",
ACL: "public-read",
Preview:newFileInfo,
};
var re=await insert(bucketParams);
if (re=="error") {
resolve({"error":"true" ,"message":"Image Insertion error"});
}else{
if (no_of_files==i+1) {
resolve({"message":"done"});
}
}
})
.catch(function(err){
console.log("Got Error");
resolve({"error":"true" , "message": err.message});
});
}
});
}
app.get("/", async (req, res) => {
console.log("helllo");
res.json("Hello world");
});
// run();
// insert images
app.post("/images",upload.array('images'), async (req, res, next) => {
if (req.files.length===0) {
res.locals.data={"error":"true","message":"No Image is selected"};
res.status(200).json(res.locals.data);
}else if (!req.body.vendor) {
res.locals.data={"error":"true","message":"No Vendor is selected"};
res.status(200).json(res.locals.data);
}else{
let no_of_files=req.files.length;
var folder=req.body.vendor;
var files=req.files;
uploadNewImage(no_of_files,folder,files).then(function(file_location){
console.log(file_location);
if (file_location.hasOwnProperty('error')) {
res.locals.data=file_location;
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
}else{
res.status(200).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=[];
// }
if (!req.body.images) {
res.locals.data={"error":"true","message":"No Image is selected"};
res.status(200).json(res.locals.data);
}else if (!req.body.vendor) {
res.locals.data={"error":"true","message":"No Vendor is selected"};
res.status(200).json(res.locals.data);
}else{
var images=req.body.images;
images=images.split(',');
for (let i = 0; i < images.length; i++) {
let file_name=req.body.vendor+"/"+images[i];
bucketParams = {
Bucket: bucket,
Key: file_name,
};
await delete_image(bucketParams);
}
res.status(200).json({"message":"deleted successfully"});
}
});
// change images
app.put("/images",upload.array('images'), async (req, res, next) => {
if (req.files.length===0) {
res.locals.data={"error":"true","message":"No Image is selected"};
res.status(200).json(res.locals.data);
}else if (!req.body.files_name) {
res.locals.data={"error":"true","message":"No Previous Image is selected"};
res.status(200).json(res.locals.data);
}else if (!req.body.vendor) {
res.locals.data={"error":"true","message":"No Vendor is selected"};
res.status(200).json(res.locals.data);
}else{
let no_of_files=req.files.length;
var files_name=req.body.files_name;
files_name=files_name.split(",");
var files=req.files;
var folder=req.body.vendor;
console.log(files_name.length+"!=="+no_of_files);
if (files_name.length!==no_of_files) {
res.locals.data={"error":"true","message":"No of files and file name are not equal"};
res.status(200).json(res.locals.data);
}else{
changeImage(no_of_files,folder,files,files_name).then(function(res_data){
if (res_data.hasOwnProperty('error')) {
res.locals.data=res_data;
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
}else{
res.status(200).send({"message":"Change Succesfully"});
}
// console.log(file_location);
});
}
}
});
// all in one
app.put("/change_images",upload.array('images'), async (req, res, next) => {
let no_of_files=req.files.length;
console.log(no_of_files);
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) {
changeImage(no_of_files,folder,req.files,files_name).then(function(res_data){
if (res_data.hasOwnProperty('error')) {
res.locals.data=res_data;
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
}else{
res.status(200).json({"message":"Change Succesfully"});
}
// console.log(file_location);
});
// when no of new file is less than change or delete file name length
}else if (no_of_files<files_name.length) {
changeImage(no_of_files,folder,req.files,files_name).then(async function(res_data){
if (res_data.hasOwnProperty('error')) {
res.locals.data=res_data;
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
}else{
if (no_of_files<files_name.length) {
for (let j = no_of_files; j < files_name.length; j++) {
let file_name=folder+"/"+files_name[j];
bucketParams = {
Bucket: bucket,
Key: file_name,
};
await delete_image(bucketParams);
file_location.push(files_name[j]);
if (j+1==files_name.length) {
res.status(200).json({"data":file_location});
}
}
}
}
});
// when no of new file is greater than change or deleted 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);
let ud=uid()+".webp";
let file_name=folder+"/"+ud;
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/png",
ACL: "public-read",
Preview: newFileInfo,
};
var re=await insert(bucketParams);
if (re=="error") {
res.locals.data={"error":"true" ,"message":"Image Insertion error"};
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
}
})
.catch(function(err){
console.log("Got Error");
res.locals.data={"error":"true","message":err.message};
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
});
file_location.push(ud);
}else{
let file_name=folder+"/"+files_name[i];
sharp(req.files[i].buffer).resize({ width:1920}).toBuffer()
.then( async function(newFileInfo){
bucketParams = {
Bucket: bucket,
Key: file_name,
Body: newFileInfo,
ContentType: "image/png",
ACL: "public-read",
Preview: newFileInfo,
};
var re=await insert(bucketParams);
if (re=="error") {
res.locals.data={"error":"true" ,"message":"Image Insertion error"};
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
}
})
.catch(function(err){
res.locals.data={"error":"true","message":err.message};
res.status(500).json({"error":"true", "message":"Something is wrong, Please try again"});
});
}
if (c==no_of_files) {
res.status(200).json({"data":file_location});
}
}
}else{
res.send({"error":"something is wrong "});
}
});
// 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.message);
return "error";
}
};
export const delete_image = async (bucketParams) => {
try {
const data = await s3Client.deleteObject(bucketParams);
return data;
} catch (err) {
console.log("Error", err.message);
return err.message;
}
};
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0102 ]-- |