First version of rust web server serving album.json

This commit is contained in:
Bill Thiede 2020-02-10 21:30:20 -08:00
parent 1c99396705
commit e191b2a3ff
3 changed files with 17 additions and 6 deletions

View File

@ -12,7 +12,7 @@ class Album extends React.Component {
}
componentDidMount() {
let {album} = this.props;
fetch(process.env.PUBLIC_URL + `/photosync/${album}/album.json`)
fetch(process.env.PUBLIC_URL + `/api/album/${album}`)
.then(res => res.json())
.then(
(result) => this.setState({media_items: result}),
@ -30,7 +30,7 @@ class Album extends React.Component {
// TODO(wathiede): use coverPhotoMediaItemId and fetch from a
// locally cached image.
return <figure key={ mi.id } className="figure">
<img height="256" width="256" src={ `/photosync/images/${mi.id}/${mi.filename}?thumb` } className="mr-3" alt={ mi.filename }/>
<img height="256" width="256" src={ `/api/image/${mi.id}?w=256&h=256` } className="mr-3" alt={ mi.filename }/>
<figcaption className="figure-caption">
<a key={ mi.id } href={ mi.productUrl }>
<p className="text-truncate">{ mi.filename}</p>
@ -53,7 +53,7 @@ class AlbumIndex extends React.Component {
};
}
componentDidMount() {
fetch(process.env.PUBLIC_URL + "/photosync/albums.json")
fetch(process.env.PUBLIC_URL + "/api/albums")
.then(res => res.json())
.then(
(result) => this.setState({albums: result}),
@ -68,8 +68,8 @@ class AlbumIndex extends React.Component {
console.log(albums);
return albums.map((a) => {
let img = <img src="https://via.placeholder.com/256x128" className="mr-3" alt="unset"/>;
if (a.coverPhotoBaseUrl !== undefined) {
img = <img src={ a.coverPhotoBaseUrl + "=w256" } className="mr-3" alt={ a.title }/>;
if (a.coverPhotoMediaItemId !== undefined) {
img = <img height="256" width="256" src={ `/api/image/${a.coverPhotoMediaItemId}?w=256&h=256` } className="mr-3" alt={ a.title }/>
}
let figure = <figure key={ a.id } className="figure">

View File

@ -53,6 +53,11 @@ impl Library {
info!("saving {}", path.to_string_lossy());
fs::write(path, j)
}
pub fn album(&self, album_id: &str) -> Result<Vec<MediaItem>, Box<dyn std::error::Error>> {
let album_path = self.root.join(album_id).join("album.json");
let bytes = fs::read(album_path)?;
Ok(serde_json::from_slice(&bytes)?)
}
pub fn download_image(
&self,
filename: &str,

View File

@ -2,6 +2,7 @@ use std::error::Error;
use std::net::SocketAddr;
use std::path::PathBuf;
use log::warn;
use prometheus::Encoder;
use serde::Deserialize;
use warp;
@ -34,7 +35,11 @@ fn index() -> Result<impl warp::Reply, warp::Rejection> {
}
fn album(lib: Library, id: String) -> Result<impl warp::Reply, warp::Rejection> {
Ok(format!("Hello world: {}", id))
let album = lib.album(&id).map_err(|e| {
warn!("Couldn't find album {}: {}", id, e);
warp::reject::not_found()
})?;
Ok(warp::reply::json(&album))
}
#[derive(Debug, Deserialize)]
@ -57,6 +62,7 @@ pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> {
let lib = warp::any().map(move || lib.clone());
let index = warp::path::end().and_then(index);
// TODO(wathiede): implement album index
let album = warp::path("album")
.and(lib.clone())
.and(warp::path::param())