Skip to content

Commit

Permalink
Uploading done. (Python is awesome.)
Browse files Browse the repository at this point in the history
  • Loading branch information
1j01 committed Jul 11, 2013
1 parent 6dec7be commit a94a7ba
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 30 deletions.
19 changes: 12 additions & 7 deletions aa.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

/// Handles loading and storing of images for live editing. Also uploading.
// #persistant

ArtAssets = function(mainctx, allloaded, update){
var aa=this;
aa.progress=0;
aa.progress_l=0;
aa.images={};
aa.dir="content/art/";
// splitting tilesets into tiles is unnecessary. drawImage supports clipping.
Expand All @@ -19,14 +20,15 @@ ArtAssets = function(mainctx, allloaded, update){
}
};

//should reuse ajax code
aa.uploadImage=function(name){
var form=new FormData();
var img=aa.getImage(name);
if(!aa.src.match(/^data:/)){
if(!img.src.match(/^data:/)){
console.log("uploadImage: image not modified.");

return;
}
form.append("file",aa.src,aa.dir+name);
var form=new FormData();
form.append("imagedata",img.src,aa.dir+name);
form.append("fname",aa.dir+name);
var x=new XMLHttpRequest();
x.onreadystatechange=function(){
Expand All @@ -37,16 +39,18 @@ ArtAssets = function(mainctx, allloaded, update){
};
aa.newImage=function(name, img){
var form=new FormData();
var fname=aa.dir+name;
if(name.indexOf(".png")==-1)name+=".png";
var fname=aa.dir;
aa.images[fname]=img;
form.append("file","img",fname);
form.append("imagedata",img.src,fname);
form.append("fname",fname);
var x=new XMLHttpRequest();
x.onreadystatechange=function(){
console.log(x.responseText);
};
x.open("POST","upload.py");
x.send(form);
return fname||name;//idk||update_later
};
//.replace(/content\/art\//,"")

Expand Down Expand Up @@ -84,4 +88,5 @@ ArtAssets = function(mainctx, allloaded, update){
//x.open("POST","upload.py");
x.open("GET","listcontent.py");
x.send();

};
1 change: 1 addition & 0 deletions am.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// #disposable
ArtManager = function(aa, gui){
var am=this;
var timeout=0;
am.show = function(){
am.m = this.m || gui.M();
am.m.title("Art Manager").position("center left-ish?");
Expand Down
Empty file modified content/art/README.md
100644 → 100755
Empty file.
Empty file modified content/art/dawn_of_the_gods_0.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified content/art/platformertiles.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified content/art/platshrooms.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified content/blocks/README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion listcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
print "Content-Type: text/plain\n\n"
for root, dirs, files in os.walk("content"):
for file in files:
if not file.endswith(('.md')):
if not file.endswith('.md'):
print os.path.join(root, file)
45 changes: 23 additions & 22 deletions upload.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()
import cgi, cgitb, os, base64, re; cgitb.enable()

form = cgi.FieldStorage()
dataUrlPattern = re.compile('data:image/png;base64,(.*)$')

# A nested FieldStorage instance holds the file
if form.has_key('file'):
fileitem = form['file']
# Test if the file was uploaded
fname = fileitem.filename
if fname:
fn = os.path.basename(fname)
open('content/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
print 'Content-Type: text/plain\n';

if form.has_key('imagedata'):
imagedata = form['imagedata'].value
if form.has_key('fname'):
fn = form['fname'].value
fn = 'content/'+os.path.basename(fn)
match = dataUrlPattern.match(imagedata)
if match is not None:
imgb64 = match.group(1)
if imgb64 is not None and len(imgb64) > 0:
img = base64.b64decode(imgb64)
open(fn, 'wb').write(img)
os.chmod(fn, 0o0777);
print '@win\nThe file "' + fn + '" was uploaded successfully (but at what cost?)'
else:
print '@err\nIt didn\'t work.'
else:
print '@err\nNot a data:uri'
else:
message = 'No form["file"].filename'
print '@err\nNo form["filename"]'
else:
message = 'No form["file"]'

print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message)

print form
print '@err\nNo form["imagedata"]'

0 comments on commit a94a7ba

Please sign in to comment.