From 4100860d10e2fb015db01d22bbf3f4735bcf10ec Mon Sep 17 00:00:00 2001 From: yo mama Date: Fri, 13 Feb 2015 01:33:31 -0800 Subject: first --- impattern/im/cgi-bin/imweb/databaser.py | 24 ++++++ impattern/im/cgi-bin/imweb/downImg | 55 +++++++++++++ impattern/im/cgi-bin/imweb/downloader.py | 114 +++++++++++++++++++++++++++ impattern/im/cgi-bin/imweb/fieldstorager.py | 21 +++++ impattern/im/cgi-bin/imweb/filenamer.py | 115 ++++++++++++++++++++++++++++ impattern/im/cgi-bin/imweb/hexdirer.py | 38 +++++++++ impattern/im/cgi-bin/imweb/saveImg | 48 ++++++++++++ 7 files changed, 415 insertions(+) create mode 100755 impattern/im/cgi-bin/imweb/databaser.py create mode 100755 impattern/im/cgi-bin/imweb/downImg create mode 100755 impattern/im/cgi-bin/imweb/downloader.py create mode 100755 impattern/im/cgi-bin/imweb/fieldstorager.py create mode 100755 impattern/im/cgi-bin/imweb/filenamer.py create mode 100755 impattern/im/cgi-bin/imweb/hexdirer.py create mode 100755 impattern/im/cgi-bin/imweb/saveImg (limited to 'impattern/im/cgi-bin/imweb') diff --git a/impattern/im/cgi-bin/imweb/databaser.py b/impattern/im/cgi-bin/imweb/databaser.py new file mode 100755 index 0000000..07b8723 --- /dev/null +++ b/impattern/im/cgi-bin/imweb/databaser.py @@ -0,0 +1,24 @@ +#!/usr/bin/python + +from db import * +import os +DB = db () + +PATH_TO_S3CMD = "/var/www/asdf.us/cgi-bin/im/s3/s3cmd-1.1.0-beta3/s3cmd" +PATH_TO_IMAGES = "/var/www/asdf.us/docs/im" + +global idnumber + +function dbGet(idnumber): + try: + DB.cursor.execute('SELECT dir, newfile, id FROM im_cmd WHERE id='+id) + #sql = "INSERT INTO im_cmd (date,remote_addr,name,dir,newfile,cmd) VALUES(%s,%s,%s,%s,%s,%s)" + #args = (now(), remoteaddress, name, dir, newfile, cmd) + data = cursor.fetchall + print str(data) + except (): + return + +if __name__ == '__main__': + sys.argv[1] = idnumber + dbGet(idnumber) \ No newline at end of file diff --git a/impattern/im/cgi-bin/imweb/downImg b/impattern/im/cgi-bin/imweb/downImg new file mode 100755 index 0000000..d029477 --- /dev/null +++ b/impattern/im/cgi-bin/imweb/downImg @@ -0,0 +1,55 @@ +#!/usr/bin/python + +import cgi +import downloader +import fieldstorager +import sys +import os +#import gifchooser + +#BASE_DIR = "/var/www/asdf.us/httpdocs/imweb/" +BASE_DIR = "" +PARAM_LIST = "texture heightmap" +#BIN_CONVERT = "/usr/bin/convert" +BIN_CONVERT = "convert" + +print "Content-type: text/plain" +print "" + +form = cgi.FieldStorage() +newvalues = fieldstorager.fieldstorage(PARAM_LIST, form) +thekeys = PARAM_LIST.split() +for key in thekeys: + globals()[key] = newvalues[key] +######################################################## +#test with values here +texture = "http://i.asdf.us/im/f3/imBreak_1327641897_clifford_1327640637_pepper.png" +heightmap = "http://dump.fm/images/20120127/1327645413657-dumpfm-pepper-topcomments.png" +####################################################### +def uploadit(pictureUrl): + if len(pictureUrl) > 3 and "." in pictureUrl: + imageparts = pictureUrl.split('.') + filetype = imageparts[-1] + if "?" in filetype: + theparts = filetype.split('?') + filetype = theparts[0] + if pictureUrl == texture: + imagename = "Texture" + if pictureUrl == heightmap: + imagename = "Heightmap" + if "png" in pictureUrl.lower() or "jpg" in pictureUrl.lower() or "jpeg" in pictureUrl.lower() or "gif" in pictureUrl.lower(): + downloader.download(pictureUrl, (imagename+"."+filetype), BASE_DIR) + firstfile = BASE_DIR+imagename+"."+filetype + endfile = BASE_DIR+imagename+"."+"jpg" + os.system(BIN_CONVERT+" -resize 256x256\! "+firstfile+" "+endfile) + if pictureUrl == "Heightmap": + os.system(BIN_CONVERT+" colorspace -gray "+" "+endfile+" "+endfile) + os.system("rm heightmap.png heightmap.gif texture.png texture.gif") + print "upload"+imagename+" successful" + return endfile + else: + print "This is not an acceptable image format for "+imagename + return +uploadit(texture) +uploadit(heightmap) +sys.exit() diff --git a/impattern/im/cgi-bin/imweb/downloader.py b/impattern/im/cgi-bin/imweb/downloader.py new file mode 100755 index 0000000..7083b11 --- /dev/null +++ b/impattern/im/cgi-bin/imweb/downloader.py @@ -0,0 +1,114 @@ +#!/usr/bin/python +import random +import urllib, urllib2 +urlopen = urllib2.urlopen +Request = urllib2.Request +import sys + +MAX_SIZE = 1024 * 1024 * 1.2 + +###___this somewhat poorly written program basically performs a get request +#you can specify three arguments, the url, the destination path and the filename#that you'll want your file to be called +#these can be specified as argument values from the commandline or as +#values in the mainfunction which is downloader.download(url,filename,destination) +#the destination and filename arguments are optional + + + +url = "" +destination = "" +filename = "" + +def argval(): + global destination + global url + global filename + stuff = sys.argv + lenstuff = len(stuff) + if lenstuff >= 4: + getvalues() + if lenstuff ==3: + print str(lenstuff) + url = sys.argv[-2] + filename = sys.argv[-1] + if lenstuff ==2: + url = sys.argv[-1] + destination = "" + partz = url.split('/') + filename = partz[-1] + if lenstuff ==1: + print 'please provide url' + return None + return url + +def getvalues(): + global url + global destination + global filename + url = sys.argv[-3] + destination = sys.argv[-1] + filename = sys.argv[-2] + if len(filename) == 0: + partz = url.split('/') + filename = partz[-1] + if filename == "" or filename == None: + partz = url.split('/') + filename = partz[-1] + return url + +def browser_request (url,data=None): + headers = { + 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', + 'Accept': '*/*', + } + try: + req = Request(url, data, headers) + response = urlopen(req) + return response + except: + print "ERROR" + sys.stdout.write('there is a problem with the url or an I/O error') + return None + sys.exit() + + +def download(url, filename="",destination=""): + response = browser_request(url) + rawimg = response.read() + if len(rawimg) == 0: + print error("got zero-length file") + sys.stdout.write("file did not exist or was zero-length") + sys.exit() + if len(rawimg) > MAX_SIZE and "asdf.us" not in url: + error = "file too big: max size " + str(MAX_SIZE/1024) + " KB / " + filename + " is " + str(len(rawimg)/1024) + " KB" + print error + sys.stdout.write(error) + sys.exit() + if filename == "": + partz = url.split('/') + filename = partz[-1] + path = "" + if destination == "" or destination == '': + path = filename + else: + path = destination+filename + f = open(path, "w") + f.write(rawimg) + print 'success' + f.close() + finalitems = {} + finalitems['url'] = url + finalitems['filename'] = filename + finalitems['destination'] = destination + return finalitems + +def downloader(): + global url + global destination + global filename + argval() + finalitems = download(url, filename, destination) + return finalitems + +if __name__ == '__main__': + downloader() diff --git a/impattern/im/cgi-bin/imweb/fieldstorager.py b/impattern/im/cgi-bin/imweb/fieldstorager.py new file mode 100755 index 0000000..55b00ad --- /dev/null +++ b/impattern/im/cgi-bin/imweb/fieldstorager.py @@ -0,0 +1,21 @@ +#!/usr/bin/python + +def fieldstorage(paramlist, form): + theparams = paramlist.split() + newvalues = {} + for item in theparams: + if item in form: + newvalues[item] = form[item].value + else: + newvalues[item] = None + for item in theparams: + if newvalues[item] is not None: + if 'true' in newvalues[item] or 'True' in newvalues[item]: + if len(newvalues[item]) <= 6: + newvalues[item] = True + elif 'false' in newvalues[item] or 'False' in newvalues[item]: + if len(newvalues[item]) <= 7: + newvalues[item] = False + return newvalues + + diff --git a/impattern/im/cgi-bin/imweb/filenamer.py b/impattern/im/cgi-bin/imweb/filenamer.py new file mode 100755 index 0000000..9b6ef32 --- /dev/null +++ b/impattern/im/cgi-bin/imweb/filenamer.py @@ -0,0 +1,115 @@ +#!/usr/bin/python + +import sys +import os +import re +import time +import string +import urllib +import sha + +#this program takes a url and makes a new filename for it. it also +#splits out the extension. this is important for manipulating images +#it returns a hash, part[0] is the filename and part[1] is the extension +#called like this filenamer.filename(url, nametag, username) + + +thefilename = "" +extension = "" + +nametag = "" +username = "" +url = "" + +def getargs(): + global username + global nametag + global url + args = sys.argv + if len(args) < 2: + return None + if len(args) >= 4: + username = args[-1] + nametag = args[-2] + url = args[-3] + if len(args) == 3: + nametag = args[-1] + url = args[-2] + if len(args) == 2: + url = args[-1] + thereturn = {} + thereturn['url'] = url + thereturn['nametag'] = nametag + thereturn['username'] = username + return thereturn + +def sanitize (str):#this removes pretty much everything but letters(and underscores) from a url + return re.sub(r'\W+', '', str) +#def sanitize (str): +# tmp_str = urllib.unquote(str) +# return re.sub(r'\W+', '', tmp_str) + + + + +def now (): + return int(time.mktime(time.localtime())) + +def spliturl(url): + if len(url) < 3 or '/' not in url: + sys.stdout.write('you have not provided a url') + print 'you have not provided a url' + sys.exit() + parts = url.split('/') + namepart = parts[-1] + importantparts = namepart.split('.') + extension = importantparts[-1] + if "?" in extension: + querysplit = extension.split('?') + extension = querysplit[0] + if len(extension) >= 5: + sys.stdout.write('the file is not an image or any standard format') + print 'the file is not an image or any standard format' + sys.exit + namepart = importantparts[-2] + if len(namepart)>50: + namepart = "xx_abridged__" + sanitized = sanitize(namepart) + salvaged = {} + salvaged['extension'] = extension + salvaged['namepart'] = sanitized + return salvaged + +def prepare(url): + global nametag + global username + time = now() + datestamp = str(time) + parts = spliturl(url) + extension = parts['extension'] + original = parts['namepart'] + thefilename = nametag+'_'+datestamp+"_"+original+"_"+username+'.'+extension + return thefilename + +def filename(url, tag="", usrname=""): + global nametag + global username + if tag == None: + tag = "" + if usrname == None: + usrname = "" + nametag = tag + username = usrname + thefilename = prepare(url) + print thefilename + return thefilename + sys.exit() + + +if __name__ == '__main__': + vars = getargs() + url=vars['url'] + nametag=vars['nametag'] + username = vars['username'] + filename(url,nametag,username) + diff --git a/impattern/im/cgi-bin/imweb/hexdirer.py b/impattern/im/cgi-bin/imweb/hexdirer.py new file mode 100755 index 0000000..af6f60e --- /dev/null +++ b/impattern/im/cgi-bin/imweb/hexdirer.py @@ -0,0 +1,38 @@ +#!/usr/bin/python + +import sys +import os +import time +import string +import sha +BASE_DIR = "/var/www/asdf.us/httpdocs/im/" +OTHER_BASE = "http://asdf.us/im/" + +#THIS MODULE RETURNS A COMPLETE PATH! ALL YOU HAVE TO DO IS +#SPECIFY THE BASE DIRECTORY +#the hash works like this: +#0 is private path +#1 is the public path + + + +def hash_dir (s): + return sha.new(s).hexdigest()[:2] + +def now (): + return int(time.mktime(time.localtime())) + +def hexdir(): + time = now() + time = str(time) + ending = hash_dir(time) + dirs = [] + dirs.append(BASE_DIR+ending+'/') + dirs.append(OTHER_BASE+ending+'/') + print str(dirs) + return dirs + sys.exit() + +if __name__ == "__main__": + hexdir() + diff --git a/impattern/im/cgi-bin/imweb/saveImg b/impattern/im/cgi-bin/imweb/saveImg new file mode 100755 index 0000000..e76c36a --- /dev/null +++ b/impattern/im/cgi-bin/imweb/saveImg @@ -0,0 +1,48 @@ +#!/usr/bin/python + +import hexdirer +import filenamer +import cgi +import fieldstorager +import base64 +import random +import sys +import databaser + +print "Content-type: text/plain" +print "" + +nametag = "imWeb" +#name is the image name + +PARAM_LIST = "name username picture" +form = cgi.FieldStorage() +newvalues = fieldstorager.fieldstorage(PARAM_LIST, form) +thekeys = PARAM_LIST.split() +for key in thekeys: + globals()[key] = newvalues[key] +if username == None: + username = "" +if name == None: + name = str(random.randint(0,100)) +############################################################ +#test here +########################################################### +try: + #the picture is first encoded so we have to decode it + picture = base64.b64decode(picture) + namepart = filenamer.filename(("/"+name+".png"),username,nametag) + filepaths = hexdirer.hexdir() + filepaths = ["",""] + + thefile = filepaths[0]+namepart + f = open(thefile,'w') + for line in picture: + f.write(line) + f.close() + +# databaser.database(filepaths[0],namepart,"imweb capture",username) + print "capture successful" + print filepaths[1]+namepart +except: + print "sorry, there is a problem with image capture" -- cgit v1.2.3-70-g09d2