forked from renefritze/spring-infolog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.py
executable file
·89 lines (80 loc) · 2.61 KB
/
upload.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TODO - don't allow zero byte files to be stored
from bottle import route,request,redirect
import bottle
from siteglobals import env, db,config,tasbot,cache,is_debug
from utils import *
import hashlib,os,sys
from zipfile import ZipFile
import recordlist
if is_debug:
@route('/upload', method='GET')
def output():
try:
return env.get_template('upload.html').render( )
except Exception, m:
return env.get_template('error.html').render(err_msg=str(m))
def parseZip( fn ):
date_time = ''
members = dict()
removemembers = False
zipfile = ZipFile( fn )
cache.invalidate(recordlist.output, 'list_output', )
files_of_interest = ['infolog.txt','ext.txt','platform.txt','script.txt','settings.txt','unitsync.log','client.txt','information.txt','demo.sdf']
for info in zipfile.infolist():
if info.filename in files_of_interest and info.file_size < 5 * 1024 * 1024:
members[info.filename] = zipfile.read( info.filename )
if info.filename == 'infolog.txt':
date_time = info.date_time
else:
removemembers = True
if removemembers:
newzipfile = ZipFile (fn + '.new', 'w')
tmpfilename = '/tmp/' + os.path.basename (fn) + '.tmp'
for file in members.keys ():
tmpfile = open (tmpfilename, 'w')
tmpfile.write (zipfile.read (file))
tmpfile.close ()
newzipfile.write (tmpfilename, file)
os.remove (tmpfilename)
newzipfile.close ()
zipfile.close ()
os.rename (fn, fn + '.orig')
os.rename (fn + '.new', fn)
else:
zipfile.close ()
return db.parseZipMembers( fn, members, date_time )
@route('/upload', method='POST')
def output_post():
try:
data = request.POST['file'].value
upload_dir = config.get('site','uploads')
fn_relative = '%s/%s.zip'%( upload_dir,hashlib.sha224(data).hexdigest() )
fn = fn_relative
#fn = '%s/%s'%( os.getcwd(), fn_relative )
fd = open( fn, 'wb')
fd.write( data )
fd.close()
new_id = parseZip( fn )
base_url = config.get('site', 'baseurl' )
chan = config.get('tasbot', 'report_channel' )
tasbot.tasclient.say(chan,'new crash report uploaded: http://%s%d'%(config.get('site', 'detailurl' ),new_id))
return 'success'
except Exception, m:
print m
return env.get_template('error.html').render(err_msg=str(m))
if __name__=="__main__":
failed = []
# print 'clearing DB'
# import rebuild
sys.stdout.write( 'parsing' )
for fn in sys.argv[1:]:
try:
sys.stdout.write( '.' )
sys.stdout.flush()
parseZip( fn )
except Exception, e:
failed.append( 'failed: %s: %s'%(fn, str(e) ) )
print '\nsuccesfully imported %d of %d records'%(len(sys.argv[1:]) - len(failed), len(sys.argv[1:]) )
print '\n'.join(failed)