Guest User

upload

a guest
Feb 8th, 2019
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http=require("http"),fs=require("fs");
  2. const fpath="./store",fdir=fpath+"/dir.json";
  3. http.createServer(function(req,res) {
  4. console.log(req.method,req.url,"Content-Type:"+req.headers["content-type"]);
  5. if(req.method=="GET") {
  6.  if(req.url.match(/\/[^\?].+/)) { //GET /.. (->regexr.com)
  7.  if(!fs.existsSync(fpath+req.url)) {res.writeHead(308,{"Location":"/"}); res.end(); return;}
  8.  var im=fs.readFileSync(fpath+req.url);
  9.  var suf=(fpath+req.url).match(/[^\.\/]+$/)[0],ctype="application/octet-stream";
  10.  switch(suf) {
  11.  case "jpg": ctype="image/jpeg"; break;
  12.  case "png": ctype="image/png"; break;
  13.  case "gif": ctype="image/gif"; break;
  14.  case "mp4": ctype="video/mp4"; break;
  15.  case "webm": ctype="video/webm"; break;
  16.  }
  17.  //console.log("read "+req.url);
  18.  res.writeHead(200,{"Content-Type":ctype,"Access-Control-Allow-Origin":"*"});
  19.  res.end(im,"binary");
  20.  } else if(req.url.match(/\/\?c=.+/)) { //GET /?c=.. (Kommentare zu Bild)
  21.  var df=JSON.parse(fs.readFileSync(fdir))[req.url.slice(4)];
  22.  if(!(df&&df.comments)) {res.writeHead(204,{"Content-Type":"text/plain"}); res.end(""); return;}
  23.  var c=df["comments"];
  24.  //console.log("comments "+JSON.stringify(c));
  25.  var t="";
  26.  for(var a=0; a<c.length; a++) t+=""+c[a]+"\n";
  27.  res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"*"});
  28.  res.end(t);
  29.  } else if(req.url.match(/\/\?d=.+/)) { //GET /?d=.. (Bild löschen)
  30.  if(!fs.existsSync(fpath+"/"+req.url.slice(4))) {res.writeHead(304); res.end(); return;}
  31.  fs.unlinkSync(fpath+"/"+req.url.slice(4));
  32.  var dir=JSON.parse(fs.readFileSync(fdir));
  33.  delete dir[req.url.slice(4)];
  34.  fs.writeFileSync(fdir,JSON.stringify(dir));
  35.  //console.log("deleted ",fpath+"/"+req.url.slice(4));
  36.  res.writeHead(200);
  37.  res.end();
  38.  }
  39.  else { //GET /
  40.  var ht=fs.readFileSync("./m_upload.html");
  41.  var dir=JSON.parse(fs.readFileSync(fdir));
  42.     ht+="<div id='images'>";
  43.  for(var a in dir) {
  44.   var af=dir[a]["file"];
  45.   ht+="<a href='"+af+"' target='view'><img src='"+af+"' alt='["+"Video"/*af*/+"]'></img></a>"; }
  46.     ht+="</div>";
  47.  res.writeHead(200,{"Content-Type":"text/html","Access-Control-Allow-Origin":"*"});
  48.  res.write(ht);
  49.  res.end();
  50.  }
  51. } else if(req.method=="POST") {
  52.  if(req.url!="/upload") { res.writeHead(403); res.end(); }
  53.  var ct=req.headers["content-type"];
  54.  if(!(ct&&ct.match(/(image|video)\/.+/))) {
  55.   res.writeHead(415,{"Accept":"image/*,video/*","Access-Control-Allow-Origin":"*"}); res.end(); }
  56.  var body=Buffer.from("");
  57.  req.on("data",(chunk)=>body=Buffer.concat([body,Buffer.from(chunk)])); //body<-body+chunk
  58.  req.on("end",function() {
  59.  console.log(body.length,body);
  60.  var suf="bin"; //application/octet-stream (default)
  61.  switch(req.headers["content-type"]) {
  62.  case "image/jpeg": suf="jpg"; break;
  63.  case "image/png": suf="png"; break;
  64.  case "image/gif": suf="gif"; break;
  65.  case "video/mp4": suf="mp4"; break;
  66.  case "video/webm": suf="webm"; break;
  67.  }
  68.  fname=Date.now()+"."+suf;
  69.  if(!fs.existsSync(fpath)) fs.mkdirSync(fpath);
  70.  if(!fs.existsSync(fdir)) { fs.writeFileSync(fdir,"{}"); fs.writeFileSync(fpath+"favicon.ico",""); }
  71.  var dir=JSON.parse(fs.readFileSync(fdir));
  72.  dir[fname]={"file":fname,"comments":[]};
  73.  fs.writeFileSync(fdir,JSON.stringify(dir));
  74.  fs.writeFileSync(fpath+"/"+fname,body);
  75.  console.log("wrote /"+fname);
  76.  res.writeHead(201,{"Access-Control-Allow-Origin":"*"});
  77.  res.end();
  78.  });
  79. } else if(req.method=="DELETE") {
  80.  //redir. to /?d=..
  81. } else if(req.method=="PUT") {
  82.  var dir=JSON.parse(fs.readFileSync(fdir));
  83.  var df=dir[req.url.slice(1)];
  84.  if(!(df&&df.comments&&req.headers["content-type"]=="text/plain"))
  85.     {res.writeHead(304); res.end(); return;}
  86.  var com="";
  87.  req.on("data",(x)=>com+=x);
  88.  req.on("end",()=>{
  89.  df["comments"].push(com);
  90.  fs.writeFileSync(fdir,JSON.stringify(dir));
  91.  res.writeHead(201,{"Access-Control-Allow-Origin":"*"});
  92.  res.end();
  93.  }); //end of req.on('end' )
  94. } else if(req.method=="OPTIONS") {
  95.  res.writeHead(200,{"Access-Control-Allow-Methods":"GET,POST,PUT,DELETE,OPTIONS",
  96. "Access-Control-Allow-Headers":"Content-Type","Access-Control-Allow-Origin":"*"});
  97. res.end();
  98. }
  99. }).listen(44003);
RAW Paste Data