ignite-core 0.7.3

Core types for Ignite, the admin panel for Rocket.rs: resources, fields, forms and runtime resources.
Documentation
//! The media library that [`Image`](crate::FieldKind::Image),
//! [`File`](crate::FieldKind::File) and [`Gallery`](crate::FieldKind::Gallery) fields pick
//! from.
//!
//! Ignite assumes no storage, table or upload path: the app implements [`MediaLibrary`]
//! and registers it with [`Registries::register_media_library`], as Laravel binds an implementation to
//! a contract. A media field stores item ids. With a library registered, the panel shows
//! the stored items and refuses an id the library does not have. With none, the fields
//! still render, and their ids are checked as positive whole numbers only.
//!
//! ```ignore
//! registries.register_media_library(Attachments);
//! ```

use std::sync::{Arc, PoisonError};

use crate::registries::Registries;

use orbit_orm::Database;
use serde::Serialize;

use crate::RuntimeFuture;

/// One item of the media library, as a form shows it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MediaItem {
    /// The item's id, as a media field stores it.
    pub id: i64,
    /// The item's title, shown next to it.
    pub title: String,
    /// A URL that shows the item: the image itself, or the file to download.
    pub url: String,
}

/// The app's media library. Implement it and add it with [`Registries::register_media_library`].
pub trait MediaLibrary: Send + Sync + 'static {
    /// The items among `ids` that exist, in any order. An id with no item is left out.
    fn find<'a>(&'a self, db: &'a Database, ids: &'a [i64]) -> RuntimeFuture<'a, Vec<MediaItem>>;

    /// The URL of the page where a user browses and uploads media, which the form links to
    /// so the user can find an item's id. The default is none.
    fn browse_url(&self) -> Option<String> {
        None
    }
}

impl Registries {
    /// Sets the media library that media fields use, from the next request on. Registering
    /// again replaces it.
    pub fn register_media_library(&self, library: impl MediaLibrary) {
        *self
            .inner
            .media
            .write()
            .unwrap_or_else(PoisonError::into_inner) = Some(Arc::new(library));
    }

    /// The registered media library, if the app registered one.
    pub fn media_library(&self) -> Option<Arc<dyn MediaLibrary>> {
        self.inner
            .media
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
    }
}

/// Sets the media library of the default app, as 0.7.1 did. See
/// [`Registries::register_media_library`](crate::Registries::register_media_library).
pub fn register_media_library(library: impl MediaLibrary) {
    Registries::default_app().register_media_library(library);
}

/// The media library of the default app. See
/// [`Registries::media_library`](crate::Registries::media_library).
pub fn media_library() -> Option<Arc<dyn MediaLibrary>> {
    Registries::default_app().media_library()
}