-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
69 lines (50 loc) · 2.14 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
from __future__ import with_statement
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import files
from blobfile import File;
from blobfile import getContentType;
import logging
class RepoUpload(webapp.RequestHandler):
def post(self):
fileupload = self.request.POST.get("file",None)
if fileupload is None :
logging.debug("no file provided")
self.error(400)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write( "No file provided")
return
name=fileupload.filename
contentType = getContentType( name )
if contentType is None:
logging.debug("unsuported file type for " + name)
self.error(400)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write( "Unsupported file type: " + name)
return
data= fileupload.file.read()
mimeType=contentType
file = File.get_by_key_name(name)
if file is not None:
self.error(409)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write( "Artifact with same Id/version already there ")
return
# Create the file
file_name = files.blobstore.create(mime_type='application/octet-stream',_blobinfo_uploaded_filename=name)#_blobinfo_uploaded_filename not tested
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(data)
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
file = File(key_name=name,blobKey=blob_key)
file.put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('uploaded ' + name + ' at ' + str(blob_key))
application = webapp.WSGIApplication([('/.*', RepoUpload)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()