Exemplo n.º 1
0
def zip_album(h):
    a = Album.from_hash(h)
    paths = map(lambda f: file_storage(f.original), a.items)
    zip_path = file_storage(h + ".zip")

    if os.path.exists(zip_path):
        return

    call(["zip", "-j", zip_path] + paths)

    a.metadata = json.dumps({"has_zip": True})
    a.save()
Exemplo n.º 2
0
def compression_rate(originalpath, f):
    if f.processor == "default":
        return 0
    processor = get_processor(f.processor)

    original_size = os.path.getsize(originalpath)
    minsize = min(original_size, original_size)
    for ext in processor.outputs:
        try:
            convsize = os.path.getsize(file_storage("%s.%s" % (f.hash, ext)))
            print(("%s: %s (%s)" % (ext, convsize, original_size)))
            minsize = min(minsize, convsize)
        except OSError:
            continue  # One of the target files wasn't processed.
            # This will fail later in the processing workflow.

    # Cross-multiplication:
    # Original size   1
    # ------------- = -
    # Min size        x

    x = minsize / float(original_size)

    # Compression rate: 1/x
    return round(1 / x, 2)
Exemplo n.º 3
0
    def __init__(self, tmppath, f, processor_state, ignore_limit):
        self.path = tmppath
        self.output = file_storage(f.hash)
        self.processor_state = processor_state
        self.ignore_limit = ignore_limit

        self.important = True

        self.f = f
Exemplo n.º 4
0
    def __init__(self, tmppath, f, processor_state, ignore_limit):
        self.path = tmppath
        self.output = file_storage(f.hash)
        self.processor_state = processor_state
        self.ignore_limit = ignore_limit

        self.important = True

        self.f = f
Exemplo n.º 5
0
def upload(f, filename):
    if not f.content_type:
        f.content_type = get_mimetype(filename) or "application/octet-stream"

    #if f.content_type.split("/")[0] not in ['video', 'image', 'audio']:
    #    return "no", 415

    ignore_limit = current_app.debug or r.sismember(_k("whitelisted_ips"),
                                                    get_ip())
    if not ignore_limit:
        rate_limit_update(file_length(f))
        if rate_limit_exceeded():
            return None, 420

    h = get_hash(f)
    identifier = to_id(h)
    if "." not in filename:
        ext = mimetypes.guess_extension(
            f.content_type)[1:]  # This not very scientific, but it works
    else:
        ext = extension(filename)

    filename = "%s.%s" % (identifier, ext)
    path = tempfile.NamedTemporaryFile(
        suffix="." + ext).name  # Fix for imagemagick's silliness

    if os.path.exists(file_storage(filename)):
        if File.exists(identifier):
            return identifier, 409
        else:
            # Delete residual files from storage by creating a dummy File
            dummy = File(original=filename)
            dummy.delete = lambda: None  # nop
            delete_file(dummy)

    f.seek(0)  # Otherwise it'll write a 0-byte file
    f.save(path)

    file_object = File(hash=identifier)
    file_object.compression = os.path.getsize(path)
    file_object.original = filename
    file_object.mimetype = f.content_type
    file_object.ip = secure_ip()

    result = process_file.delay(path, identifier, ignore_limit)
    file_object.taskid = result.id

    file_object.save()

    return identifier, 200
Exemplo n.º 6
0
    def album_zip(self):
        if "hash" not in request.form:
            return {"error": 415}, 415

        h = request.form["hash"]
        klass = RedisObject.klass(h)
        if not klass or klass is not Album:
            return {"error": 404}, 404

        if os.path.exists(file_storage(h) + ".zip"):
            return {"status": "done"}

        zip_album.delay(h)

        return {"status": "success"}
Exemplo n.º 7
0
    def album_zip(self):
        if "hash" not in request.form:
            return {"error": 415}, 415

        h = request.form["hash"]
        klass = RedisObject.klass(h)
        if not klass or klass is not Album:
            return {"error": 404}, 404

        if os.path.exists(file_storage(h) + ".zip"):
            return {"status": "done"}

        zip_album.delay(h)

        return {"status": "success"}
Exemplo n.º 8
0
def upload(f, filename):
    if not f.content_type:
        f.content_type = get_mimetype(filename) or "application/octet-stream"

    #if f.content_type.split("/")[0] not in ['video', 'image', 'audio']:
    #    return "no", 415

    ignore_limit = current_app.debug or r.sismember(_k("whitelisted_ips"), get_ip())
    if not ignore_limit:
        rate_limit_update(file_length(f))
        if rate_limit_exceeded():
            return None, 420

    h = get_hash(f)
    identifier = to_id(h)
    if "." not in filename:
        ext = mimetypes.guess_extension(f.content_type)[1:] # This not very scientific, but it works
    else:
        ext = extension(filename)

    filename = "%s.%s" % (identifier, ext)
    path = tempfile.NamedTemporaryFile(suffix="." + ext).name # Fix for imagemagick's silliness

    if os.path.exists(file_storage(filename)):
        if File.exists(identifier):
            return identifier, 409
        else:
            # Delete residual files from storage by creating a dummy File
            dummy = File(original=filename)
            dummy.delete = lambda: None # nop
            delete_file(dummy)

    f.seek(0)  # Otherwise it'll write a 0-byte file
    f.save(path)

    file_object = File(hash=identifier)
    file_object.compression = os.path.getsize(path)
    file_object.original = filename
    file_object.mimetype = f.content_type
    file_object.ip = secure_ip()

    result = process_file.delay(path, identifier, ignore_limit)
    file_object.taskid = result.id

    file_object.save()

    return identifier, 200
Exemplo n.º 9
0
def compression_rate(originalpath, f):
    if f.processor == 'default': return 0
    processor = get_processor(f.processor)

    original_size = os.path.getsize(originalpath)
    minsize = min(original_size, original_size)
    for ext in processor.outputs:
        try:
            convsize = os.path.getsize(file_storage("%s.%s" % (f.hash, ext)))
            print("%s: %s (%s)" % (ext, convsize, original_size))
            minsize = min(minsize, convsize)
        except OSError:
            continue # One of the target files wasn't processed.
                     # This will fail later in the processing workflow.

    # Cross-multiplication:
    # Original size   1
    # ------------- = -
    # Min size        x

    x = minsize / float(original_size)

    # Compression rate: 1/x
    return round(1/x, 2)