Add duplicate check on import

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/5/head
trivernis 3 years ago
parent 8d5253fdad
commit ea14222e44
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1219,7 +1219,7 @@ dependencies = [
[[package]]
name = "mediarepo-daemon"
version = "0.13.0"
version = "0.13.1"
dependencies = [
"console-subscriber",
"glob",

@ -217,11 +217,7 @@ impl File {
JoinType::LeftJoin,
content_descriptor_tag::Relation::Tag.def().rev(),
)
.join(
JoinType::InnerJoin,
content_descriptor_tag::Relation::ContentDescriptorId.def(),
)
.filter(content_descriptor::Column::Id.eq(self.content_descriptor.id))
.filter(content_descriptor_tag::Column::CdId.eq(self.content_descriptor.id))
.all(&self.db)
.await?;
let tags = tags
@ -251,16 +247,21 @@ impl File {
return Ok(());
}
let cd_id = self.content_descriptor.id;
let own_tag_ids = self.tags().await?.into_iter().map(|t| t.id()).collect::<Vec<i64>>();
let models: Vec<content_descriptor_tag::ActiveModel> = tag_ids
.into_iter()
.filter(|tag_id|!own_tag_ids.contains(tag_id))
.map(|tag_id| content_descriptor_tag::ActiveModel {
cd_id: Set(cd_id),
tag_id: Set(tag_id),
})
.collect();
content_descriptor_tag::Entity::insert_many(models)
.exec(&self.db)
.await?;
if models.len() > 0 {
content_descriptor_tag::Entity::insert_many(models)
.exec(&self.db)
.await?;
}
Ok(())
}

@ -18,6 +18,7 @@ use mediarepo_core::mediarepo_api::types::identifier::FileIdentifier;
use mediarepo_core::thumbnailer::ThumbnailSize;
use mediarepo_core::utils::parse_namespace_and_tag;
use tokio::io::AsyncReadExt;
use mediarepo_core::content_descriptor::create_content_descriptor;
pub struct FilesNamespace;
@ -135,15 +136,22 @@ impl FilesNamespace {
.into_inner();
let AddFileRequestHeader { metadata, tags } = request;
let repo = get_repo_from_context(ctx).await;
let bytes = bytes.into_inner();
let cd = create_content_descriptor(&bytes);
let file = repo
.add_file(
metadata.mime_type,
bytes.into_inner(),
metadata.creation_time,
metadata.change_time,
)
.await?;
let file = if let Some(file) = repo.file_by_cd(&cd).await? {
tracing::debug!("Inserted file already exists");
file
} else {
repo
.add_file(
metadata.mime_type,
bytes,
metadata.creation_time,
metadata.change_time,
)
.await?
};
file.metadata().await?.set_name(metadata.name).await?;
let tags = repo

Loading…
Cancel
Save