First version of rust web server serving album.json
This commit is contained in:
parent
1c99396705
commit
e191b2a3ff
10
react-debug/src/App.js
vendored
10
react-debug/src/App.js
vendored
@ -12,7 +12,7 @@ class Album extends React.Component {
|
|||||||
}
|
}
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
let {album} = this.props;
|
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(res => res.json())
|
||||||
.then(
|
.then(
|
||||||
(result) => this.setState({media_items: result}),
|
(result) => this.setState({media_items: result}),
|
||||||
@ -30,7 +30,7 @@ class Album extends React.Component {
|
|||||||
// TODO(wathiede): use coverPhotoMediaItemId and fetch from a
|
// TODO(wathiede): use coverPhotoMediaItemId and fetch from a
|
||||||
// locally cached image.
|
// locally cached image.
|
||||||
return <figure key={ mi.id } className="figure">
|
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">
|
<figcaption className="figure-caption">
|
||||||
<a key={ mi.id } href={ mi.productUrl }>
|
<a key={ mi.id } href={ mi.productUrl }>
|
||||||
<p className="text-truncate">{ mi.filename}</p>
|
<p className="text-truncate">{ mi.filename}</p>
|
||||||
@ -53,7 +53,7 @@ class AlbumIndex extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
fetch(process.env.PUBLIC_URL + "/photosync/albums.json")
|
fetch(process.env.PUBLIC_URL + "/api/albums")
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(
|
.then(
|
||||||
(result) => this.setState({albums: result}),
|
(result) => this.setState({albums: result}),
|
||||||
@ -68,8 +68,8 @@ class AlbumIndex extends React.Component {
|
|||||||
console.log(albums);
|
console.log(albums);
|
||||||
return albums.map((a) => {
|
return albums.map((a) => {
|
||||||
let img = <img src="https://via.placeholder.com/256x128" className="mr-3" alt="unset"/>;
|
let img = <img src="https://via.placeholder.com/256x128" className="mr-3" alt="unset"/>;
|
||||||
if (a.coverPhotoBaseUrl !== undefined) {
|
if (a.coverPhotoMediaItemId !== undefined) {
|
||||||
img = <img src={ a.coverPhotoBaseUrl + "=w256" } className="mr-3" alt={ a.title }/>;
|
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">
|
let figure = <figure key={ a.id } className="figure">
|
||||||
|
|||||||
@ -53,6 +53,11 @@ impl Library {
|
|||||||
info!("saving {}", path.to_string_lossy());
|
info!("saving {}", path.to_string_lossy());
|
||||||
fs::write(path, j)
|
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(
|
pub fn download_image(
|
||||||
&self,
|
&self,
|
||||||
filename: &str,
|
filename: &str,
|
||||||
|
|||||||
@ -2,6 +2,7 @@ use std::error::Error;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use log::warn;
|
||||||
use prometheus::Encoder;
|
use prometheus::Encoder;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use warp;
|
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> {
|
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)]
|
#[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 lib = warp::any().map(move || lib.clone());
|
||||||
let index = warp::path::end().and_then(index);
|
let index = warp::path::end().and_then(index);
|
||||||
|
|
||||||
|
// TODO(wathiede): implement album index
|
||||||
let album = warp::path("album")
|
let album = warp::path("album")
|
||||||
.and(lib.clone())
|
.and(lib.clone())
|
||||||
.and(warp::path::param())
|
.and(warp::path::param())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user