Skip to content

Commit

Permalink
[WIP] Start division between new and edit file uploads.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmiegit committed Jul 11, 2024
1 parent 0882387 commit 1001d8d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 40 deletions.
3 changes: 3 additions & 0 deletions deepwell/src/services/blob/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ impl BlobService {
}
};

debug!("Deleting pending blob");
BlobPending::delete_by_id(pending_blob_id).exec(txn).await?;

// Special handling for empty blobs
if data.is_empty() {
debug!("File being created is empty, special case");
Expand Down
123 changes: 87 additions & 36 deletions deepwell/src/services/file/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,26 @@ use crate::services::{BlobService, FileRevisionService, FilterService};
pub struct FileService;

impl FileService {
/// Creates a new file.
///
/// Starts a file upload and tracks it as a distinct file entity.
///
/// In the background, this stores the blob via content addressing,
/// meaning that duplicates are not uploaded twice.
pub async fn start_upload(
pub async fn start_new_upload(
ctx: &ServiceContext<'_>,
UploadFile {
UploadNewFile {
site_id,
page_id,
name,
revision_comments,
user_id,
licensing,
bypass_filter,
}: UploadFile,
}: UploadNewFile,
) -> Result<UploadFileOutput> {
info!("Creating file with name '{}'", name);
let txn = ctx.transaction();

// Ensure row consistency
Self::check_conflicts(ctx, page_id, &name, "create").await?;
Expand All @@ -74,10 +77,9 @@ impl FileService {
..Default::default()
};

let txn = ctx.transaction();
let file = model.insert(txn).await?;

// Add new file revision (with dummy data)
// Add file revision (with dummy file data)
let revision_output = FileRevisionService::create_first(
ctx,
CreateFirstFileRevision {
Expand All @@ -98,17 +100,17 @@ impl FileService {
Ok(revision_output)
}

pub async fn finish_upload(
pub async fn finish_new_upload(
ctx: &ServiceContext<'_>,
FinishUploadFile {
FinishUploadNewFile {
site_id,
page_id,
file_id,
pending_blob_id,
}: FinishUploadFile,
}: FinishUploadNewFile,
) -> Result<FinishUploadFileOutput> {
info!(
"Finishing file upload with site ID {} page ID {} file ID {} pending ID {}",
"Finishing new file upload with site ID {} page ID {} file ID {} pending ID {}",
site_id, page_id, file_id, pending_blob_id,
);

Expand Down Expand Up @@ -157,8 +159,6 @@ impl FileService {
};
model.update(txn).await?;

File::delete_by_id(pending_blob_id).exec(txn).await?;

// Update file revision to add the uploaded data
let FinalizeBlobUploadOutput {
hash,
Expand All @@ -176,7 +176,82 @@ impl FileService {
Ok(FinishUploadFileOutput { created })
}

/// Edits a file, including the ability to upload a new version.
/// Edits a file, uploading a new file version.
pub async fn start_edit_upload(
ctx: &ServiceContext<'_>,
UploadFileEdit {
site_id,
page_id,
file_id,
user_id,
revision_comments,
}: UploadFileEdit,
) -> Result<_UploadFileEditOutput> {
info!("Uploading new version to file ID {file_id}");

let txn = ctx.transaction();
let last_revision =
FileRevisionService::get_latest(ctx, site_id, page_id, file_id).await?;

// Add pending file
let pending = BlobService::create_upload(ctx).await?;

// Add file revision (with dummy file data)
let revision_output = FileRevisionService::create(
ctx,
CreateFileRevision {
site_id,
page_id,
file_id,
user_id,
comments: revision_comments,
body: CreateFileRevisionBody {
blob: FileBlob {
s3_hash: EMPTY_BLOB_HASH,
mime_hint: str!(EMPTY_BLOB_MIME),
size_hint: 0,
},
..Default::default()
},
},
last_revision,
)
.await?;

Ok(revision_output)
}

pub async fn finish_edit_upload(
ctx: &ServiceContext<'_>,
FinishUploadFileEdit {
site_id,
page_id,
file_id,
pending_blob_id,
}: FinishUploadFileEdit,
) -> Result<_> {
info!(
"Finishing file edit upload with site ID {} page ID {} file ID {} pending ID {}",
site_id, page_id, file_id, pending_blob_id,
);

// Get latest file revision
// TODO

// Update file metadata
let model = file::ActiveModel {
file_id: Set(file_id),
updated_at: Set(Some(now())),
..Default::default()
};
model.update(txn).await?;

todo!()
}

/// Edits a file, creating a new revision.
///
/// Cannot be used to upload a new file version.
pub async fn edit(
ctx: &ServiceContext<'_>,
EditFile {
Expand Down Expand Up @@ -209,29 +284,6 @@ impl FileService {
}
}

// Upload to S3, get derived metadata
// FIXME upload new file revision
/*
let blob = match data {
ProvidedValue::Unset => ProvidedValue::Unset,
ProvidedValue::Set(bytes) => {
let FinalizeBlobUploadOutput {
hash,
mime,
size,
created: _,
} = BlobService::finalize_upload(ctx, &bytes).await?;
ProvidedValue::Set(FileBlob {
s3_hash: hash,
size_hint: size,
mime_hint: mime,
})
}
};
*/
let blob = ProvidedValue::Unset;

// Update file metadata
let model = file::ActiveModel {
file_id: Set(file_id),
Expand All @@ -251,7 +303,6 @@ impl FileService {
comments: revision_comments,
body: CreateFileRevisionBody {
name,
blob,
licensing,
..Default::default()
},
Expand Down
29 changes: 25 additions & 4 deletions deepwell/src/services/file/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use serde_json::Value as JsonValue;
use time::OffsetDateTime;

#[derive(Deserialize, Debug, Clone)]
pub struct UploadFile {
pub struct UploadNewFile {
pub site_id: i64,
pub page_id: i64,
pub name: String,
Expand All @@ -39,21 +39,42 @@ pub struct UploadFile {
pub bypass_filter: bool,
}

pub type UploadFileOutput = CreateFirstFileRevisionOutput;
// TODO
pub type UploadNewFileOutput = CreateFirstFileRevisionOutput;

#[derive(Deserialize, Debug, Clone)]
pub struct FinishUploadFile {
pub struct FinishUploadNewFile {
pub site_id: i64,
pub page_id: i64,
pub file_id: i64,
pub pending_blob_id: i64,
}

#[derive(Serialize, Debug, Copy, Clone)]
pub struct FinishUploadFileOutput {
pub struct FinishUploadNewFileOutput {
pub created: bool,
}

#[derive(Deserialize, Debug, Clone)]
pub struct UploadFileEdit {
pub site_id: i64,
pub page_id: i64,
pub file_id: i64,
pub user_id: i64,
pub revision_comments: String,
}

pub type UploadFileEditOutput = CreateFileRevisionOutput;

#[derive(Deserialize, Debug, Clone)]
pub struct FinishUploadFileEdit {
}

#[derive(Serialize, Debug, Clone)]
pub struct FinishUploadFileEditOutput {
// TODO
}

#[derive(Deserialize, Debug, Clone)]
pub struct GetFile<'a> {
pub site_id: i64,
Expand Down

0 comments on commit 1001d8d

Please sign in to comment.