Compare commits

...

84 Commits

Author SHA1 Message Date
8633e9a6cc More cacheable Dockerfile. 2020-06-24 21:11:40 -07:00
f8d0da1af4 Latest lock. 2020-06-24 21:06:10 -07:00
625af91c20 Pin google_api_auth to known working version. 2020-06-23 21:15:10 -07:00
7cc0af4f07 Use supported nodejs and nightly rust compiler (required for rocket). 2020-06-22 21:12:42 -07:00
7d3b38af12 Version bumps. 2020-06-22 21:12:16 -07:00
06f82cc160 Remove unused params. 2020-06-22 21:12:07 -07:00
1bca4c3642 Rewrite to use s3 instead of local files and rocksdb. 2020-06-22 20:48:15 -07:00
337f5dfd49 Remove unnecessary Rocket.toml 2020-06-22 20:19:07 -07:00
e7b29509e5 Use cache for thumbnail too. 2020-06-20 22:40:08 -07:00
58064a6309 Use cacher for downloading fullsize images. 2020-06-20 22:25:29 -07:00
3402d7bcf4 Clean lint. 2020-06-20 20:08:37 -07:00
009dd1ff19 Remove deprecated file. 2020-06-20 20:05:35 -07:00
639a1c7b3a Set default handler to high rank to prevent collision. 2020-06-20 17:08:26 -07:00
76594dc0c1 Rocket based webserver now has parity with warp version. 2020-06-20 14:36:00 -07:00
80ef93f20f Start of rewrite from warp->rocket. 2020-06-20 12:50:22 -07:00
a19874fe47 Attempt to create a cacher but I can't make it work with warp. 2020-06-19 20:37:51 -07:00
4fc0bc9d01 react-slideshow: cleaner looking front page. 2020-06-17 18:11:58 -07:00
7e2cf1b956 Move image helpers to xinu.tv/imageutils 2020-04-01 18:21:22 -07:00
b61e65bd83 Pair-up portraits when in landscape mode. 2020-03-14 22:02:35 -07:00
0799708109 Fix build. 2020-03-14 20:29:15 -07:00
24240c5f68 Use day of month instead of day of week when seeding shuffle. 2020-03-14 16:30:44 -07:00
f683acf0ae Build react-slideshow when building photosync into a docker image. 2020-03-14 15:08:36 -07:00
f14dbff066 Prettier /embedz, now with sizes. 2020-03-14 15:07:58 -07:00
ba304c85c3 Remove unused logos. 2020-03-14 14:29:05 -07:00
bd3aac5bc0 Add /embedz handler to see what files are in binary. 2020-03-14 14:26:12 -07:00
737b290cc0 Docker build for slideshow app. 2020-03-14 13:37:02 -07:00
2ee2a98c7d Set production slide time to 1 minute (matches Google hub). 2020-03-14 13:36:25 -07:00
a96fe1da9d Implement ServeAndSync. 2020-03-10 18:38:45 -07:00
89037b6b24 Prefetch images for next slide. 2020-03-08 11:56:07 -07:00
9e4fdf7644 Fix UI after refactor. 2020-02-26 21:23:32 -08:00
e3182d4cf2 Refactor image handling in preparation of multi-image slides. 2020-02-26 21:17:38 -08:00
9f9c3cc00c Version bump target so newer features can be used.
This allows usage of #private members.
2020-02-26 21:17:00 -08:00
0dc3c5edef Cleanup page metadata. 2020-02-26 14:19:40 -08:00
62ae230f70 Remove TODOs, and set favicon. 2020-02-26 13:51:30 -08:00
49695dd393 Preserve aspect ratio in debug thumbnails. 2020-02-26 09:21:07 -08:00
669a129f21 Convert main App to typescript. 2020-02-26 08:50:53 -08:00
ea8f15ab23 Teeny tiny refactor. 2020-02-25 17:39:50 -08:00
407358bc43 More robust timer for autoadvancing image. 2020-02-25 16:31:52 -08:00
01c7582ae3 Implement timeout to automatically advance slide. 2020-02-23 09:00:22 -08:00
7ec3a11037 Use non-filling resize on server side. Client does filling. 2020-02-23 08:46:20 -08:00
792471bf00 Shuffle slideshow, only show images in the same orientation as screen. 2020-02-23 08:45:06 -08:00
d0ce89b27c Comment out verbose logging. 2020-02-22 21:36:12 -08:00
1b292b30c9 Don't log on cache hit. 2020-02-22 21:32:17 -08:00
233d0c4883 Use Lanczos3 when generating thumbnails. 2020-02-22 17:49:35 -08:00
1c82313c98 Benchmark more jpeg decoders. Do downsizing decode with jpeg_decoder. 2020-02-22 17:19:57 -08:00
7f64a4d2f6 Implement less abstracted nearest filtering.
Add some tests that try to compare image's Nearest with my
implementation.  According the PSNR they're very different, but to a
human the look the same.
2020-02-22 09:38:47 -08:00
77d69221d1 Parameterized tests. Include pixels/s. 2020-02-21 13:50:26 -08:00
df28512450 Refactor for testing. 2020-02-20 20:19:27 -08:00
b552b922fa Finish implementation of option parameter resize. 2020-02-19 21:30:12 -08:00
ecca57f489 Basic slideshow with forward and backward navigation. 2020-02-19 21:20:15 -08:00
f2f5279266 Handle unset size parameters, print # gc'd entries on startup. 2020-02-19 21:19:40 -08:00
ab1a63da1e Ran: npx create-react-app react-slideshow 2020-02-17 17:54:22 -08:00
d74a8117d7 Refactor thumbnail generation. 2020-02-17 17:50:43 -08:00
43974b7406 Properly resize images, bump library generation to match. 2020-02-17 14:57:48 -08:00
b801954599 More efficient garbage collection of cache.
Iterates backwards from first entry of this generation, and forward from
last entry.
2020-02-17 14:54:39 -08:00
914e30365e Store thumbnails in rocksdb. 2020-02-17 14:50:09 -08:00
d07542b544 Do force resize in img tag, assume serve is sending the right size. 2020-02-17 14:49:35 -08:00
d4df74a799 Clean up lint. 2020-02-16 09:03:55 -08:00
12b5193b26 Add static serving from assets built into binary. 2020-02-15 23:09:01 -08:00
4bb2354fdf Implement album index and image resize. 2020-02-15 08:12:13 -08:00
8abeef8859 Add routing to albums. 2020-02-15 08:09:12 -08:00
e191b2a3ff First version of rust web server serving album.json 2020-02-15 07:54:37 -08:00
1c99396705 Move common code to lib module. More stub webservice. 2020-02-10 21:13:46 -08:00
5c0f409f03 Minor logging changes. 2020-02-10 21:03:04 -08:00
c4deab996b Move file creation code to library module. 2020-02-10 20:59:52 -08:00
48da92e278 Move flag requirements for credentials to subcommands that need it. 2020-02-09 22:05:48 -08:00
88234c156c Delegate stub webserver to mod web.
Add prometheus monitoring.
2020-02-09 21:56:41 -08:00
c71af2dc90 react: proxy unknown URLs to localhost:4000 2020-02-09 21:55:59 -08:00
43baeb71ad Address lint. 2020-02-09 21:46:58 -08:00
8aeee408b0 Remove symlink from git. 2020-02-09 21:46:45 -08:00
2dd5bb1d43 Stub websever, don't store to API given filename (sometimes they have /) 2020-02-09 21:46:05 -08:00
93af9725df Commit react app lock file. 2020-02-09 21:26:28 -08:00
249db6500e sync: download fullsize images. 2020-02-09 21:26:03 -08:00
991b5a1469 Get images from new /images/ path. 2020-02-09 21:25:40 -08:00
abfd04f340 Use local fullsize images when rendering album. 2020-02-09 21:18:20 -08:00
dbb282d73c Use git instead of local path for google-photoslibrary1. 2020-02-09 15:35:45 -08:00
df3d29ce7c Write out images in per-album json. 2020-02-09 12:34:53 -08:00
cced612139 Ran cargo update. 2020-02-09 10:51:14 -08:00
3ad5c4f706 Merge branch 'master' of https://git.z.xinu.tv/wathiede/photosync 2020-02-09 10:49:16 -08:00
50c40c56db Made main buildable again. 2020-02-09 09:03:47 -08:00
bdf23a6e48 Render album or index based on the presence of a hash in the URL. 2020-02-09 09:00:13 -08:00
b08e9616bf Write out per album.json 2020-02-09 08:59:59 -08:00
32b47d44ec Merge branch 'master' of ggriffiniii/photosync into master 2020-02-09 16:59:13 +00:00
2a2bb0c2a3 Ignore symlink'd dir that regenerates on sync. 2020-02-07 19:54:29 -08:00
37 changed files with 13444 additions and 202 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
Dockerfile
target
*/node_modules
*/yarn.lock

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
auth/
react-debug/public/photosync/

2084
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,12 +7,49 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
yup-oauth2 = "^3.1"
google_api_auth = { git = "https://github.com/google-apis-rs/generator", features = ["with-yup-oauth2"] }
# TODO, use https://git.z.xinu.tv/wathiede/google-api-photoslibrary and figure out auth story.
google-photoslibrary1 = { path = "../google-api-photoslibrary" }
structopt = "0.3.9"
regex = "1.3.4"
google-photoslibrary1 = { git = "https://git.z.xinu.tv/wathiede/google-api-photoslibrary" }
google_api_auth = { git = "https://github.com/google-apis-rs/generator", rev="7504e31", features = ["with-yup-oauth2"] }
hexihasher = { git = "https://git.z.xinu.tv/wathiede/hexihasher" }
lazy_static = "1.4.0"
log = "0.4.8"
stderrlog = "0.4.3"
regex = "1.3.4"
reqwest = { version = "0.10.1", features = ["blocking"] }
serde_json = "1.0.46"
stderrlog = "0.4.3"
structopt = "0.3.9"
yup-oauth2 = "^3.1"
serde = { version = "1.0.104", features = ["derive"] }
image = { version = "0.23.2" } #, default-features = false, features = ["jpeg"] }
rust-embed = "5.2.0"
mime_guess = "2.0.1"
jpeg-decoder = "0.1.18"
imageutils = { git = "https://git.z.xinu.tv/wathiede/imageutils" }
cacher = { git = "https://git.z.xinu.tv/wathiede/cacher" }
rocket = "0.4.5"
thiserror = "1.0.20"
rusoto_s3 = "0.42.0"
rusoto_core = "0.42.0"
[dependencies.prometheus]
features = ["process"]
version = "0.7.0"
[dev-dependencies]
tempdir = "0.3.7"
criterion = "0.3"
stb_image = "0.2.2"
load_image = "2.12.0"
[[bench]]
name = "image"
harness = false
# Build dependencies with release optimizations even in dev mode.
[profile.dev.package."*"]
opt-level = 3
[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["json"]

17
Dockerfile Normal file
View File

@ -0,0 +1,17 @@
FROM rustlang/rust:nightly AS build-env
COPY ./dockerfiles/netrc /root/.netrc
RUN mkdir /root/.cargo
COPY ./dockerfiles/cargo-config /.cargo/config
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get update && apt-get install -y strace build-essential clang nodejs yarn
COPY ./ /src/
WORKDIR /src/react-slideshow
RUN yarn install
RUN yarn build
WORKDIR /src
RUN cargo version && cargo install --path .
FROM rust:slim
COPY --from=build-env /usr/local/cargo/bin/photosync /usr/bin/

120
benches/image.rs Normal file
View File

@ -0,0 +1,120 @@
use criterion::BenchmarkId;
use criterion::Throughput;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use image::imageops;
use image::GenericImageView;
use load_image as load_image_crate;
use stb_image::image::load as stb_load;
use stb_image::image::LoadResult;
use photosync::library::load_image;
use photosync::library::resize;
use photosync::library::save_to_jpeg_bytes;
use photosync::library::FilterType;
pub fn criterion_benchmark(c: &mut Criterion) {
const TEST_IMAGE_PATH: &'static str = "testdata/image.jpg";
let img = load_image(TEST_IMAGE_PATH, None, None).expect("failed to load test image");
c.bench_function("Load image", |b| {
b.iter(|| black_box(load_image(TEST_IMAGE_PATH, None, None)))
});
c.bench_function("Load image 256x256", |b| {
b.iter(|| black_box(load_image(TEST_IMAGE_PATH, Some(256), Some(256))))
});
c.bench_function("Load load_image", |b| {
b.iter(|| {
black_box(load_image_crate::load_image(TEST_IMAGE_PATH, true).expect("failed to load"))
})
});
c.bench_function("Load stb_image", |b| {
b.iter(|| match stb_load(TEST_IMAGE_PATH) {
LoadResult::Error(err) => panic!(err),
LoadResult::ImageU8(img) => {
black_box(img);
}
LoadResult::ImageF32(img) => {
black_box(img);
}
})
});
let mut group = c.benchmark_group("Resizing");
for size in [
(None, None),
(Some(256), Some(256)),
(Some(512), Some(512)),
(Some(1024), Some(1024)),
(Some(2048), Some(2048)),
]
.iter()
{
let (w, h) = size;
for filter in [
FilterType::Builtin(imageops::Nearest),
FilterType::Builtin(imageops::CatmullRom),
FilterType::Builtin(imageops::Lanczos3),
FilterType::Nearest,
]
.iter()
{
let (img_w, img_h) = img.dimensions();
let pixels = (img_w * img_h) as u64;
group.throughput(Throughput::Elements(pixels));
group.bench_with_input(
BenchmarkId::new(
format!("{:?}", filter),
format!(
"{}x{}",
w.map(|i| i.to_string()).unwrap_or("FULL".to_string()),
h.map(|i| i.to_string()).unwrap_or("FULL".to_string())
),
),
&(size, filter),
|b, (size, &filter)| b.iter(|| black_box(resize(&img, **size, filter))),
);
}
group.bench_with_input(
BenchmarkId::new(
"Save to bytes",
format!(
"{}x{}",
w.map(|i| i.to_string()).unwrap_or("FULL".to_string()),
h.map(|i| i.to_string()).unwrap_or("FULL".to_string())
),
),
size,
|b, size| {
let small_img = resize(&img, *size, FilterType::Builtin(imageops::Lanczos3));
b.iter(|| black_box(save_to_jpeg_bytes(&small_img)))
},
);
group.bench_with_input(
BenchmarkId::new(
"Full pipeline Lanczos3",
format!(
"{}x{}",
w.map(|i| i.to_string()).unwrap_or("FULL".to_string()),
h.map(|i| i.to_string()).unwrap_or("FULL".to_string())
),
),
size,
|b, size| {
b.iter(|| {
let img = load_image(TEST_IMAGE_PATH, size.0, size.1)
.expect("failed to load test image");
let small_img = resize(&img, *size, FilterType::Builtin(imageops::Lanczos3));
black_box(save_to_jpeg_bytes(&small_img))
})
},
);
}
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

1
config.dbuild Normal file
View File

@ -0,0 +1 @@
package="app/photosync"

2
dockerfiles/cargo-config Normal file
View File

@ -0,0 +1,2 @@
[net]
git-fetch-with-cli = true

1
dockerfiles/netrc Normal file
View File

@ -0,0 +1 @@
machine git.z.xinu.tv login wathiede password gitgit

View File

@ -1,6 +1,7 @@
{
"name": "react-debug",
"version": "0.1.0",
"proxy": "http://localhost:4000",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
@ -8,6 +9,7 @@
"@testing-library/user-event": "^7.1.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1"
},
"scripts": {

View File

@ -1 +0,0 @@
/tmp/photosync

View File

@ -36,3 +36,7 @@
transform: rotate(360deg);
}
}
.figure {
width: 285px;
}

111
react-debug/src/App.js vendored
View File

@ -1,6 +1,56 @@
import React from 'react';
import {
HashRouter as Router,
Switch,
Route,
useParams
} from "react-router-dom";
class App extends React.Component {
import './App.css';
class Album extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
media_items: null,
};
}
componentDidMount() {
let {album} = this.props;
fetch(process.env.PUBLIC_URL + `/api/album/${album}`)
.then(res => res.json())
.then(
(result) => this.setState({media_items: result}),
(error) => this.setState({error}),
);
}
render() {
let {error, media_items} = this.state;
console.log(this.state);
if (error !== null) {
return <h2>Error: {JSON.stringify(error)}</h2>;
} else if (media_items !== null) {
console.log(media_items);
return media_items.map((mi) => {
// TODO(wathiede): use coverPhotoMediaItemId and fetch from a
// locally cached image.
return <figure key={ mi.id } className="figure">
<img 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>
</a>
</figcaption>
</figure>
});
} else {
return <h2>Loading...</h2>;
}
}
}
class AlbumIndex extends React.Component {
constructor(props) {
super(props);
this.state = {
@ -9,7 +59,7 @@ class App 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}),
@ -18,37 +68,50 @@ class App extends React.Component {
}
render() {
let {error, albums} = this.state;
console.log(this.state);
let content;
if (error !== null) {
content = <h2>Error: {JSON.stringify(error)}</h2>;
return <h2>Error: {JSON.stringify(error)}</h2>;
} else if (albums !== null) {
console.log(albums);
content = albums.map((a) => {
let thumb = <img className="mr-3"/>;
if (a.coverPhotoBaseUrl !== undefined) {
thumb = <img src={ a.coverPhotoBaseUrl + "=w64-h64-c" } className="mr-3" alt={ a.title }/>;
return albums.map((a) => {
let img = <img src="https://via.placeholder.com/256x128" className="mr-3" alt="unset"/>;
if (a.coverPhotoMediaItemId !== undefined) {
img = <img src={ `/api/image/${a.coverPhotoMediaItemId}?w=256&h=256` } className="mr-3" alt={ a.title }/>
}
return <div key={ a.id } className="media">
<a href={ a.productUrl }>
{thumb}
let figure = <figure key={ a.id } className="figure">
{img}
<figcaption className="figure-caption">{ a.title || "No title" } - { a.mediaItemsCount || 0 } photos </figcaption>
</figure>;
return <a key={ a.id } href={ '#' + a.id }>
{ figure }
</a>
<div className="media-body">
<h5 className="mt-0">{ a.title }</h5>
{ a.mediaItemsCount } photos
</div>
</div>
});
} else {
content = <h2>Loading...</h2>;
return <h2>Loading...</h2>;
}
return (
<div className="container">
{ content }
</div>
);
}
}
const AlbumRoute = () => {
// We can use the `useParams` hook here to access
// the dynamic pieces of the URL.
let { album_id } = useParams();
return <Album album={album_id} />;
}
const App = () => {
return <div className="container">
<Router>
<Switch>
<Route exact path="/">
<AlbumIndex />
</Route>
<Route exact path="/:album_id">
<AlbumRoute />
</Route>
</Switch>
</Router>
</div>
}
export default App;

View File

@ -849,7 +849,7 @@
core-js-pure "^3.0.0"
regenerator-runtime "^0.13.2"
"@babel/runtime@7.8.4", "@babel/runtime@^7.0.0", "@babel/runtime@^7.3.4", "@babel/runtime@^7.4.5", "@babel/runtime@^7.5.1", "@babel/runtime@^7.6.2", "@babel/runtime@^7.6.3", "@babel/runtime@^7.7.2", "@babel/runtime@^7.7.4", "@babel/runtime@^7.7.6":
"@babel/runtime@7.8.4", "@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.3.4", "@babel/runtime@^7.4.0", "@babel/runtime@^7.4.5", "@babel/runtime@^7.5.1", "@babel/runtime@^7.6.2", "@babel/runtime@^7.6.3", "@babel/runtime@^7.7.2", "@babel/runtime@^7.7.4", "@babel/runtime@^7.7.6":
version "7.8.4"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.8.4.tgz#d79f5a2040f7caa24d53e563aad49cbc05581308"
integrity sha512-neAp3zt80trRVBI1x0azq6c57aNBqYZH8KhMm3TaB7wEI5Q4A2SHfBHE8w9gOhI/lrqxtEbXZgQIrHP+wvSGwQ==
@ -4609,6 +4609,11 @@ growly@^1.3.0:
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
integrity sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE=
gud@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/gud/-/gud-1.0.0.tgz#a489581b17e6a70beca9abe3ae57de7a499852c0"
integrity sha512-zGEOVKFM5sVPPrYs7J5/hYEw2Pof8KCyOwyhG8sAF26mCAeUFAcYPu1mwB7hhpIP29zOIBaDqwuHdLp0jvZXjw==
gzip-size@5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-5.1.1.tgz#cb9bee692f87c0612b232840a873904e4c135274"
@ -4726,6 +4731,18 @@ hex-color-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==
history@^4.9.0:
version "4.10.1"
resolved "https://registry.yarnpkg.com/history/-/history-4.10.1.tgz#33371a65e3a83b267434e2b3f3b1b4c58aad4cf3"
integrity sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==
dependencies:
"@babel/runtime" "^7.1.2"
loose-envify "^1.2.0"
resolve-pathname "^3.0.0"
tiny-invariant "^1.0.2"
tiny-warning "^1.0.0"
value-equal "^1.0.1"
hmac-drbg@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"
@ -4735,6 +4752,13 @@ hmac-drbg@^1.0.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.1"
hoist-non-react-statics@^3.1.0:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
dependencies:
react-is "^16.7.0"
hosted-git-info@^2.1.4:
version "2.8.5"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.5.tgz#759cfcf2c4d156ade59b0b2dfabddc42a6b9c70c"
@ -5376,6 +5400,11 @@ is-wsl@^1.1.0:
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d"
integrity sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=
isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=
isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
@ -6219,7 +6248,7 @@ loglevel@^1.6.6:
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.6.tgz#0ee6300cc058db6b3551fa1c4bf73b83bb771312"
integrity sha512-Sgr5lbboAUBo3eXCSPL4/KoVz3ROKquOjcctxmHIt+vol2DrqTQe3SwkKKuYhEiWB5kYa13YyopJ69deJ1irzQ==
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
@ -6421,6 +6450,15 @@ min-indent@^1.0.0:
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.0.tgz#cfc45c37e9ec0d8f0a0ec3dd4ef7f7c3abe39256"
integrity sha1-z8RcN+nsDY8KDsPdTvf3w6vjklY=
mini-create-react-context@^0.3.0:
version "0.3.2"
resolved "https://registry.yarnpkg.com/mini-create-react-context/-/mini-create-react-context-0.3.2.tgz#79fc598f283dd623da8e088b05db8cddab250189"
integrity sha512-2v+OeetEyliMt5VHMXsBhABoJ0/M4RCe7fatd/fBy6SMiKazUSEt3gxxypfnk2SHMkdBYvorHRoQxuGoiwbzAw==
dependencies:
"@babel/runtime" "^7.4.0"
gud "^1.0.0"
tiny-warning "^1.0.2"
mini-css-extract-plugin@0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/mini-css-extract-plugin/-/mini-css-extract-plugin-0.9.0.tgz#47f2cf07aa165ab35733b1fc97d4c46c0564339e"
@ -7187,6 +7225,13 @@ path-to-regexp@0.1.7:
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
path-to-regexp@^1.7.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.8.0.tgz#887b3ba9d84393e87a0a0b9f4cb756198b53548a"
integrity sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==
dependencies:
isarray "0.0.1"
path-type@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-2.0.0.tgz#f012ccb8415b7096fc2daa1054c3d72389594c73"
@ -8268,11 +8313,40 @@ react-error-overlay@^6.0.5:
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.5.tgz#55d59c2a3810e8b41922e0b4e5f85dcf239bd533"
integrity sha512-+DMR2k5c6BqMDSMF8hLH0vYKtKTeikiFW+fj0LClN+XZg4N9b8QUAdHC62CGWNLTi/gnuuemNcNcTFrCvK1f+A==
react-is@^16.8.1, react-is@^16.8.4:
react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4:
version "16.12.0"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.12.0.tgz#2cc0fe0fba742d97fd527c42a13bec4eeb06241c"
integrity sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q==
react-router-dom@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-5.1.2.tgz#06701b834352f44d37fbb6311f870f84c76b9c18"
integrity sha512-7BPHAaIwWpZS074UKaw1FjVdZBSVWEk8IuDXdB+OkLb8vd/WRQIpA4ag9WQk61aEfQs47wHyjWUoUGGZxpQXew==
dependencies:
"@babel/runtime" "^7.1.2"
history "^4.9.0"
loose-envify "^1.3.1"
prop-types "^15.6.2"
react-router "5.1.2"
tiny-invariant "^1.0.2"
tiny-warning "^1.0.0"
react-router@5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-5.1.2.tgz#6ea51d789cb36a6be1ba5f7c0d48dd9e817d3418"
integrity sha512-yjEuMFy1ONK246B+rsa0cUam5OeAQ8pyclRDgpxuSCrAlJ1qN9uZ5IgyKC7gQg0w8OM50NXHEegPh/ks9YuR2A==
dependencies:
"@babel/runtime" "^7.1.2"
history "^4.9.0"
hoist-non-react-statics "^3.1.0"
loose-envify "^1.3.1"
mini-create-react-context "^0.3.0"
path-to-regexp "^1.7.0"
prop-types "^15.6.2"
react-is "^16.6.0"
tiny-invariant "^1.0.2"
tiny-warning "^1.0.0"
react-scripts@3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/react-scripts/-/react-scripts-3.3.1.tgz#dee7962045dbee5b02b1d47569815e62f7a546b5"
@ -8630,6 +8704,11 @@ resolve-from@^4.0.0:
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6"
integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==
resolve-pathname@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/resolve-pathname/-/resolve-pathname-3.0.0.tgz#99d02224d3cf263689becbb393bc560313025dcd"
integrity sha512-C7rARubxI8bXFNB/hqcp/4iUeIXJhJZvFPFPiSPRnhU5UPxzMFIl+2E6yY6c4k9giDJAhtV+enfA+G89N6Csng==
resolve-url-loader@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/resolve-url-loader/-/resolve-url-loader-3.1.1.tgz#28931895fa1eab9be0647d3b2958c100ae3c0bf0"
@ -9622,6 +9701,16 @@ timsort@^0.3.0:
resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.3.0.tgz#405411a8e7e6339fe64db9a234de11dc31e02bd4"
integrity sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q=
tiny-invariant@^1.0.2:
version "1.1.0"
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.1.0.tgz#634c5f8efdc27714b7f386c35e6760991d230875"
integrity sha512-ytxQvrb1cPc9WBEI/HSeYYoGD0kWnGEOR8RY6KomWLBVhqz0RgTwVO9dLrGz7dC+nN9llyI7OKAgRq8Vq4ZBSw==
tiny-warning@^1.0.0, tiny-warning@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==
tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
@ -9970,6 +10059,11 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"
value-equal@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/value-equal/-/value-equal-1.0.1.tgz#1e0b794c734c5c0cade179c437d356d931a34d6c"
integrity sha512-NOJ6JZCAWr0zlxZt+xqCHNTEKOsrks2HQd4MqhP1qy4z1SkbEP467eNx6TgDKXMvUOb+OENfJCZwM+16n7fRfw==
vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"

23
react-slideshow/.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

68
react-slideshow/README.md Normal file
View File

@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `yarn start`
Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.<br />
You will also see any lint errors in the console.
### `yarn test`
Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `yarn build`
Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `yarn eject`
**Note: this is a one-way operation. Once you `eject`, you cant go back!**
If you arent satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
### Analyzing the Bundle Size
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
### Making a Progressive Web App
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
### Advanced Configuration
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
### Deployment
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
### `yarn build` fails to minify
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

View File

@ -0,0 +1,45 @@
{
"name": "react-slideshow",
"version": "0.1.0",
"proxy": "http://sky.h:8000",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^25.1.3",
"@types/node": "^13.7.6",
"@types/react": "^16.9.23",
"@types/react-dom": "^16.9.5",
"@types/react-router": "^5.1.4",
"@types/react-router-dom": "^5.1.3",
"bootstrap": "^4.5.0",
"react": "^16.12.0",
"react-bootstrap": "^1.0.1",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"typescript": "^3.8.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="https://static.xinu.tv/favicon/gallery.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Photo gallery @ xinu.tv"
/>
<link rel="apple-touch-icon" href="https://static.xinu.tv/favicon/gallery.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Xinu Slideshow</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

View File

@ -0,0 +1,14 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "https://static.xinu.tv/favicon/gallery.png",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

View File

@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

View File

@ -0,0 +1,25 @@
body, html, #root {
height: 100%;
}
.container {
margin-top: 2em;
}
#ui {
top: 0;
line-height: 3em;
position: absolute;
width: 100%;
height: 100%;
}
#ui .meta {
background-color: rgba(255, 255, 255, 0.5);
line-height: 3em;
text-align: center;
}
#slide {
height: 100%;
}

View File

@ -0,0 +1,9 @@
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});

359
react-slideshow/src/App.tsx Normal file
View File

@ -0,0 +1,359 @@
import React from 'react';
import {
HashRouter as Router,
Switch,
Route,
useParams
} from "react-router-dom";
import {
Button,
Card
} from 'react-bootstrap';
import Random from './rand';
import './App.css';
type Config = {
sleepTimeSeconds: number;
showUI: boolean;
}
let CONFIG: Config;
if (process.env.NODE_ENV === 'production') {
CONFIG = {
sleepTimeSeconds: 60,
showUI: false,
}
} else {
CONFIG = {
sleepTimeSeconds: 10,
showUI: true,
}
}
const IMAGE_CHUNK = 256;
const roundup = (v: number, mod: number) => {
let r = v % mod;
if (r === 0) {
return v;
} else {
return v + (mod - r);
}
};
/**
* Shuffles array in place. ES6 version
* From https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
* @param {Array} a items An array containing the items.
*/
function shuffle<T>(a: Array<T>) {
let rng = new Random(new Date().getDate());
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(rng.nextFloat() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
class Slide {
// One or two items. For example if display is landscape we'll try to fit
// two portrait images and only one landscape.
items: Array<MediaItem>;
nextSlide?: Slide;
prevSlide?: Slide;
constructor(items: Array<MediaItem>) {
this.items = items;
}
prefetchImages() {
console.log(`prefetchImages, I have ${this.imageUrls.length} images`);
this.imageUrls.map(url => new Image().src = url);
}
get imageUrls(): Array<string> {
let w = window.innerWidth * window.devicePixelRatio;
let h = window.innerHeight * window.devicePixelRatio;
let ratio = w/h;
if (ratio > 1) {
// Landscape image
w = roundup(w, IMAGE_CHUNK);
h = Math.round(w/ratio);
} else {
// Portrait image
h = roundup(h, IMAGE_CHUNK);
w = Math.round(h/ratio);
}
//console.log(`Window size ${window.innerWidth}x${window.innerHeight} with a devicePixelRatio of ${window.devicePixelRatio} for a total size of ${w}x${h}`);
return this.items.map(img => `/api/image/${img.id}?w=${w}&h=${h}`);
}
render() {
let urls = this.imageUrls;
let frac = 100 / urls.length;
let imgs = urls.map(url => {
// TODO(wathiede): make this landscape/portrait aware.
let style: React.CSSProperties = {
height: '100%',
width: frac + '%',
backgroundColor: 'black',
backgroundImage: `url(${url})`,
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center center',
backgroundSize: 'cover',
float: 'left',
};
return <div key={url} style={style}></div>;
});
// TODO(wathiede): make sure the style handles multiple items.
return <div style={{
height: '100%',
width: '100%',
}}>{imgs}</div>;
}
};
function makePairs<T>(items: Array<T>) {
const half = Math.floor(items.length/2);
console.log(`items ${items.length} half ${half}`)
let pairs = [];
for (let i = 0; i < half; i++) {
pairs.push([items[2*i], items[2*i+1]]);
}
return pairs;
}
type MediaMetadata = {
width: number;
height: number;
};
type MediaItem = {
id: string;
mediaMetadata: MediaMetadata;
filename: string;
};
type AlbumProps = {
album: string;
showUI: boolean;
sleepTimeSeconds: number;
};
type AlbumState = {
error: any;
mediaItems: Array<MediaItem> | null;
curSlide?: Slide,
showUI: boolean;
timerID: any | null;
};
class Album extends React.Component<AlbumProps, AlbumState> {
state: AlbumState = {
error: null,
mediaItems: null,
showUI: this.props.showUI,
timerID: null,
};
componentDidMount() {
this.loadAlbum()
}
loadAlbum() {
let {album} = this.props;
fetch(process.env.PUBLIC_URL + `/api/album/${album}`)
.then(res => res.json())
.then(
(mediaItems: Array<MediaItem>) => {
let w = window.innerWidth * window.devicePixelRatio;
let h = window.innerHeight * window.devicePixelRatio;
let ratio = w/h;
let landscapes = mediaItems.filter((mi) => {
let md = mi.mediaMetadata;
let ratio = md.width/md.height;
return ratio > 1;
});
let portraits = mediaItems.filter((mi) => {
let md = mi.mediaMetadata;
let ratio = md.width/md.height;
return ratio <= 1;
});
console.log(`${landscapes.length} landscape photos`);
console.log(`${portraits.length} portraits photos`);
let slides: Array<Slide>;
if (ratio > 1) {
console.log('display in landscape mode');
slides = landscapes.map((p)=>{
return new Slide([p]);
});
let pairs = makePairs(shuffle(portraits));
slides = slides.concat(pairs.map((p, i) => new Slide(p)));
} else {
console.log('display in portrait mode');
slides = portraits.map((p)=>{
return new Slide([p]);
});
// TODO(wathiede): fix Slide::render before adding landscapes
// to slides here.
}
slides = shuffle(slides);
console.log(`${slides.length} slides`);
let numSlides = slides.length;
slides.forEach((p, idx)=>{
let nextIdx = (idx+1)%numSlides;
let prevIdx = (numSlides+idx-1)%numSlides;
p.nextSlide = slides[nextIdx];
p.prevSlide = slides[prevIdx];
})
this.setState({curSlide: slides[0]});
let {sleepTimeSeconds} = this.props;
let timerID = setInterval(()=>{
let {curSlide} = this.state;
this.setState({curSlide: curSlide?.nextSlide})
console.log('timer fired');
}, sleepTimeSeconds*1000);
this.setState({timerID});
},
(error) => this.setState({error}),
);
}
componentWillUnmount() {
let {timerID} = this.state;
clearInterval(timerID);
}
nextPhoto() {
}
render() {
// TODO(wathiede): fade transition.
let {curSlide, error, showUI} = this.state;
if (error !== null) {
return <h2>Error: {JSON.stringify(error)}</h2>;
} else if (curSlide) {
let nextSlide = curSlide?.nextSlide;
let prevSlide = curSlide?.prevSlide;
let prefetchStyle: React.CSSProperties = {
backgroundColor: 'rgba(127, 127, 127, 0.5)',
backgroundPosition: 'center center',
bottom: 0,
height: '25%',
position: 'absolute',
width: '25%',
};
let leftPrefetchStyle: React.CSSProperties = {
left: 0,
...prefetchStyle
};
let rightPrefetchStyle: React.CSSProperties = {
right: 0,
...prefetchStyle
};
let ui;
if (showUI) {
ui = <div id="ui">
<div
style={leftPrefetchStyle}
onClick={(e)=>{
e.stopPropagation();
this.setState({curSlide: curSlide?.prevSlide})
}}>{ prevSlide?.render() }</div>
{/* TODO(wathiede): make this work with multiple items. */}
<div className="meta">{curSlide?.items.map(i=>i.filename).join(' | ')}</div>
<div
style={rightPrefetchStyle}
onClick={(e)=>{
e.stopPropagation();
this.setState({curSlide: curSlide?.nextSlide})
}}>{ nextSlide?.render() }</div>
</div>;
}
nextSlide?.prefetchImages();
return <div id="slide" onClick={(e)=>{
e.stopPropagation();
this.setState({showUI: !showUI})
}}>
{ curSlide?.render() }
{ ui }
</div>;
} else {
return <h2>Loading...</h2>;
}
}
}
type AlbumIndexProps = {
};
type AlbumIndexState = {
error: any | null,
albums: Array<any> | null,
};
class AlbumIndex extends React.Component<AlbumIndexProps, AlbumIndexState> {
state: AlbumIndexState = {
error: null,
albums: null,
}
componentDidMount() {
fetch(process.env.PUBLIC_URL + "/api/albums")
.then(res => res.json())
.then(
(result) => this.setState({albums: result}),
(error) => this.setState({error}),
);
}
render() {
let {error, albums} = this.state;
if (error !== null) {
return <h2>Error: {JSON.stringify(error)}</h2>;
} else if (albums !== null) {
return albums.map((a) => {
let img_url = "https://via.placeholder.com/256x128";
let img = <img src="https://via.placeholder.com/256x128" className="mr-3" alt="unset"/>;
if (a.coverPhotoMediaItemId !== undefined) {
img_url = `/api/image/${a.coverPhotoMediaItemId}?w=512&h=512`
img = <img src={ `/api/image/${a.coverPhotoMediaItemId}?w=256&h=256` } className="mr-3" alt={ a.title }/>
}
let figure = <figure key={ a.id } className="figure">
{img}
<figcaption className="figure-caption">{ a.title || "No title" } - { a.mediaItemsCount || 0 } photos </figcaption>
</figure>;
return <Card key={a.id} style={{width: '50%'}}>
<Card.Img variant="top" src={img_url} />
<Card.Body>
<Card.Title>{a.title}</Card.Title>
<Card.Text>
{a.mediaItemsCount || 0} photos
</Card.Text>
<Button href={'#' + a.id} variant="primary" block>Slideshow</Button>
</Card.Body>
</Card>
});
} else {
return <h2>Loading...</h2>;
}
}
}
type AlbumRouteProps = { sleepTimeSeconds: number, showUI: boolean };
const AlbumRoute = ({sleepTimeSeconds, showUI}: AlbumRouteProps) => {
// We can use the `useParams` hook here to access
// the dynamic pieces of the URL.
let { albumId } = useParams();
albumId = albumId || '';
return <Album album={albumId} showUI={showUI} sleepTimeSeconds={sleepTimeSeconds} />;
}
const App = () => {
let {showUI, sleepTimeSeconds} = CONFIG;
return <Router>
<Switch>
<Route exact path="/">
<div className="container">
<AlbumIndex />
</div>
</Route>
<Route exact path="/lookup/:albumId">
<AlbumRoute showUI={showUI} sleepTimeSeconds={sleepTimeSeconds} />
</Route>
<Route exact path="/:albumId">
<AlbumRoute showUI={showUI} sleepTimeSeconds={sleepTimeSeconds} />
</Route>
</Switch>
</Router>
}
export default App;

View File

@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}

14
react-slideshow/src/index.js vendored Normal file
View File

@ -0,0 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
// Importing the Bootstrap CSS
import 'bootstrap/dist/css/bootstrap.min.css';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

View File

@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3">
<g fill="#61DAFB">
<path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/>
<circle cx="420.9" cy="296.5" r="45.7"/>
<path d="M520.5 78.1z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

30
react-slideshow/src/rand.js vendored Normal file
View File

@ -0,0 +1,30 @@
// From https://gist.github.com/blixt/f17b47c62508be59987b
/**
* Creates a pseudo-random value generator. The seed must be an integer.
*
* Uses an optimized version of the Park-Miller PRNG.
* http://www.firstpr.com.au/dsp/rand31/
*/
function Random(seed) {
console.log(`Seeding prng with ${seed}`);
this._seed = seed % 2147483647;
if (this._seed <= 0) this._seed += 2147483646;
}
/**
* Returns a pseudo-random value between 1 and 2^32 - 2.
*/
Random.prototype.next = function () {
return this._seed = this._seed * 16807 % 2147483647;
};
/**
* Returns a pseudo-random floating point number in range [0, 1).
*/
Random.prototype.nextFloat = function () {
// We know that result of next() will be 1 to 2147483646 (inclusive).
return (this.next() - 1) / 2147483646;
};
export default Random;

View File

@ -0,0 +1 @@
/// <reference types="react-scripts" />

141
react-slideshow/src/serviceWorker.js vendored Normal file
View File

@ -0,0 +1,141 @@
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' }
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then(registration => {
registration.unregister();
})
.catch(error => {
console.error(error.message);
});
}
}

5
react-slideshow/src/setupTests.js vendored Normal file
View File

@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}

9744
react-slideshow/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

7
src/lib.rs Normal file
View File

@ -0,0 +1,7 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
pub mod library;
pub mod rweb;

168
src/library.rs Normal file
View File

@ -0,0 +1,168 @@
use std::io;
use std::io::Read;
use std::path::PathBuf;
use cacher::s3::S3CacherError;
use cacher::S3Cacher;
use google_photoslibrary1 as photos;
use image::imageops;
use imageutils::{load_image_buffer, resize, resize_to_fill, save_to_jpeg_bytes, FilterType};
use log::{error, info};
use photos::schemas::{Album, MediaItem};
use rusoto_core::RusotoError;
use rusoto_s3::GetObjectError;
use thiserror::Error;
// Used to ensure DB is invalidated after schema changes.
const LIBRARY_GENERATION: &'static str = "16";
#[derive(Error, Debug)]
pub enum LibraryError {
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("s3 error: {0}")]
S3CacherError(#[from] S3CacherError),
#[error("json error: {0}")]
JsonError(#[from] serde_json::Error),
}
#[derive(Clone)]
pub struct Library {
s3: S3Cacher,
}
impl Library {
pub fn new(s3: S3Cacher) -> Result<Library, Box<dyn std::error::Error>> {
let lib = Library { s3 };
Ok(lib)
}
pub fn create_album_index(&self, albums: &Vec<Album>) -> Result<(), LibraryError> {
// Serialize it to a JSON string.
let j = serde_json::to_string(albums)?;
let filename = "albums.json";
self.s3
.set(&Library::generational_key(filename), j.as_ref())?;
Ok(())
}
pub fn create_album(
&self,
album_id: &str,
media_items: &Vec<MediaItem>,
) -> Result<(), LibraryError> {
let relpath = format!("{}.json", &album_id);
let j = serde_json::to_string(&media_items)?;
self.s3
.set(&Library::generational_key(&relpath), j.as_ref())?;
Ok(())
}
pub fn albums(&self) -> Result<Vec<Album>, Box<dyn std::error::Error>> {
let filename = "albums.json";
let bytes = self.s3.get(&Library::generational_key(filename))?;
let album: Vec<Album> = serde_json::from_slice(&bytes)?;
Ok(album)
}
pub fn album(&self, album_id: &str) -> Result<Vec<MediaItem>, Box<dyn std::error::Error>> {
let relpath = format!("{}.json", &album_id);
let bytes = self.s3.get(&Library::generational_key(&relpath))?;
let mis: Vec<MediaItem> = serde_json::from_slice(&bytes)?;
Ok(mis)
}
pub fn download_image(
&self,
_filename: &str,
media_items_id: &str,
base_url: &str,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
let filename = Library::generational_key(&format!("images/originals/{}", media_items_id));
if !self.s3.contains_key(&filename) {
let url = format!("{}=d", base_url);
let mut r = reqwest::blocking::get(&url)?;
let mut buf = Vec::new();
info!("Downloading {}", &url);
r.read_to_end(&mut buf)?;
self.s3.set(&filename, &buf)?;
}
Ok(filename.into())
}
pub fn original_buffer(&self, media_items_id: &str) -> Result<Vec<u8>, LibraryError> {
let filename = Library::generational_key(&format!("images/originals/{}", media_items_id));
let bytes = self.s3.get(&filename)?;
Ok(bytes)
}
// TODO(wathiede): make this a macro like format! to skip the second string create and copy.
fn generational_key(key: &str) -> String {
format!("{}/{}", LIBRARY_GENERATION, key)
}
pub fn generate_thumbnail(
&self,
media_items_id: &str,
dimensions: (Option<u32>, Option<u32>),
filter: FilterType,
fill: bool,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let buf = self.original_buffer(&media_items_id)?;
let dimension_hint = match dimensions {
(Some(w), Some(h)) => Some((w, h)),
// Partial dimensions should be handled by the caller of this function. So all
// other options are None.
_ => None,
};
let orig_img = load_image_buffer(buf, dimension_hint)?;
//.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let img = if fill {
resize_to_fill(&orig_img, dimensions, filter)
} else {
resize(&orig_img, dimensions, filter)
};
let buf = save_to_jpeg_bytes(&img).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(buf)
}
pub fn thumbnail(
&self,
media_items_id: &str,
dimensions: (Option<u32>, Option<u32>),
fill: bool,
) -> Option<Vec<u8>> {
fn cache_key(media_items_id: &str, dimensions: (Option<u32>, Option<u32>)) -> String {
let dim = match dimensions {
(Some(w), Some(h)) => format!("-w={}-h={}", w, h),
(Some(w), None) => format!("-w={}", w),
(None, Some(h)) => format!("-h={}", h),
(None, None) => "".to_string(),
};
Library::generational_key(&format!("images/thumbnails/{}-{}", media_items_id, dim))
}
let key = cache_key(media_items_id, dimensions);
match self.s3.get(&key) {
Ok(bytes) => return Some(bytes),
Err(S3CacherError::GetObjectError(RusotoError::Service(
GetObjectError::NoSuchKey(msg),
))) => info!("Missing thumbnail {} in s3: {}", key, msg),
Err(e) => error!("Error fetching thumbnail {} from s3: {}", key, e),
};
info!("cache MISS {}", key);
let bytes = match self.generate_thumbnail(
media_items_id,
dimensions,
FilterType::Builtin(imageops::FilterType::Lanczos3),
fill,
) {
Ok(bytes) => bytes,
Err(e) => {
error!("Failed to generate thumbnail for {}: {}", media_items_id, e);
return None;
}
};
if let Err(e) = self.s3.set(&key, &bytes) {
error!("Failed to put thumbnail {}: {}", &key, e);
}
Some(bytes)
}
}

View File

@ -1,31 +1,93 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::thread;
use std::time;
use cacher::S3Cacher;
use google_api_auth;
use google_photoslibrary1 as photos;
use log::{debug, info};
use hexihasher;
use lazy_static::lazy_static;
use log::{debug, error, info};
use photos::schemas::{Album, MediaItem, SearchMediaItemsRequest};
use regex::Regex;
use structopt::StructOpt;
use yup_oauth2::{Authenticator, InstalledFlow};
use photosync::library::Library;
use photosync::rweb;
fn parse_duration(src: &str) -> Result<time::Duration, std::num::ParseIntError> {
let secs = str::parse::<u64>(src)?;
Ok(time::Duration::from_secs(secs))
}
#[derive(Debug, StructOpt)]
struct Sync {
#[structopt(flatten)]
auth: Auth,
/// Optional album title to filter. Default will mirror all albums.
#[structopt(short, long)]
title_filter: Option<Regex>,
/// S3 bucket holding metadata and images.
#[structopt(long, default_value = "photosync-dev")]
s3_bucket: String,
}
#[derive(Debug, StructOpt)]
struct Serve {
/// HTTP address to listen for web requests.
#[structopt(long = "addr", default_value = "0.0.0.0:0")]
addr: SocketAddr,
}
#[derive(Debug, StructOpt)]
enum Command {
/// List albums for the user of the given credentials. Optionally title filter.
ListAlbums {
#[structopt(flatten)]
auth: Auth,
title_filter: Option<Regex>,
},
SearchMediaItems {
#[structopt(flatten)]
auth: Auth,
album_id: String,
},
Sync {
/// Optional album title to filter. Default will mirror all albums.
#[structopt(short, long)]
title_filter: Option<Regex>,
/// Directory to store sync.
output: PathBuf,
#[structopt(flatten)]
sync: Sync,
},
Serve {
#[structopt(flatten)]
serve: Serve,
/// S3 bucket holding metadata and images.
#[structopt(default_value = "photosync-dev")]
s3_bucket: String,
},
ServeAndSync {
/// Sync albums at given interval.
#[structopt(parse(try_from_str = parse_duration))]
interval: time::Duration,
#[structopt(flatten)]
sync: Sync,
/// HTTP address to listen for web requests.
#[structopt(long = "addr", default_value = "0.0.0.0:0")]
addr: SocketAddr,
},
}
#[derive(Debug, StructOpt)]
struct Auth {
/// Path to json file containing Google client ID and secrets for out of band auth flow.
#[structopt(long)]
credentials: PathBuf,
/// Path to json file where photosync will store auth tokens refreshed from Google.
#[structopt(long)]
token_cache: PathBuf,
}
#[derive(Debug, StructOpt)]
@ -38,13 +100,6 @@ struct Opt {
#[structopt(short, parse(from_occurrences))]
verbose: usize,
/// Path to json file containing Google client ID and secrets for out of band auth flow.
#[structopt(long)]
credentials: PathBuf,
/// Path to json file where photosync will store auth tokens refreshed from Google.
#[structopt(long)]
token_cache: PathBuf,
#[structopt(subcommand)]
cmd: Command,
}
@ -123,49 +178,80 @@ impl<'a> Iterator for SearchIter<'a> {
}
}
fn search_media_items(client: photos::Client, album_id: String) -> Result<(), Box<dyn Error>> {
let mut total = 0;
fn print_media_items(media_items: Vec<MediaItem>) {
for mi in &media_items {
let id = mi
.id
.as_ref()
.map_or("NO ID".to_string(), |s| s.to_string());
println!(
"media item: {}\n\t{}\n\t{}",
mi.filename.as_ref().unwrap_or(&"NO FILENAME".to_string()),
hexihasher::sha256(id.as_bytes()),
id,
);
}
println!("({}) items total", media_items.len());
}
fn search_media_items(
client: &photos::Client,
album_id: &str,
) -> Result<Vec<MediaItem>, Box<dyn Error>> {
let media_items = SearchIter::new(
&client,
SearchMediaItemsRequest {
album_id: Some(album_id.clone()),
album_id: Some(album_id.to_string()),
// 100 is the documented max.
page_size: Some(100),
..Default::default()
},
);
for mi in media_items {
let mi = mi?;
total += 1;
println!(
"{} {}",
mi.id.unwrap_or("NO ID".to_string()),
mi.filename.unwrap_or("NO FILENAME".to_string())
);
}
println!("({}) items total", total);
Ok(())
)
.filter_map(|mi| mi.ok())
.collect();
Ok(media_items)
}
lazy_static! {
static ref MIME_TO_EXT: HashMap<&'static str, &'static str> = [
("image/gif", "gif"),
("image/heif", "heic"),
("image/jpeg", "jpg"),
]
.iter()
.copied()
.collect();
}
fn sync_albums(
client: photos::Client,
title_filter: Option<Regex>,
output_dir: PathBuf,
client: &photos::Client,
title_filter: &Option<Regex>,
lib: &Library,
) -> Result<(), Box<dyn Error>> {
let albums = list_albums(client, title_filter)?;
info!("albums {:?}", albums);
lib.create_album_index(&albums)?;
for a in &albums {
let album_dir = output_dir.join(a.id.as_ref().expect("missing album id"));
if !album_dir.exists() {
info!("making album directory {}", album_dir.to_string_lossy());
fs::create_dir_all(album_dir)?;
let album_id = a.id.as_ref().expect("unset album id").to_string();
let media_items = search_media_items(client, &album_id)?;
lib.create_album(&album_id, &media_items)?;
for (i, mi) in media_items.iter().enumerate() {
let mi_id = mi.id.as_ref().expect("unset media item id").to_string();
let filename = mi
.filename
.as_ref()
.map_or("NO_FILENAME".to_string(), |s| s.to_string());
let base_url = mi.base_url.as_ref().expect("missing base_url");
let image_path = lib.download_image(&filename, &mi_id, &base_url)?;
info!(
"({}/{}) Checking {} -> {}",
i + 1,
&media_items.len(),
&filename,
image_path.to_string_lossy()
);
}
}
// Serialize it to a JSON string.
let j = serde_json::to_string(&albums)?;
let path = output_dir.join("albums.json");
info!("saving {}", path.to_string_lossy());
fs::write(path, j)?;
Ok(())
}
@ -181,13 +267,19 @@ fn print_albums(albums: Vec<Album>) {
}
fn list_albums(
client: photos::Client,
title_filter: Option<Regex>,
client: &photos::Client,
title_filter: &Option<Regex>,
) -> Result<Vec<Album>, Box<dyn Error>> {
Ok(client
.shared_albums()
.albums()
.list()
.iter_shared_albums_with_all_fields()
.iter_albums_with_all_fields()
.chain(
client
.shared_albums()
.list()
.iter_shared_albums_with_all_fields(),
)
.filter_map(|a| a.ok())
.filter(|a| {
match (&title_filter, &a.title) {
@ -204,6 +296,25 @@ fn list_albums(
.collect())
}
fn background_sync(
client: photos::Client,
interval: time::Duration,
title_filter: Option<Regex>,
lib: Library,
) -> Result<(), Box<dyn Error>> {
thread::spawn(move || loop {
if let Err(err) = sync_albums(&client, &title_filter, &lib) {
error!("Error syncing: {}", err);
}
thread::sleep(interval);
});
Ok(())
}
pub fn serve(addr: SocketAddr, lib: Library) -> Result<(), Box<dyn Error>> {
rweb::run(addr, lib)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt = Opt::from_args();
stderrlog::new()
@ -212,16 +323,56 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.init()
.unwrap();
debug!("opt: {:?}", opt);
let client = new_client(&opt.credentials, &opt.token_cache)?;
match opt.cmd {
Command::ListAlbums { title_filter } => {
print_albums(list_albums(client, title_filter)?);
Command::ListAlbums { auth, title_filter } => {
let client = new_client(&auth.credentials, &auth.token_cache)?;
print_albums(list_albums(&client, &title_filter)?);
Ok(())
}
Command::SearchMediaItems { auth, album_id } => {
let client = new_client(&auth.credentials, &auth.token_cache)?;
print_media_items(search_media_items(&client, &album_id)?);
Ok(())
}
Command::SearchMediaItems { album_id } => search_media_items(client, album_id),
Command::Sync {
title_filter,
output,
} => sync_albums(client, title_filter, output),
sync:
Sync {
auth,
title_filter,
s3_bucket,
},
} => {
let s3 = S3Cacher::new(s3_bucket.clone())?;
let client = new_client(&auth.credentials, &auth.token_cache)?;
let lib = Library::new(s3)?;
sync_albums(&client, &title_filter, &lib)?;
Ok(())
}
Command::Serve {
serve: Serve { addr },
s3_bucket,
} => {
let s3 = S3Cacher::new(s3_bucket.clone())?;
let lib = Library::new(s3)?;
serve(addr, lib)
}
Command::ServeAndSync {
interval,
sync:
Sync {
auth,
title_filter,
s3_bucket,
},
addr,
} => {
let s3 = S3Cacher::new(s3_bucket.clone())?;
let client = new_client(&auth.credentials, &auth.token_cache)?;
let lib = Library::new(s3)?;
background_sync(client, interval, title_filter, lib.clone())?;
serve(addr, lib)?;
Ok(())
}
}
}

148
src/rweb.rs Normal file
View File

@ -0,0 +1,148 @@
use std::error::Error;
use std::io::Write;
use std::net::SocketAddr;
use std::path::PathBuf;
use google_photoslibrary1 as photos;
use log::error;
use photos::schemas::{Album, MediaItem};
use prometheus::Encoder;
use rocket::config::{Config, Environment};
use rocket::http::ContentType;
use rocket::response::status::NotFound;
use rocket::response::Content;
use rocket::State;
use rocket_contrib::json::Json;
use rust_embed::RustEmbed;
use crate::library::Library;
#[get("/metrics")]
fn metrics() -> Content<Vec<u8>> {
let mut buffer = Vec::new();
let encoder = prometheus::TextEncoder::new();
// Gather the metrics.
let metric_families = prometheus::gather();
// Encode them to send.
encoder.encode(&metric_families, &mut buffer).unwrap();
// TODO(wathiede): see if there's a wrapper like html()
Content(ContentType::Plain, buffer)
}
#[get("/")]
fn index() -> Result<Content<Vec<u8>>, NotFound<String>> {
file("index.html")
}
// This is the catch-all handler, it has a high rank so it is the last match in any tie-breaks.
#[get("/<path..>", rank = 99)]
fn path(path: PathBuf) -> Result<Content<Vec<u8>>, NotFound<String>> {
let path = path.to_str().unwrap();
let path = if path.ends_with("/") {
format!("{}index.html", path.to_string())
} else {
path.to_string()
};
file(&path)
}
fn file(path: &str) -> Result<Content<Vec<u8>>, NotFound<String>> {
match Asset::get(path) {
Some(bytes) => {
let mime = mime_guess::from_path(path).first_or_octet_stream();
let ct = ContentType::parse_flexible(mime.essence_str()).unwrap_or(ContentType::Binary);
Ok(Content(ct, bytes.into()))
}
None => Err(NotFound(path.to_string())),
}
}
#[get("/api/albums")]
fn albums(lib: State<Library>) -> Result<Json<Vec<Album>>, NotFound<String>> {
let albums = lib
.albums()
.map_err(|e| NotFound(format!("Couldn't find albums: {}", e)))?;
Ok(Json(albums))
}
#[get("/api/album/<id>")]
fn album(id: String, lib: State<Library>) -> Result<Json<Vec<MediaItem>>, NotFound<String>> {
let album = lib
.album(&id)
.map_err(|e| NotFound(format!("Couldn't find album {}: {}", id, e)))?;
Ok(Json(album))
}
#[get("/api/image/<media_items_id>?<w>&<h>&<fill>")]
fn image(
media_items_id: String,
w: Option<u32>,
h: Option<u32>,
fill: Option<bool>,
lib: State<Library>,
) -> Result<Content<Vec<u8>>, NotFound<String>> {
// TODO(wathiede): add caching headers.
match lib.thumbnail(&media_items_id, (w, h), fill.unwrap_or(false)) {
None => Err(NotFound(format!(
"Couldn't find original {}",
&media_items_id
))),
Some(bytes) => Ok(Content(ContentType::JPEG, bytes.into())),
}
}
#[derive(RustEmbed)]
#[folder = "react-slideshow/build/"]
struct Asset;
#[get("/embedz")]
fn embedz() -> Content<Vec<u8>> {
let mut w = Vec::new();
write!(
w,
r#"<html><table><tbody><tr><th>size</th><th style="text-align: left;">path</th></tr>"#
)
.unwrap();
for path in Asset::iter() {
write!(
w,
r#"<tr><td style="text-align: right;">{0}</td><td><a href="{1}">{1}</a></td</tr>"#,
Asset::get(&path).unwrap().len(),
path
)
.unwrap();
}
Content(ContentType::HTML, w)
}
pub fn run(addr: SocketAddr, lib: Library) -> Result<(), Box<dyn Error>> {
let config = Config::build(Environment::Development)
.address(addr.ip().to_string())
.port(addr.port())
.finalize()?;
let e = rocket::custom(config)
.manage(lib)
.mount(
"/",
routes![album, albums, image, embedz, metrics, index, path],
)
.launch();
match e.kind() {
rocket::error::LaunchErrorKind::Collision(v) => {
error!("Route collisions:");
for (r1, r2) in v {
error!(" R1 {}", r1);
error!(" R2 {}", r2);
}
for (r1, r2) in v {
error!(" R1 {:#?}", r1);
error!(" R2 {:#?}", r2);
}
}
_ => (),
};
return Err(e.into());
}