summaryrefslogtreecommitdiff
path: root/pb/Imlandscape
diff options
context:
space:
mode:
Diffstat (limited to 'pb/Imlandscape')
-rwxr-xr-xpb/Imlandscape/__init__.py101
-rwxr-xr-xpb/Imlandscape/landscape163
2 files changed, 264 insertions, 0 deletions
diff --git a/pb/Imlandscape/__init__.py b/pb/Imlandscape/__init__.py
new file mode 100755
index 0000000..24cee8d
--- /dev/null
+++ b/pb/Imlandscape/__init__.py
@@ -0,0 +1,101 @@
+#!/usr/bin/python2.7
+import os
+import sys
+import random
+import re
+import pb.lib.Utils as utils
+import urllib
+import urlparse
+from pb.Config import *
+
+import base64
+import time
+import string
+import urllib
+from subprocess import Popen, PIPE
+import sha
+import simplejson as json
+
+import mimetypes
+
+class Imlandscape(object):
+ def __init__(self, **kwargs):
+ self.params = {}
+ self.tag = "imlandscape"
+ self.commands = [];
+ self._required_keys = [
+ "heightmap",
+ "imgdata",
+ "texture",
+ "name",
+ ]
+ self.now = utils.now()
+ self.files_created = []
+ for k in self._required_keys:
+ if k in kwargs:
+ self.params[k] = kwargs[k]
+ else:
+ self.params[k] = utils.bool_correct(utils.sanitize(kwargs[k]))
+
+ def _filename_from_url (self, url, name=""):
+ if "?" in url:
+ url = url.split("?")[0]
+ if "/" in url:
+ url = urllib.unquote(url).replace(" ","")
+ filename = url.split("/")[-1]
+ filetype = "png"
+ filename = sanitize(filename[:-4])
+ else:
+ filename = ""
+ if name != "":
+ name = name+"_"
+ return "{}_{}{}_{}.{}".format("imlandscape", name, filename,self.now, "png")
+
+ def _saveImgData(self, imgdata, filename):
+ try:
+# up = urlparse.urlparse(url)
+# head, data = imgdata.split(',', 1)
+# bits = head.split(';')
+ parts = imgdata.split(';')
+ mime_type = parts[0] if parts[0] else 'text/plain'
+ data = parts[1]
+ charset, b64 = 'ASCII', False
+ for part in parts[1]:
+ if part.startswith('charset='):
+ charset = part[8:]
+ elif part == 'base64':
+ b64 = True
+
+ # Do something smart with charset and b64 instead of assuming
+ if b64:
+ plaindata = base64.b64decode(data)
+ else:
+ plaindata = data
+ with open(filename, 'wb') as f:
+ f.write(plaindata)
+ except Exception as e:
+ sys.stderr.write("ERROR: {}\n".format(str(e)));
+
+# def _cleanup(self):
+# cmd = ["rm"]+self.files_created
+# self._call_cmd(cmd)
+
+ def create(self, breakmode=""):
+ self.filepath = self._filename_from_url(self.params.get('texture',""), self.params.get('name',""));
+ self._saveImgData(self.params.get('imgdata'), self.filepath);
+
+if __name__ == "__main__":
+ f = open("/tmp/base64img")
+ data = f.read()
+ f.close()
+ TEST_PARAMS = {
+ "heightmap" : "https%3A%2F%2Fwww.google.com%2Fimages%2Fsrpr%2Flogo11w.png" ,
+ "imgdata" : data,
+ "texture" : "https%3A%2F%2Fwww.google.com%2Fimages%2Fsrpr%2Flogo11w.png",
+ "name" : "pepper",
+ }
+ b = Imlandscape(**TEST_PARAMS)
+ b.create();
+ print b.filepath
+
+
diff --git a/pb/Imlandscape/landscape b/pb/Imlandscape/landscape
new file mode 100755
index 0000000..10e8ede
--- /dev/null
+++ b/pb/Imlandscape/landscape
@@ -0,0 +1,163 @@
+#!/usr/bin/python2.7
+import cgi
+import sys
+import os
+import re
+import time
+import string
+import urllib
+from subprocess import Popen, PIPE
+import sha
+import simplejson as json
+
+import mimetypes
+import s3
+
+import db
+DB = db.db ()
+
+import base64
+import urlparse
+
+
+AWS_ACCESS_KEY_ID = 'AKIAIR53VPBXKJMXZIBA'
+AWS_SECRET_ACCESS_KEY = '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'
+BUCKET_NAME = 'i.asdf.us'
+BASE_PATH = "/var/www/asdf.us/httpdocs/imlandscape"
+BASE_URL = "http://i.asdf.us/"
+PARAM_LIST = "heightmap texture name imgdata filename"
+BIN_IDENTIFY = "/usr/bin/identify"
+
+print "Content-type: text/plain"
+print ""
+def insert_cmd (dir, newfile, name, texture, dataobj):
+ if texture == "":
+ texture = "NULL"
+ try:
+ sql = "INSERT INTO im_cmd (date,remote_addr,name,url,dir,oldfile,newfile,cmd, dataobj, tag) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s, %s)"
+
+ args = (now(), "NULL", name, texture, dir, "NULL", newfile, "NULL", dataobj, "imlandscape")
+ DB.execute(sql, args)
+ except ():
+ return
+
+def hash_dir (s):
+ return sha.new(s).hexdigest()[:2]
+
+def bin_identify (filename):
+ ident = Popen([BIN_IDENTIFY, filename], stdout=PIPE).communicate()[0]
+ partz = ident.split(" ")
+ width,height = partz[2].split("x")
+ return width, height
+
+def get_params (paramlist):
+ paramkeys = paramlist.split()
+ form = cgi.FieldStorage()
+ params = {}
+ for key in paramkeys:
+ if key in form:
+ if key == 'heightmap':
+ params[key] = form[key].value
+ elif key == 'imgdata':
+ params[key] = form[key].value
+ elif key == 'texture':
+ params[key] = form[key].value
+ else:
+ params[key] = sanitize(form[key].value)
+ else:
+ params[key] = None
+ return params
+
+def error (e):
+ print "#@imlandscape"
+ print "ERROR\t"+e
+ sys.exit()
+
+def now ():
+ return int(time.mktime(time.localtime()))
+
+def sanitize (str):
+ return re.sub(r'\W+', '', str)
+
+def filename_from_url (url, name=""):
+ if "?" in url:
+ url = url.split("?")[0]
+ if "/" in url:
+ url = urllib.unquote(url).replace(" ","")
+ filename = url.split("/")[-1]
+ filetype = "png"
+ filename = sanitize(filename[:-4])
+ else:
+ filename = ""
+ if name != "":
+ name = name+"_"
+ return "{}_{}{}_{}.{}".format("imlandscape", name, filename,now(), "png")
+
+def saveImgData(url, filename):
+ try:
+ up = urlparse.urlparse(url)
+ head, data = up.path.split(',', 1)
+ bits = head.split(';')
+ mime_type = bits[0] if bits[0] else 'text/plain'
+ charset, b64 = 'ASCII', False
+ for bit in bits[1]:
+ if bit.startswith('charset='):
+ charset = bit[8:]
+ elif bit == 'base64':
+ b64 = True
+
+ # Do something smart with charset and b64 instead of assuming
+ plaindata = base64.b64decode(data)
+
+ with open(filename, 'wb') as f:
+ f.write(plaindata)
+ except Exception as e:
+ error(str(e));
+
+def file_size (file):
+ return os.stat(file)[6]
+
+def moveToS3(filename,objectname):
+ conn = s3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
+ sys.stderr.write( "Uploading %s" % filename)
+ filedata = open(filename, 'rb').read()
+ content_type = mimetypes.guess_type(filename)[0]
+ if not content_type:
+ content_type = 'text/plain'
+ conn.put(BUCKET_NAME, objectname, s3.S3Object(filedata),
+ {'x-amz-acl': 'public-read', 'Content-Type': content_type, 'x-amz-storage-class': 'REDUCED_REDUNDANCY'})
+
+param = get_params(PARAM_LIST)
+if param['imgdata'] is None:
+ error("no imgdata")
+url = param['imgdata']
+if param['texture'] is None:
+ param['texture'] = "";
+if param['heightmap'] is None:
+ param['heightmap'] = "";
+if param['name'] is None:
+ param['name'] = "";
+
+dataobj = json.dumps({
+ 'texture' : param['texture'],
+ 'heightmap' : param['heightmap'],
+ 'name' : param['name']
+})
+
+dir = hash_dir(param['imgdata']);
+
+filename = filename_from_url(param['texture'], param['name']);
+
+tag = "imlandscape"
+objectname = "im/"+dir+"/"+filename
+
+saveImgData(param['imgdata'], filename);
+
+print "#@imlandscape"
+#print ", ".join([k+"="+str(v) for k,v in param.iteritems()])
+print file_size (filename)
+print bin_identify (filename)
+print BASE_URL+objectname
+insert_cmd(dir, filename, param['name'], param['texture'], dataobj);
+moveToS3(filename, objectname);
+os.remove(filename);