upub_cli/
attachments.rs

1use futures::TryStreamExt;
2use sea_orm::{ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, TransactionTrait};
3
4pub async fn fix_attachments_types(ctx: upub::Context) -> Result<(), Box<dyn std::error::Error>> {
5	tracing::info!("fixing attachments documentType...");
6	let tx = ctx.db().begin().await?;
7
8	let mut stream = upub::model::attachment::Entity::find()
9		.filter(upub::model::attachment::Column::DocumentType.eq(apb::DocumentType::Document))
10		.stream(ctx.db())
11		.await?;
12
13	while let Some(attachment) = stream.try_next().await? {
14		let Some((mime_kind, _mime_type)) = attachment.media_type.split_once('/') else { continue };
15
16		let document_type = match mime_kind {
17			"image" => apb::DocumentType::Image,
18			"video" => apb::DocumentType::Video,
19			"audio" => apb::DocumentType::Audio,
20			"text"  => apb::DocumentType::Page,
21			_ => continue,
22		};
23
24		tracing::info!("updating {} to {document_type}", attachment.url);
25		let mut active = attachment.into_active_model();
26		active.document_type = Set(document_type);
27		active.update(&tx).await?;
28	}
29
30	tracing::info!("committing transaction...");
31	tx.commit().await?;
32	tracing::info!("done");
33
34	Ok(())
35}