Skip to content
Permalink
Browse files

Ability to load images from external servers.

  • Loading branch information...
Piotr Foltyn
Piotr Foltyn committed Jan 15, 2014
1 parent a1234ca commit 6798ac98c9002bf5ba24ca630914c55b1393feca
Showing with 81 additions and 29 deletions.
  1. +1 −0 .gitmodules
  2. +11 −5 app.js
  3. +4 −5 public/dispatch.coffee
  4. +25 −5 public/file-upload.coffee
  5. +40 −14 public/infragram.js
@@ -1,2 +1,3 @@
[submodule "public/getUserMedia.js"]
path = public/getUserMedia.js
url = https://github.com/addyosmani/getUserMedia.js.git
16 app.js
@@ -64,9 +64,9 @@ io.sockets.on('connection', function (socket) {
socket.on('upload', function (data) {
var thumbnail = false;
if ('thumbnail' in data) {
thumbnail = data['thumbnail']
thumbnail = data['thumbnail'];
}
var name = ''
var name = '';
if ('name' in data) {
name = String(data['name']).toLowerCase().replace(/[^a-z0-9]/g, '_');
n = name.lastIndexOf('_');
@@ -77,11 +77,17 @@ io.sockets.on('connection', function (socket) {
name += '_thumb.jpg';
}
}
var buffer = ''
var buffer = '';
if ('data' in data) {
buffer = data['data'];
if (data['data'].substring(0, 5) == 'data:') {
encoding = 'base64';
buffer = data['data'].split(',')[1];
}
else {
encoding = 'binary';
buffer = data['data'];
}
}
encoding = (thumbnail) ? 'base64' : 'binary';
fs.writeFile('./public/upload/' + name, buffer, encoding, function (err) {
if (err) console.log(err);
socket.emit("done", {"name": name});
@@ -20,7 +20,7 @@ log = [] # a record of previous commands run

getURLParameter = (name) ->
result = decodeURI(
(RegExp(name + "=" + "(.+?)(&|$|/)").exec(location.search) || [null, null])[1]
(RegExp(name + "=" + "(.+?)(&|$)").exec(location.search) || [null, null])[1]
)
return if result == "null" then null else result

@@ -59,10 +59,9 @@ $(document).ready(() ->

src = getURLParameter("src")
if src
setFilename(src)
src_img = new Image()
src_img.onload = () -> updateImage(this)
src_img.src = "../upload/" + src
$("#download").show()
$("#save-modal-btn").show()
loadFileFromUrl(src, updateImage)

return true
)
@@ -18,6 +18,11 @@ socket = io.connect("http://localhost:8001")
filename = ""


isUrl = (s) ->
regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s)


sendImage = (file) ->
$("#file-sel").prop('disabled', true);
upload = new FileReader()
@@ -35,11 +40,30 @@ sendThumbnail = (img) ->
canvas.width = 260
canvas.height = 195
ctx.drawImage(this, 0, 0, this.width, this.height, 0, 0, canvas.width, canvas.height)
dataUrl = canvas.toDataURL("image/jpeg").split(",")[1]
dataUrl = canvas.toDataURL("image/jpeg")
socket.emit("upload", {"name": filename, "data": dataUrl, "thumbnail": true})
e.src = img


loadFileFromUrl = (url, onLoadImage) ->
img = new Image()
img.crossOrigin = "anonymous"
img.onload = () ->
onLoadImage(this)
canvas = document.createElement("canvas")
ctx = canvas.getContext("2d")
canvas.width = this.width
canvas.height = this.height
ctx.drawImage(this, 0, 0, this.width, this.height)
dataUrl = canvas.toDataURL("image/jpeg")
name = url.substring(url.lastIndexOf("/") + 1)
socket.emit("upload", {"name": name, "data": dataUrl, "thumbnail": false})
if isUrl(url)
img.src = url
else
img.src = "../upload/" + url


handleOnChangeFile = (files, onLoadImage) ->
if files && files[0]
file = files[0]
@@ -60,7 +84,3 @@ socket.on("done", (data) ->

getFilename = () ->
return filename


setFilename = (name) ->
filename = name

Some generated files are not rendered by default. Learn more.

0 comments on commit 6798ac9

Please sign in to comment.
You can’t perform that action at this time.