import json
import logging
import tempfile
-import time
import tornado.web
from .. import builds
from .. import builders
-from .. import uploads
from .. import users
log = logging.getLogger("pakfire.hub")
return h, hexdigest
-class UploadsCreateHandler(BaseHandler):
- """
- Create a new upload object in the database and return a unique ID
- to the uploader.
- """
-
- @tornado.web.authenticated
- def get(self):
- # XXX Check permissions
-
- filename = self.get_argument("filename")
- filesize = self.get_argument_int("filesize")
- filehash = self.get_argument("hash", None)
-
- with self.db.transaction():
- upload = self.backend.uploads.create(filename, filesize,
- filehash, user=self.user, builder=self.builder)
-
- self.finish(upload.uuid)
-
-
-@tornado.web.stream_request_body
-class UploadsStreamHandler(BaseHandler):
- @tornado.web.authenticated
- def prepare(self):
- # Received file size
- self.size = 0
-
- upload_uuid = self.get_argument("id")
-
- # Fetch upload object from database
- self.upload = self.backend.uploads.get_by_uuid(upload_uuid)
- if not self.upload:
- raise tornado.web.HTTPError(404)
-
- def data_received(self, data):
- logging.debug("Received chunk of %s bytes" % len(data))
- self.size += len(data)
-
- # Write the received chunk to disk
- with self.db.transaction():
- self.upload.append(data)
-
- def put(self):
- logging.info("Received entire file (%s bytes)" % self.size)
-
- with self.db.transaction():
- self.upload.finished()
-
- self.finish("OK")
-
-
-class UploadsSendChunkHandler(BaseHandler):
- @tornado.web.authenticated
- def post(self, upload_id):
- upload = self.backend.uploads.get_by_uuid(upload_id)
- if not upload:
- raise tornado.web.HTTPError(404, "Invalid upload id.")
-
- if not upload.builder == self.builder:
- raise tornado.web.HTTPError(403, "Uploading an other host's file.")
-
- chksum = self.get_argument("chksum")
- data = self.get_argument("data")
-
- # Decode data.
- data = base64.b64decode(data)
-
- # Calculate hash and compare.
- h = hashlib.new("sha512")
- h.update(data)
-
- if not chksum == h.hexdigest():
- raise tornado.web.HTTPError(400, "Checksum mismatch")
-
- # Append the data to file.
- with self.db.transaction():
- upload.append(data)
-
-
-class UploadsFinishedHandler(BaseHandler):
- @tornado.web.authenticated
- def get(self, upload_id):
- upload = self.backend.uploads.get_by_uuid(upload_id)
- if not upload:
- raise tornado.web.HTTPError(404, "Invalid upload id.")
-
- if not upload.builder == self.builder:
- raise tornado.web.HTTPError(403, "Uploading an other host's file.")
-
- # Validate the uploaded data to its hash.
- ret = upload.validate()
-
- # If the validation was successfull, we mark the upload
- # as finished and send True to the client.
- if ret:
- upload.finished()
- self.finish("OK")
-
- return
-
- # In case the download was corrupted or incomplete, we delete it
- # and tell the client to start over.
- with self.db.transaction():
- upload.remove()
-
- self.finish("ERROR: CORRUPTED OR INCOMPLETE FILE")
-
-
-class UploadsDestroyHandler(BaseHandler):
- @tornado.web.authenticated
- def get(self, upload_id):
- upload = self.backend.uploads.get_by_uuid(upload_id)
- if not upload:
- raise tornado.web.HTTPError(404, "Invalid upload id.")
-
- if not upload.builder == self.builder:
- raise tornado.web.HTTPError(403, "Removing an other host's file.")
-
- # Remove the upload from the database and trash the data.
- with self.db.transaction():
- upload.remove()
-
-
# Builds
class BuildsCreateHandler(BaseHandler):