diff --git a/migrations/versions/70ab3c81827a_.py b/migrations/versions/70ab3c81827a_.py index a8c5c20..60765dc 100644 --- a/migrations/versions/70ab3c81827a_.py +++ b/migrations/versions/70ab3c81827a_.py @@ -11,50 +11,62 @@ import sqlalchemy_utils # revision identifiers, used by Alembic. -revision = '70ab3c81827a' -down_revision = '4cd7cdbc2d1f' +revision = "70ab3c81827a" +down_revision = "4cd7cdbc2d1f" branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.create_table('uploads', - sa.Column('upload_id', sa.String(), nullable=False), - sa.Column('share_id', sqlalchemy_utils.types.uuid.UUIDType(), nullable=True), - sa.Column('create_date', sa.DateTime(), nullable=False), - sa.Column('total_chunks', sa.Integer(), nullable=False), - sa.Column('recv_chunks', sa.Integer(), nullable=False), - sa.Column('completed', sa.Boolean(), nullable=False), - sa.ForeignKeyConstraint(['share_id'], ['shares.share_id'], ), - sa.PrimaryKeyConstraint('upload_id') + op.create_table( + "uploads", + sa.Column("upload_id", sa.String(), nullable=False), + sa.Column("share_id", sqlalchemy_utils.types.uuid.UUIDType(), nullable=True), + sa.Column("create_date", sa.DateTime(), nullable=False), + sa.Column("total_chunks", sa.Integer(), nullable=False), + sa.Column("recv_chunks", sa.Integer(), nullable=False), + sa.Column("completed", sa.Boolean(), nullable=False), + sa.ForeignKeyConstraint( + ["share_id"], + ["shares.share_id"], + ), + sa.PrimaryKeyConstraint("upload_id"), ) - op.create_table('chunks', - sa.Column('chunk_id', sa.Integer(), autoincrement=True, nullable=False), - sa.Column('create_date', sa.DateTime(), nullable=False), - sa.Column('index', sa.Integer(), nullable=False), - sa.Column('upload_id', sa.String(), nullable=True), - sa.Column('filename', sa.String(), nullable=False), - sa.ForeignKeyConstraint(['upload_id'], ['uploads.upload_id'], ), - sa.PrimaryKeyConstraint('chunk_id') + op.create_table( + "chunks", + sa.Column("chunk_id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("create_date", sa.DateTime(), nullable=False), + sa.Column("index", sa.Integer(), nullable=False), + sa.Column("upload_id", sa.String(), nullable=True), + sa.Column("filename", sa.String(), nullable=False), + sa.ForeignKeyConstraint( + ["upload_id"], + ["uploads.upload_id"], + ), + sa.PrimaryKeyConstraint("chunk_id"), ) - with op.batch_alter_table('shares', schema=None) as batch_op: - batch_op.alter_column('share_id', - existing_type=sa.NUMERIC(precision=16), - type_=sqlalchemy_utils.types.uuid.UUIDType(), - existing_nullable=False) + with op.batch_alter_table("shares", schema=None) as batch_op: + batch_op.alter_column( + "share_id", + existing_type=sa.NUMERIC(precision=16), + type_=sqlalchemy_utils.types.uuid.UUIDType(), + existing_nullable=False, + ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('shares', schema=None) as batch_op: - batch_op.alter_column('share_id', - existing_type=sqlalchemy_utils.types.uuid.UUIDType(), - type_=sa.NUMERIC(precision=16), - existing_nullable=False) + with op.batch_alter_table("shares", schema=None) as batch_op: + batch_op.alter_column( + "share_id", + existing_type=sqlalchemy_utils.types.uuid.UUIDType(), + type_=sa.NUMERIC(precision=16), + existing_nullable=False, + ) - op.drop_table('chunks') - op.drop_table('uploads') + op.drop_table("chunks") + op.drop_table("uploads") # ### end Alembic commands ### diff --git a/sachet/server/files/views.py b/sachet/server/files/views.py index 679c9f6..900b429 100644 --- a/sachet/server/files/views.py +++ b/sachet/server/files/views.py @@ -90,19 +90,13 @@ class FileContentAPI(MethodView): except KeyError as err: return ( jsonify( - dict( - status="fail", message=f"Missing data for chunking; {err}" - ) + dict(status="fail", message=f"Missing data for chunking; {err}") ), 400, ) except ValueError as err: return ( - jsonify( - dict( - status="fail", message=f"{err}" - ) - ), + jsonify(dict(status="fail", message=f"{err}")), 400, ) diff --git a/sachet/server/models.py b/sachet/server/models.py index adef63f..b1dabd8 100644 --- a/sachet/server/models.py +++ b/sachet/server/models.py @@ -333,8 +333,10 @@ class Upload(db.Model): completed = db.Column(db.Boolean, nullable=False, default=False) chunks = db.relationship( - "Chunk", backref=db.backref("upload"), order_by="Chunk.chunk_id", - cascade="all, delete" + "Chunk", + backref=db.backref("upload"), + order_by="Chunk.chunk_id", + cascade="all, delete", ) def __init__(self, upload_id, total_chunks, share_id): @@ -406,10 +408,11 @@ class Chunk(db.Model): self.upload = Upload.query.filter_by(upload_id=upload_id).first() if self.upload is None: self.upload = Upload(upload_id, total_chunks, share.share_id) - self.upload_id = upload_id self.upload.recv_chunks = 0 db.session.add(self.upload) + self.upload_id = upload_id + self.create_date = datetime.datetime.now() self.index = index self.filename = f"{share.share_id}_{self.upload_id}_{self.index}" diff --git a/tests/conftest.py b/tests/conftest.py index 1aa3b37..bddd6a0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -224,6 +224,7 @@ def upload(client): method : function Method like client.post or client.put to use. """ + def upload(url, data, headers={}, chunk_size=int(2e6), method=client.post): data_size = len(data.getbuffer()) @@ -257,6 +258,4 @@ def upload(client): return resp - - return upload diff --git a/tests/test_anonymous.py b/tests/test_anonymous.py index 04c6aa1..ac38bf8 100644 --- a/tests/test_anonymous.py +++ b/tests/test_anonymous.py @@ -150,10 +150,7 @@ def test_files_invalid(client, auth, rand, upload): data = resp.get_json() url = data.get("url") upload_data = rand.randbytes(4000) - resp = upload( - url + "/content", - BytesIO(upload_data) - ) + resp = upload(url + "/content", BytesIO(upload_data)) assert resp.status_code == 201 # disable all permissions @@ -165,26 +162,15 @@ def test_files_invalid(client, auth, rand, upload): assert resp.status_code == 200 # test initializing a share without perms - resp = upload( - url + "/content", - BytesIO(upload_data) - ) + resp = upload(url + "/content", BytesIO(upload_data)) assert resp.status_code == 401 # test reading a share without perms resp = client.get(url + "/content") # test modifying an uninitialized share without perms - resp = upload( - uninit_url + "/content", - BytesIO(upload_data), - method=client.put - ) + resp = upload(uninit_url + "/content", BytesIO(upload_data), method=client.put) assert resp.status_code == 401 # test modifying a share without perms - resp = upload( - url + "/content", - BytesIO(upload_data), - method=client.put - ) + resp = upload(url + "/content", BytesIO(upload_data), method=client.put) assert resp.status_code == 401 # test deleting a share without perms