|
|
@ -1,4 +1,6 @@
|
|
|
|
use crate::types::requests::{AddFileRequest, GetFileThumbnailsRequest, ReadFileRequest};
|
|
|
|
use crate::types::requests::{
|
|
|
|
|
|
|
|
AddFileRequest, FindFilesByTagsRequest, GetFileThumbnailsRequest, ReadFileRequest,
|
|
|
|
|
|
|
|
};
|
|
|
|
use crate::types::responses::{FileResponse, ThumbnailResponse};
|
|
|
|
use crate::types::responses::{FileResponse, ThumbnailResponse};
|
|
|
|
use crate::utils::{file_by_identifier, get_repo_from_context};
|
|
|
|
use crate::utils::{file_by_identifier, get_repo_from_context};
|
|
|
|
use mediarepo_core::error::RepoError;
|
|
|
|
use mediarepo_core::error::RepoError;
|
|
|
@ -16,6 +18,7 @@ impl NamespaceProvider for FilesNamespace {
|
|
|
|
fn register(handler: &mut EventHandler) {
|
|
|
|
fn register(handler: &mut EventHandler) {
|
|
|
|
events!(handler,
|
|
|
|
events!(handler,
|
|
|
|
"all_files" => Self::all_files,
|
|
|
|
"all_files" => Self::all_files,
|
|
|
|
|
|
|
|
"find_files" => Self::find_files,
|
|
|
|
"add_file" => Self::add_file,
|
|
|
|
"add_file" => Self::add_file,
|
|
|
|
"read_file" => Self::read_file,
|
|
|
|
"read_file" => Self::read_file,
|
|
|
|
"get_thumbnails" => Self::thumbnails,
|
|
|
|
"get_thumbnails" => Self::thumbnails,
|
|
|
@ -38,6 +41,18 @@ impl FilesNamespace {
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Searches for files by tags
|
|
|
|
|
|
|
|
async fn find_files(ctx: &Context, event: Event) -> IPCResult<()> {
|
|
|
|
|
|
|
|
let tags = event.data::<FindFilesByTagsRequest>()?;
|
|
|
|
|
|
|
|
let repo = get_repo_from_context(ctx).await;
|
|
|
|
|
|
|
|
let files = repo.find_files_by_tags(tags.tags).await?;
|
|
|
|
|
|
|
|
let responses: Vec<FileResponse> = files.into_iter().map(FileResponse::from).collect();
|
|
|
|
|
|
|
|
ctx.emitter
|
|
|
|
|
|
|
|
.emit_response_to(event.id(), Self::name(), "find_files", responses)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Adds a file to the repository
|
|
|
|
/// Adds a file to the repository
|
|
|
|
async fn add_file(ctx: &Context, event: Event) -> IPCResult<()> {
|
|
|
|
async fn add_file(ctx: &Context, event: Event) -> IPCResult<()> {
|
|
|
|
let request = event.data::<AddFileRequest>()?;
|
|
|
|
let request = event.data::<AddFileRequest>()?;
|
|
|
@ -68,7 +83,7 @@ impl FilesNamespace {
|
|
|
|
reader.read_to_end(&mut buf).await?;
|
|
|
|
reader.read_to_end(&mut buf).await?;
|
|
|
|
|
|
|
|
|
|
|
|
ctx.emitter
|
|
|
|
ctx.emitter
|
|
|
|
.emit_response_to(event.id(), Self::name(), "read_file", buf)
|
|
|
|
.emit_response_to(event.id(), Self::name(), "read_file", BytePayload::new(buf))
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
@ -104,7 +119,12 @@ impl FilesNamespace {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
reader.read_to_end(&mut buf).await?;
|
|
|
|
reader.read_to_end(&mut buf).await?;
|
|
|
|
ctx.emitter
|
|
|
|
ctx.emitter
|
|
|
|
.emit_response_to(event.id(), Self::name(), "read_thumbnail", buf)
|
|
|
|
.emit_response_to(
|
|
|
|
|
|
|
|
event.id(),
|
|
|
|
|
|
|
|
Self::name(),
|
|
|
|
|
|
|
|
"read_thumbnail",
|
|
|
|
|
|
|
|
BytePayload::new(buf),
|
|
|
|
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|