text: const express = require("express");
  const app = express();
  var MongoClient = require('mongodb').MongoClient;
  const crypto = require("crypto");
  const path = require("path");
  const mongoose = require("mongoose");
  const multer = require("multer");
  const GridFsStorage = require("multer-gridfs-storage");
  const guID=require("guid");
  // Middlewares
  app.use(express.json());
  app.set("view engine", "ejs");
  
  // DB
  const mongoURI = "mongodb://umidzxbep4xmxomcm4kh:fu4KSDGWcNVF8DVlpLJy@b68fdezb1hzazzc-mongodb.services.clever-cloud.com:27017/b68fdezb1hzazzc";
  
  // connection
  const conn = mongoose.createConnection(mongoURI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });
  
  // init gfs
  let gfs;
  conn.once("open", () => {
    // init stream
    gfs = new mongoose.mongo.GridFSBucket(conn.db, {
      bucketName: "uploads"
    });
  
  });
  
  
  // Storage
  const storage = new GridFsStorage({
    url: mongoURI,
    file: (req, file) => {
      return new Promise((resolve, reject) => {
        crypto.randomBytes(16, (err, buf) => {
          if (err) {
            return reject(err);
          }
          const filename = buf.toString("hex") + path.extname(file.originalname);
          const fileInfo = {
            filename: filename,
            bucketName: "uploads"
          };
          resolve(fileInfo);
        });
      });
    }
  });
  
  
  
  const upload = multer({
    storage
  });
  
  
  
  
  var generatedURL=null;
  // get / page
  app.get("/", (req, res) => {
    if(!gfs) {
      console.log("some error occured, check connection to db");
      res.send("some error occured, check connection to db");
      process.exit(0);
    }
    
    gfs.find().toArray((err, files) => {
      // check if files
      if (!files || files.length === 0) {
        return res.render("index", {
          files: false,
          generatedURL: generatedURL
        });
      } else {
        const f = files
          .map(file => {
            if (
              file.contentType === "image/png" ||
              file.contentType === "image/jpeg"
            ) {
              file.isImage = true;
            } else {
              file.isImage = false;
            }
            return file;
          })
          .sort((a, b) => {
            return (
              new Date(b["uploadDate"]).getTime() -
              new Date(a["uploadDate"]).getTime()
            );
          });
  
        return res.render("index", {
          files: f,
          generatedURL: generatedURL
        });
      }
  
      // return res.json(files);
    });
  });
  
  app.post("/upload", upload.single("file"), (req, res) => {
    // res.json({file : req.file})
    res.redirect("/");
  });
  
  
  app.get("/files", (req, res) => {
    gfs.find().toArray((err, files) => {
      // check if files
      if (!files || files.length === 0) {
        return res.status(404).json({
          err: "no files exist"
        });
      }
  
      return res.json(files);
    });
  });
  
  app.get("/files/:filename", (req, res) => {
    gfs.find(
      {
        filename: req.params.filename
      },
      (err, file) => {
        if (!file) {
          return res.status(404).json({
            err: "no files exist"
          });
        }
  
        return res.json(file);
      }
    );
  });
  
  app.get("/image/:filename", (req, res) => {
    // console.log('id', req.params.id)
    const file = gfs
      .find({
        filename: req.params.filename
      })
      .toArray((err, files) => {
        if (!files || files.length === 0) {
          return res.status(404).json({
            err: "no files exist"
          });
        }
        gfs.openDownloadStreamByName(req.params.filename).pipe(res);
      });
  });
  
  // files/del/:id
  // Delete chunks from the db
  app.post("/files/del/:id", (req, res) => {
    gfs.delete(new mongoose.Types.ObjectId(req.params.id), (err, data) => {
      if (err) return res.status(404).json({ err: err.message });
      res.redirect("/");
    });
  });
  
  app.get("/generateURLControl/:url",(req,res)=>{
    var alinanURL=String(req.params.url);
  
    conn.collection('url').findOne({'key.value':alinanURL} ,(error, result) => {
      if(error) {
          return res.status(500).send(error);
      }else{
      res.json(result);
    }
  });
  
  });
  
  app.get("/generateURL",(req,res)=>{
    var guid = guID.create();
    genurl =  guid;
    generatedURL={
      url:genurl,
      res:'OK',
    };
  res.send(genurl);
    var myobj = { key: genurl };
  
    conn.collection('url').insertOne(myobj, function(err, res) {
      if (err) throw err;
      console.log("1 document inserted");
      
    });
    res.redirect("/");
  });
  
  app.get("/listFiles", (req, res) => {
    if(!gfs) {
      console.log("some error occured, check connection to db");
      res.send("some error occured, check connection to db");
      process.exit(0);
    }
    
    gfs.find().toArray((err, files) => {
      // check if files
      if (!files || files.length === 0) {
         res.send("null");
      } else {
        const f = files
          .map(file => {
            return file;
          })
          .sort((a, b) => {
            return (
              new Date(b["uploadDate"]).getTime() -
              new Date(a["uploadDate"]).getTime()
            );
          });
  
         res.json(f);
      }
  
      // return res.json(files);
    });
  });
  
  
  const port = process.env.PORT||27017;
  
  app.listen(port ,() => {
    console.log("server started on " + port);
  });
  