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]] [[package]]
name = "mediarepo-daemon" name = "mediarepo-daemon"
version = "0.13.0" version = "0.13.1"
dependencies = [ dependencies = [
"console-subscriber", "console-subscriber",
"glob", "glob",

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

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

Loading…
Cancel
Save