Browse Source

URL upload!

pull/2/head
asiekierka 7 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) {
var ext = path.extname(name).split(".");
return ext[ext.length-1];
}
function finishUpload(res,fn,thfn,path,metadata, next) {
var ext = fileExt(fn);
fs.readFile(path, function(err, data) {
if(err) throw err;
addImage(data,ext,metadata,function(){ res.send("OK"); if(_.isFunction(next)) next(); },thfn,metadata.gravity);
});
}
imageHandler.express(express,app);
app.post("/upload/post", express.bodyParser());
app.post("/upload/post", restrict, function(req,res) {
var fn, thfn, path, ext;
if(!req.files || !req.files.image) {
if(!req.body.url) { res.send(500,"No file specified!"); return; }
ext = fileExt(req.body.url);
//tempurl.download(req.body.url, function(err, file, callback){
//});
} else { fn = req.files.image.name; path = req.files.image.path; ext = fileExt(fn); }
var thumbnailFilename;
if(!req.body.uploader || !req.body.author)
{ res.send(500,"Missing metadata!"); return; }
var metadata = req.body;
metadata.tags = tagArray(req.body.tags_string);
if(req.files && req.files.thumbnail) thfn = req.files.thumbnail.path;
fs.readFile(path, function(err, data) {
if(err) throw err;
addImage(data,ext,req.body,function(){ res.send("OK"); },thfn,req.body.gravity);
});
if(req.files && req.files.thumbnail) thumbnailFilename = req.files.thumbnail.path;
if(!req.files || !req.files.image) {
if(!req.body.url) { res.send(500,"No file specified!"); return; }
ext = fileExt(req.body.url);
tempurl.download(req.body.url, function(err, tmpfile, callback){
if(err) { res.send(500,"Download error!"); return; }
finishUpload(res, "file."+fileExt(tmpfile), thumbnailFilename, tmpfile, metadata, callback);
});
} else { finishUpload(res, req.files.image.name, thumbnailFilename, req.files.image.path, metadata); }
});

function handleQuery(entry,images,allImages) {

+ 1
- 1
config.json View File

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

+ 1
- 2
package.json View File

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

+ 1
- 1
templates/upload.html View File

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

+ 22
- 5
tempurl.js View File

@@ -1,16 +1,33 @@
var http = require('http')
, fs = require('fs')
, _ = require('underscore')
, tmpfile = require('temporary/file');
, temp = require('temp')
, path = require('path')
, os = require('os');

function fileExt(name) {
var ext = path.extname(name).split(".");
return ext[ext.length-1];
}

exports.download = function(url, callback) {
var file = new tmpfile;
var now = new Date();
var tmpfn = path.join(os.tmpDir(), [
"tmp-", now.getTime(), "-", Math.floor(Math.random()*1000000),
".", fileExt(url)
].join(""));
console.log("Downloading " + url + " to " + tmpfn);
var stream = fs.createWriteStream(tmpfn);
var req = http.get(url, function(response) {
response.pipe(file);
response.on("data", function(chunk){stream.write(chunk);});
response.on("end", function() {
callback(null,file.path,function(){ // Cleanup function
file.unlinkSync();
stream.on("close", function() {
callback(null,tmpfn, function() {
fs.unlinkSync(tmpfn);
});
});
stream.end();
stream.destroy();
});
});
}

Loading…
Cancel
Save