Browse Source

URL upload!

pull/2/head
asiekierka 6 years ago
parent
commit
c5a5a64789
5 changed files with 42 additions and 21 deletions
  1. 17
    12
      app.js
  2. 1
    1
      config.json
  3. 1
    2
      package.json
  4. 1
    1
      templates/upload.html
  5. 22
    5
      tempurl.js

+ 17
- 12
app.js View File

@@ -125,25 +125,30 @@ function fileExt(name) {
125 125
   var ext = path.extname(name).split(".");
126 126
   return ext[ext.length-1];
127 127
 }
128
+function finishUpload(res,fn,thfn,path,metadata, next) {
129
+  var ext = fileExt(fn);
130
+  fs.readFile(path, function(err, data) {
131
+    if(err) throw err;
132
+    addImage(data,ext,metadata,function(){ res.send("OK"); if(_.isFunction(next)) next(); },thfn,metadata.gravity);
133
+  });
134
+}
128 135
 imageHandler.express(express,app);
129 136
 app.post("/upload/post", express.bodyParser());
130 137
 app.post("/upload/post", restrict, function(req,res) {
131
-  var fn, thfn, path, ext;
132
-  if(!req.files || !req.files.image) {
133
-    if(!req.body.url) { res.send(500,"No file specified!"); return; }
134
-    ext = fileExt(req.body.url);
135
-    //tempurl.download(req.body.url, function(err, file, callback){
136
-    //});
137
-  } else { fn = req.files.image.name; path = req.files.image.path; ext = fileExt(fn); }
138
+  var thumbnailFilename;
138 139
   if(!req.body.uploader || !req.body.author)
139 140
     { res.send(500,"Missing metadata!"); return; }
140 141
   var metadata = req.body;
141 142
   metadata.tags = tagArray(req.body.tags_string);
142
-  if(req.files && req.files.thumbnail) thfn = req.files.thumbnail.path;
143
-  fs.readFile(path, function(err, data) {
144
-    if(err) throw err;
145
-    addImage(data,ext,req.body,function(){ res.send("OK"); },thfn,req.body.gravity);
146
-  });
143
+  if(req.files && req.files.thumbnail) thumbnailFilename = req.files.thumbnail.path;
144
+  if(!req.files || !req.files.image) {
145
+    if(!req.body.url) { res.send(500,"No file specified!"); return; }
146
+    ext = fileExt(req.body.url);
147
+    tempurl.download(req.body.url, function(err, tmpfile, callback){
148
+      if(err) { res.send(500,"Download error!"); return; }
149
+      finishUpload(res, "file."+fileExt(tmpfile), thumbnailFilename, tmpfile, metadata, callback);
150
+    });
151
+  } else { finishUpload(res, req.files.image.name, thumbnailFilename, req.files.image.path, metadata); }
147 152
 });
148 153
 
149 154
 function handleQuery(entry,images,allImages) {

+ 1
- 1
config.json View File

@@ -1,2 +1,2 @@
1
-{
1
+{ "salt": "Sihuioadsfuhduisaadeiohqde", "port": 8000
2 2
 }

+ 1
- 2
package.json View File

@@ -13,7 +13,6 @@
13 13
     "async": ">=0.2.5",
14 14
     "underscore.string": "2.x",
15 15
     "optimist": "0.3.x",
16
-    "progress-bar": ">=0.1.1",
17
-    "temporary": ">=0.0.5"
16
+    "progress-bar": ">=0.1.1"
18 17
   }
19 18
 }

+ 1
- 1
templates/upload.html View File

@@ -22,7 +22,7 @@
22 22
 <label>Tags [comma separated]</label>
23 23
 <input type="text" name="tags_string" id="input-tags"><br>
24 24
 File: <input type="file" name="image" id="input-image">
25
-<!--<br>or URL: <input type="text" name="url" id="input-url" style="width: 180px;">-->
25
+<br>or URL: <input type="text" name="url" id="input-url" style="width: 180px;">
26 26
 <button type="submit" class="btn" id="button-add">Submit</button>
27 27
 <br><br>
28 28
 <label>Custom thumbnail (600x600) [optional]</label>

+ 22
- 5
tempurl.js View File

@@ -1,16 +1,33 @@
1 1
 var http = require('http')
2 2
   , fs = require('fs')
3 3
   , _ = require('underscore')
4
-  , tmpfile = require('temporary/file');
4
+  , temp = require('temp')
5
+  , path = require('path')
6
+  , os = require('os');
7
+
8
+function fileExt(name) {
9
+  var ext = path.extname(name).split(".");
10
+  return ext[ext.length-1];
11
+}
5 12
 
6 13
 exports.download = function(url, callback) {
7
-  var file = new tmpfile;
14
+  var now = new Date();
15
+  var tmpfn = path.join(os.tmpDir(), [
16
+    "tmp-", now.getTime(), "-", Math.floor(Math.random()*1000000),
17
+    ".", fileExt(url)
18
+  ].join(""));
19
+  console.log("Downloading " + url + " to " + tmpfn);
20
+  var stream = fs.createWriteStream(tmpfn);
8 21
   var req = http.get(url, function(response) {
9
-    response.pipe(file);
22
+    response.on("data", function(chunk){stream.write(chunk);});
10 23
     response.on("end", function() {
11
-      callback(null,file.path,function(){ // Cleanup function
12
-        file.unlinkSync();
24
+      stream.on("close", function() {
25
+        callback(null,tmpfn, function() {
26
+          fs.unlinkSync(tmpfn);
27
+        });
13 28
       });
29
+      stream.end();
30
+      stream.destroy();
14 31
     });
15 32
   });
16 33
 }

Loading…
Cancel
Save