Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/tests.2/
Upload File :
Current File : /home/infinitibizsol/.trash/tests.2/dates-tests.js

/* const { Op } = require("sequelize");
const moment = require("moment");

let startOfCurrentWeek = moment().startOf("isoWeek").toDate();
let endOfCurrentWeek = moment().endOf("isoWeek").toDate();

tblvehicles.findAll({
  where: {
    createdAt: {
      [Op.between]: [startOfCurrentWeek, endOfCurrentWeek],
    },
  },
});
//last-week
let startOfLastWeek = moment().subtract(1, "weeks").startOf("isoWeek").toDate();
let endOfLastWeek = moment().subtract(1, "weeks").endOf("isoWeek").toDate();

tblvehicles.findAll({
  where: {
    createdAt: {
      [Op.between]: [startOfLastWeek, endOfLastWeek],
    },
  },
});
// last4weeks
let fourWeeksAgo = moment().subtract(4, "weeks").startOf("isoWeek").toDate();

tblvehicles.findAll({
  where: {
    createdAt: {
      [Op.gte]: fourWeeksAgo,
    },
  },
});
//last12weeks
let twelveWeeksAgo = moment().subtract(12, "weeks").startOf("isoWeek").toDate();

tblvehicles.findAll({
  where: {
    createdAt: {
      [Op.gte]: twelveWeeksAgo,
    },
  },
});
 */
const express = require("express");
const multer = require("multer");
const path = require("path");
const fs = require("fs");

// Set up the Express app
const app = express();
const PORT = process.env.PORT || 3000;

// Ensure the upload directory exists
const uploadDirectory = "uploads/";
fs.existsSync(uploadDirectory) || fs.mkdirSync(uploadDirectory);

// Configure Multer's storage engine
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, uploadDirectory);
  },
  filename: function (req, file, cb) {
    // Use the file's original extension
    const ext = path.extname(file.originalname);
    // Construct a unique filename using the current timestamp
    cb(null, `${file.fieldname}-${Date.now()}${ext}`);
  },
});

// Initialize upload with the storage configuration and file filter
const upload = multer({
  storage: storage,
  fileFilter: function (req, file, cb) {
    const filetypes = /jpeg|jpg|png|gif/;
    const mimetype = filetypes.test(file.mimetype);
    const extname = filetypes.test(
      path.extname(file.originalname).toLowerCase()
    );

    if (mimetype && extname) {
      return cb(null, true);
    }

    cb(
      new Error(
        "Error: File upload only supports the following filetypes - " +
          filetypes
      )
    );
  },
});

// Define the POST route for image uploading
app.post("/upload-image", upload.single("image"), (req, res) => {
  try {
    // File has been uploaded successfully
    res.status(200).json({
      message: "Upload successful",
      file: req.file,
    });
  } catch (error) {
    res.status(500).json({
      message: "An error occurred during the file upload",
      error: error.message,
    });
  }
});

// Start the server
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists