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
|
use std::collections::HashSet;
use std::{borrow::Cow, collections::BTreeMap, error::Error, time::Duration};
use chrono::NaiveDate;
use futures::{future::ready, stream::iter, StreamExt, TryStreamExt};
use lofty::file::{AudioFile, TaggedFileExt};
use lofty::tag::Accessor;
use log::info;
use regex::Regex;
use render::Renderer;
use serde::{Deserialize, Serialize};
use str_slug::slug;
pub mod de;
mod render;
pub mod s3;
/// The Index tracks the state from the last successful exection.
#[derive(Default, Serialize, Deserialize)]
pub struct Index {
#[serde(default)]
entries: BTreeMap<String, Entry>,
#[serde(default)]
templates: BTreeMap<String, Template>,
#[serde(default)]
rendered: BTreeMap<String, HashSet<String>>,
}
impl Index {
pub fn contains_key(&self, key: &str) -> bool {
self.entries.contains_key(key)
}
pub fn add(&mut self, entry: Entry) {
self.entries.insert(entry.filename.to_owned(), entry);
}
pub fn remove(&mut self, filename: &str) {
self.entries.remove(filename);
}
}
/// Entry records the metadata about a file in the collection.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Entry {
pub filename: String,
pub date: Option<NaiveDate>,
pub etag: Option<String>,
pub title: Option<String>,
pub album: Option<String>,
pub artist: Option<String>,
pub size: i64,
#[serde(with = "de::hms_duration")]
pub duration: Duration,
pub hidden: bool,
}
impl Entry {
pub fn read_from(
key: String,
etag: Option<String>,
size: i64,
date: Option<NaiveDate>,
mut file: std::fs::File,
) -> Result<Entry, Box<dyn Error>> {
let tagged = lofty::read_from(&mut file)?;
let tag = tagged.primary_tag();
Ok(Entry {
filename: key.to_string(),
etag,
date,
title: tag.and_then(|t| t.title()).map(Cow::into_owned),
artist: tag.and_then(|t| t.artist()).map(Cow::into_owned),
album: tag.and_then(|t| t.album()).map(Cow::into_owned),
size,
duration: tagged.properties().duration(),
hidden: false,
})
}
}
/// Templates are used to render content for the feed.
///
/// `partial` templates are never rendered, but may be included in other templates.
/// `index` templates are rendered once for the entire collection.
/// Non-index templates are rendered once per entry.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Template {
pub name: String,
/// False if the template should be rendered for each entry.
/// True if the template renders the list of all entries.
#[serde(default)]
pub index: bool,
#[serde(default)]
pub partial: bool,
pub content_type: Option<String>,
#[serde(default)]
/// Only render this template for files that match this regex.
pub filter: Option<String>,
pub template: String,
}
impl Template {
fn make_filename(&self, entry: &Entry) -> String {
if self.index {
self.name.to_string()
} else {
let (basename, extension) = self
.name
.rsplit_once(".")
.unwrap_or_else(|| (&self.name, ".html"));
let title = if let Some(title) = &entry.title {
slug(title)
} else {
slug(
entry
.filename
.rsplit_once(".")
.unwrap_or_else(|| (&entry.filename, ""))
.0,
)
};
if let Some(ref date) = entry.date {
format!("{basename}/{date}-{title}.{extension}")
} else {
format!("{basename}/{title}.{extension}")
}
}
}
}
pub struct MP32RSS {
access: s3::Access,
index: Index,
index_etag: Option<String>,
}
impl MP32RSS {
pub async fn open(access: s3::Access) -> Result<Self, Box<dyn Error>> {
info!("Opening index file");
let (index, index_etag) = access.fetch_index().await?;
Ok(Self {
access,
index,
index_etag,
})
}
pub fn get_mut_entry(&mut self, filename: &str) -> Option<&mut Entry> {
self.index.entries.get_mut(filename)
}
pub async fn sync(&mut self) -> Result<(), Box<dyn Error>> {
info!("Saving index file");
let new_etag = self
.access
.put_index(&self.index, self.index_etag.as_deref())
.await?;
self.index_etag = new_etag;
Ok(())
}
pub async fn refresh(&mut self) -> Result<(), Box<dyn Error>> {
info!("Syncing against files in bucket");
// 2. List files in the bucket
let objects = self.access.list_mp3s().await?;
// 3. Find files missing in the index
// 4. For each missing file, download and add to local index
let new_entries: Vec<_> = iter(objects)
.filter(|k| ready(!self.index.contains_key(k)))
.then(|k| {
info!("Found new file: {k}...");
self.access.fetch_entry(k)
})
.try_collect()
.await?;
let mut new_filenames = HashSet::new();
for entry in new_entries {
new_filenames.insert(entry.filename.to_string());
self.index.add(entry);
}
// 5. Generate files for each template and upload
// 6. Upload each file
// Check the ETAG against the ETAG in the index object?
self.render(|e| new_filenames.contains(&e.filename)).await?;
// 7. Upload the index file
// If upload fails, abort (or retry from the beginning)
self.sync().await
}
pub async fn delete(&mut self, filename: &str) -> Result<(), Box<dyn Error>> {
self.index.remove(filename);
self.access.remove_file(filename).await?;
self.render(|e| e.filename == filename).await?;
self.sync().await
}
pub fn templates(&self) -> impl Iterator<Item = &Template> {
self.index.templates.values()
}
/// Add a new template and rerender.
pub async fn add_template(
&mut self,
name: String,
template: String,
index: bool,
partial: bool,
content_type: Option<String>,
filter: Option<String>,
) -> Result<(), Box<dyn Error>> {
self.index.templates.insert(
name.to_owned(),
Template {
name,
template,
index,
partial,
content_type,
filter,
},
);
self.render(|_| true).await?;
self.sync().await
}
/// Add a new template and rerender.
pub async fn delete_template(&mut self, name: &str) -> Result<(), Box<dyn Error>> {
self.index.templates.remove(name);
self.render(|_| true).await?;
self.sync().await
}
/// Render all templates.
///
// Filter limits rerendering to entries which return true.
pub async fn render<F>(&mut self, filter: F) -> Result<(), Box<dyn Error>>
where
F: Fn(&Entry) -> bool,
{
let empty_set = HashSet::new();
let mut renderer = Renderer::new(self.index.templates.values())?;
for template in self.index.templates.values() {
let existing_files = self
.index
.rendered
.get(&template.name)
.unwrap_or(&empty_set);
let mut new_files = HashSet::new();
if !template.partial {
info!("Rendering template {}", template.name);
let content_type = template.content_type.as_deref().unwrap_or("text/html");
let file_regex = Regex::new(template.filter.as_deref().unwrap_or(".*"))?;
let entries: Vec<_> = self
.index
.entries
.values()
.filter(|e| !e.hidden && file_regex.is_match(&e.filename))
.cloned()
.collect();
renderer.set_entries(entries.clone());
if template.index {
let filename = template.name.to_string();
let data = renderer.render_index(&template.name, &filename)?;
self.access.put_file(&filename, content_type, data).await?;
new_files.insert(filename);
} else {
// Generate a file for each entry.
for entry in &entries {
let filename = template.make_filename(entry);
if filter(entry) {
let data = renderer.render_entry(&template.name, &filename, entry)?;
self.access.put_file(&filename, content_type, data).await?;
new_files.insert(filename);
} else if existing_files.contains(&filename) {
new_files.insert(filename);
}
}
}
}
// Remove any orphaned files
for filename in existing_files {
if !new_files.contains(filename) {
info!("Removing rendered file {}", filename);
self.access.remove_file(filename).await?;
}
}
// Update the list of rendered files.
self.index
.rendered
.insert(template.name.to_string(), new_files);
}
// Remove renderings of any deleted templates
for (template, files) in &self.index.rendered {
if !self.index.templates.contains_key(template) {
info!("Removing all rendered files for template {}", template);
for filename in files {
self.access.remove_file(filename).await?;
}
}
}
self.index
.rendered
.retain(|k, _| self.index.templates.contains_key(k));
self.sync().await
}
}
|