Commit 55615a55 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

Return 210 code if file hash different

parent 880fa09e
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -115,11 +115,11 @@ def _join_parts(
        for path in sorted(glob("{}/{}-*.part".format(parts_dir, total_parts))):
            with open(path, "rb") as s:
                f.write(s.read())
    if _file_hash256(destination) != sha256sum:
        raise RuntimeError("wrong checksum")
    else:  # free some disk space

    for path in glob("{}/{}-*.part".format(parts_dir, total_parts)):
        _silent_remove(path)
    if _file_hash256(destination) != sha256sum:
        return False
    return True


@@ -300,8 +300,14 @@ def upload_partial(request: HttpRequest) -> HttpResponse:
        total_chunks
    ) or os.path.exists(f_name):
        Path(lock_file_path).touch()  # lock dir

        if not os.path.exists(f_name):
            _join_parts(partials_dir, f_name, file_hash, total_chunks)
            checksum_ok = _join_parts(partials_dir, f_name, file_hash, total_chunks)
        elif file_hash == _file_hash256(f_name):
            checksum_ok = True
        else:
            checksum_ok = False

        with open(f_name, "rb") as f:
            try:
                with transaction.atomic():
@@ -326,6 +332,9 @@ def upload_partial(request: HttpRequest) -> HttpResponse:
                                user.username, user.pk, file_id, collection.pk
                            )
                        )
                if not checksum_ok:
                    return HttpResponse(file_id, status=210)  # content different
                else:
                    return HttpResponse(file_id, status=200)
            except UnknownFileType:
                _silent_rmdir(partials_dir)