dailp/
iiif.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#![allow(missing_docs)]

//! This module includes types which are intended to represent the [IIIF
//! Presentation API specification](https://iiif.io/api/presentation/3.0/).
//! We use these types to build IIIF manifests for any annotated document,
//! allowing any IIIF image viewer to consume and properly display our content.

use crate::{
    annotation::{AnnotationAttachment, DocumentRegion},
    AnnotatedDoc, Database,
};
use futures::join;
use futures::stream::{self, StreamExt};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// [IIIF manifest](https://iiif.io/api/presentation/3.0/#52-manifest) which
/// represents a particular annotated document, containing all original images
/// and current annotations on each image.
#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct Manifest {
    #[serde(rename = "@context")]
    context: String,
    id: String,
    label: LanguageString,
    summary: LanguageString,
    metadata: Vec<MetadataEntry>,
    required_statement: MetadataEntry,
    behavior: Vec<String>,
    provider: Vec<Agent>,
    homepage: Vec<Text>,
    items: Vec<Canvas>,
}
impl Manifest {
    /// Make a IIIF manifest from the given document
    pub async fn from_document(db: &Database, doc: AnnotatedDoc, manifest_uri: String) -> Self {
        let page_images = doc.meta.page_images.unwrap();
        let (
            image_source, // , _annotations
            words,
        ) = {
            // let annot_db = db.annotations();
            join!(
                db.image_source_by_id(page_images.source),
                // annot_db.on_document(&doc.meta.id),
                db.words_in_document(doc.meta.id, None, None)
            )
        };
        let annotations: Vec<crate::annotation::Annotation> = Vec::new();
        let words: Vec<_> = words.unwrap().collect();
        let words = &words;
        let annotations = &annotations;
        let image_source = &image_source.unwrap().unwrap();
        let manifest_uri = &manifest_uri;
        Self::new(
            manifest_uri.clone(),
            doc.meta.title,
            "The Newberry Library".to_owned(),
            stream::iter(page_images.ids.into_iter().enumerate())
                .then(|(index, id)| async move {
                    let image_url = format!("{}/{}", image_source.url, id);
                    let info_url = format!("{}/info.json", image_url);
                    let info = reqwest::get(info_url)
                        .await
                        .unwrap()
                        .json::<ImageInfo>()
                        .await
                        .unwrap();
                    let page_num = (index + 1) as u32;
                    let page_uri = format!("{}/page/{}", manifest_uri, page_num);
                    let canvas_uri = format!("{}/canvas", page_uri);
                    let annotations_uri = format!("{}/annotations", page_uri);
                    let annotation_page = AnnotationPage {
                        items: words
                            .iter()
                            .filter_map(|word| {
                                word.position.geometry.as_ref().map(|geometry| Annotation {
                                    id: format!("{}/{:?}", annotations_uri, word.id),
                                    motivation: "supplementing".to_owned(),
                                    body: AnnotationBody::TextualBody(TextualBody {
                                        language: "en".to_string(),
                                        format: "text/html".to_string(),
                                        value: word.source.clone(),
                                    }),
                                    target: AnnotationTarget::Selector(TargetSelector {
                                        id: canvas_uri.clone(),
                                        selector: FragmentSelector {
                                            value: geometry.to_selector_string(),
                                        },
                                    }),
                                })
                            })
                            .chain(annotations.iter().filter_map(
                                |annote| match &annote.attached_to {
                                    AnnotationAttachment::DocumentRegion(DocumentRegion {
                                        region,
                                        page: Some(annote_page),
                                        ..
                                    }) if *annote_page == page_num => Some(Annotation {
                                        id: format!("{}/{}", annotations_uri, annote.id.0),
                                        motivation: "commenting".to_owned(),
                                        body: AnnotationBody::TextualBody(TextualBody {
                                            language: "en".to_string(),
                                            format: "text/html".to_string(),
                                            value: annote.content.clone(),
                                        }),
                                        target: if let Some(region) = &region {
                                            AnnotationTarget::Selector(TargetSelector {
                                                id: canvas_uri.clone(),
                                                selector: FragmentSelector {
                                                    value: region.to_selector_string(),
                                                },
                                            })
                                        } else {
                                            AnnotationTarget::Id(canvas_uri.clone())
                                        },
                                    }),

                                    _ => None,
                                },
                            ))
                            .collect(),
                        id: annotations_uri,
                    };
                    // For each page, retrieve the size of the image to set the canvas size.
                    Canvas {
                        label: LanguageString::english(&format!("Page {}", page_num)),
                        height: info.height,
                        width: info.width,
                        items: vec![AnnotationPage {
                            items: vec![Annotation {
                                id: format!("{}/image", page_uri),
                                motivation: "painting".to_owned(),
                                body: AnnotationBody::Image(Image {
                                    id: format!("{}/full/max/0/default.jpg", image_url),
                                    width: info.width,
                                    height: info.height,
                                    format: "image/jpeg".to_owned(),
                                    service: vec![ImageService2 {
                                        id: image_url,
                                        profile: "level1".to_owned(),
                                    }],
                                }),
                                target: AnnotationTarget::Id(canvas_uri.clone()),
                            }],
                            id: page_uri,
                        }],
                        annotations: vec![annotation_page],
                        id: canvas_uri,
                    }
                })
                .collect()
                .await,
        )
    }

    /// Make a IIIF manifest
    pub fn new(uri: String, title: String, attribution: String, items: Vec<Canvas>) -> Self {
        Self {
            context: "http://iiif.io/api/presentation/3/context.json".to_owned(),
            id: uri,
            label: LanguageString::english(&title),
            summary: LanguageString::english(&title),
            metadata: vec![],
            required_statement: MetadataEntry {
                label: LanguageString::english("Attribution"),
                value: LanguageString::english(&attribution),
            },
            behavior: vec!["paged".to_owned()],
            provider: vec![Agent::neu_library()],
            homepage: vec![Text {
                id: "https://dailp.northeastern.edu/".to_owned(),
                label: LanguageString::english(
                    "Digital Archive of Indigenous Language Persistence",
                ),
                format: "text/html".to_owned(),
            }],
            items,
        }
    }
}

/// Basic image information including dimensions.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
struct ImageInfo {
    width: u32,
    height: u32,
}

/// A creative agent, which may be publisher or editor of manifest content.
#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct Agent {
    id: String,
    label: LanguageString,
    homepage: Vec<Text>,
}
impl Agent {
    /// [`Agent`] object representing Northeastern University Library
    pub fn neu_library() -> Self {
        Self {
            id: "https://library.northeastern.edu/".to_owned(),
            label: LanguageString::english("Northeastern University Library"),
            homepage: vec![Text {
                id: "https://library.northeastern.edu".to_owned(),
                label: LanguageString::english("Northeastern University Library"),
                format: "text/html".to_owned(),
            }],
        }
    }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct Text {
    id: String,
    label: LanguageString,
    format: String,
}

/// A string which may have many different translations into several languages.
#[derive(Serialize)]
#[serde(transparent)]
pub struct LanguageString(HashMap<String, Vec<String>>);
impl LanguageString {
    /// Make an English-only string
    pub fn english(s: &str) -> Self {
        let mut h = HashMap::new();
        h.insert("en".to_owned(), vec![s.to_owned()]);
        Self(h)
    }
}

/// Generic metadata entry on a manifest, page, or annotation.
#[derive(Serialize)]
pub struct MetadataEntry {
    label: LanguageString,
    value: LanguageString,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct Canvas {
    id: String,
    label: LanguageString,
    height: u32,
    width: u32,
    items: Vec<AnnotationPage>,
    annotations: Vec<AnnotationPage>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct AnnotationPage {
    id: String,
    items: Vec<Annotation>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct Annotation {
    id: String,
    motivation: String,
    body: AnnotationBody,
    target: AnnotationTarget,
}

#[derive(Serialize)]
#[serde(untagged)]
pub enum AnnotationTarget {
    Id(String),
    Selector(TargetSelector),
}

#[derive(Serialize)]
pub struct TargetSelector {
    id: String,
    selector: FragmentSelector,
}

#[derive(Serialize)]
#[serde(tag = "type")]
pub struct FragmentSelector {
    value: String,
}

#[derive(Serialize)]
#[serde(untagged)]
pub enum AnnotationBody {
    Image(Image),
    TextualBody(TextualBody),
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct Image {
    id: String,
    format: String,
    height: u32,
    width: u32,
    service: Vec<ImageService2>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct ImageService2 {
    id: String,
    profile: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct TextualBody {
    language: String,
    format: String,
    value: String,
}