1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#!/usr/bin/python2.7
from bottle import route, run, post, request
from gradient import Gradient
from imgrid import Imgrid
from breaker import Breaker
from s3config import AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, BUCKET_NAME
import os
import sys
import db
import s3
import mimetypes
import sha
from subprocess import call, Popen, PIPE
import simplejson as json
BIN_IDENTIFY = "/usr/bin/identify"
try:
DB = db.db ()
except Exception as e:
sys.stderr.write("Could not connect to db:\n{}".format(e))
sys.exit(1);
BASE_URL = "http://i.asdf.us"
def hashdir(filename):
return sha.new(filename).hexdigest()[:2]
def file_size (filepath):
try:
return os.stat(file)[6]
except Exception as e:
sys.stderr.write(str(e))
raise;
def bin_identify (filepath):
ident = Popen([BIN_IDENTIFY, filepath], stdout=PIPE).communicate()[0]
partz = ident.split(" ")
width,height = partz[2].split("x")
return width, height
def cleanup(filepath):
try:
call(['rm', filepath])
except Exception as e:
sys.stderr.write(str(e))
raise
def moveToS3(filename,objectname):
conn = s3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
filedata = open(filename, 'rb').read()
content_type = mimetypes.guess_type(filename)[0]
try:
conn.put(BUCKET_NAME, objectname, s3.S3Object(filedata),
{
'x-amz-acl': 'public-read',
'Content-Type': content_type or 'text/plain',
'x-amz-storage-class': 'REDUCED_REDUNDANCY'
}
);
except Exception as e:
sys.stderr.write(str(e))
raise
def insert_cmd (date, remote_addr, username, url, directory, oldfile, newfile, cmd, dataobj, tag):
try:
sql = "INSERT INTO im_cmd "
sql += "(date, remote_addr, name, url, dir, oldfile, newfile, cmd, dataobj, tag) "
sql += "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
#or "NULL"
args = (now(), remote_addr, username, url, directory, oldfile, newfile, cmd, dataobj, tag)
#args = (now(), os.environ['REMOTE_ADDR'], name, url, dir, oldfile, newfile, " ".join(cmd),dataobj)
DB.execute(sql, args)
except Exception as e:
sys.stderr.write(str(e))
return
def return_image(im, insert_url="NULL"):
directory = hashdir(im.filename)
dimensions = bin_identify(im.filepath)
objectname = "{}/{}".format(directory, im.filename)
try:
s3move(im.filepath, objectname)
# return "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}".format(
insert_cmd(
im.now,
request.environ.get('REMOTE_ADDR'),
im.params['username'] or "NULL",
insert_url,
directory,
"NULL",
im.filename,
";".join(im.commands),
json.dumps(im.params),
im.tag,
)
return json.loads({
'url' : "{}/{}".format(BASE_URL, objectname)
'size' : file_size(im.filepath),
'width' : "{}px".format(dimensions[0]),
'height' : "{}px".format(dimensions[1]),
})
except Exception as e:
sys.stderr.write(str(e))
raise;
@post('/gradient')
def gradient():
try:
im = Gradient(request.forms)
im.create();
return return_image(im)
except Exception as e:
sys.stderr.write(str(e))
raise;
@post('/imgrid')
def imgrid():
try:
im = Imgrid(request.forms)
im.create();
return return_image(im, im.params.imageinstead or im.params.bgimage or im.params.planebgimage or "NULL")
except Exception as e:
sys.stderr.write(str(e))
return json.load({ 'error' : 'Request could not be processed' })
@post('/breaker')
def breaker():
try:
im = Breaker(request.forms)
im.create();
return return_image(im, im.params['url'])
except Exception as e:
sys.stderr.write(str(e))
return json.load({ 'error' : 'Request could not be processed' })
run(host='0.0.0.0', port=8999, debug=True)
|