Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30d0b3064e | ||
|
|
6259ca84d8 | ||
|
|
a6819809a7 |
Generated
+1
@@ -2562,6 +2562,7 @@ dependencies = [
|
|||||||
"cookie",
|
"cookie",
|
||||||
"crc32fast",
|
"crc32fast",
|
||||||
"futures",
|
"futures",
|
||||||
|
"hex",
|
||||||
"image",
|
"image",
|
||||||
"rand 0.8.7",
|
"rand 0.8.7",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ axum = { version = "0.8", features = ["macros"] }
|
|||||||
axum-extra = { version = "0.10", features = ["cookie", "cookie-signed"] }
|
axum-extra = { version = "0.10", features = ["cookie", "cookie-signed"] }
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
hex = "0.4"
|
||||||
image = "0.25"
|
image = "0.25"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
Self-hosted client gallery for photographers. Upload RAWs/JPGs into albums,
|
Self-hosted client gallery for photographers. Upload RAWs/JPGs into albums,
|
||||||
share them with clients via private (optionally password-protected) links,
|
share them with clients via private (optionally password-protected) links,
|
||||||
collect ratings and tags, and let clients download originals.
|
collect accept/reject votes, ratings and tags, and let clients download
|
||||||
|
originals.
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
@@ -36,8 +37,10 @@ collect ratings and tags, and let clients download originals.
|
|||||||
revokes access immediately. Clients use unguessable share tokens, optionally
|
revokes access immediately. Clients use unguessable share tokens, optionally
|
||||||
gated by an argon2-hashed password (10 wrong guesses lock the link for
|
gated by an argon2-hashed password (10 wrong guesses lock the link for
|
||||||
15 minutes).
|
15 minutes).
|
||||||
- **Frontend**: React + Vite SPA — justified gallery, lightbox with rating
|
- **Frontend**: React + Vite SPA — justified gallery, lightbox with
|
||||||
stars and tag chips, drag-and-drop multi-file upload with progress.
|
accept/reject thumbs, rating stars and tag chips, keyboard-driven culling
|
||||||
|
(`P`/`X`/`U`, `1`–`5`, `?` shows all shortcuts), drag-and-drop multi-file
|
||||||
|
upload with progress.
|
||||||
|
|
||||||
## Local development
|
## Local development
|
||||||
|
|
||||||
@@ -135,9 +138,11 @@ Notes:
|
|||||||
- Each album can have any number of share links (`/s/<24-char-token>`), each
|
- Each album can have any number of share links (`/s/<24-char-token>`), each
|
||||||
with its own label (e.g. the client's name), optional password, optional
|
with its own label (e.g. the client's name), optional password, optional
|
||||||
expiry, and a per-link download toggle.
|
expiry, and a per-link download toggle.
|
||||||
- Ratings (1–5 stars) and free-form tags are stored **per link**, so create
|
- Accept/reject votes (👍/👎), ratings (1–5 stars) and free-form tags are
|
||||||
one link per client to keep feedback separate. The album view shows all
|
stored **per link**, so create one link per client to keep feedback
|
||||||
feedback grouped by link label.
|
separate. The album view shows all feedback grouped by link label, and
|
||||||
|
clients can filter their gallery by verdict (e.g. review only what's still
|
||||||
|
undecided, or select-all + download the accepted set).
|
||||||
- Clients (and you) can multi-select photos and download them — or the whole
|
- Clients (and you) can multi-select photos and download them — or the whole
|
||||||
album — as a ZIP. Archives are streamed (each file spools briefly through a
|
album — as a ZIP. Archives are streamed (each file spools briefly through a
|
||||||
temp file for its checksum, then pipelines while the next one prefetches),
|
temp file for its checksum, then pipelines while the next one prefetches),
|
||||||
@@ -150,8 +155,8 @@ Notes:
|
|||||||
- 10 wrong passwords lock a link for 15 minutes (fresh attempts after the
|
- 10 wrong passwords lock a link for 15 minutes (fresh attempts after the
|
||||||
window). A locked link shows in the album's share list with an Unlock
|
window). A locked link shows in the album's share list with an Unlock
|
||||||
button.
|
button.
|
||||||
- Deleting a link removes its ratings/tags; deleting photos or albums cleans
|
- Deleting a link removes its votes/ratings/tags; deleting photos or albums
|
||||||
up S3 objects via background jobs.
|
cleans up S3 objects via background jobs.
|
||||||
|
|
||||||
## Known limitations / deliberate v1 cuts
|
## Known limitations / deliberate v1 cuts
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,13 @@ export function postDownload(url, ids = '') {
|
|||||||
form.remove()
|
form.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SHA-256 of a File, matching the server's content hash — used to skip
|
||||||
|
// uploading bytes the album already has.
|
||||||
|
export async function sha256Hex(file) {
|
||||||
|
const digest = await crypto.subtle.digest('SHA-256', await file.arrayBuffer())
|
||||||
|
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join('')
|
||||||
|
}
|
||||||
|
|
||||||
export function uploadFile(url, file, onProgress) {
|
export function uploadFile(url, file, onProgress) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const xhr = new XMLHttpRequest()
|
const xhr = new XMLHttpRequest()
|
||||||
|
|||||||
@@ -1,42 +1,86 @@
|
|||||||
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { imgUrl } from '../api'
|
import { imgUrl } from '../api'
|
||||||
|
|
||||||
// Justified gallery: rows are built with flexbox, each tile's flex-grow is
|
// True justified layout: pack photos greedily into rows at their real aspect
|
||||||
// proportional to its aspect ratio so rows fill the container edge to edge.
|
// ratios, then scale each row's height so it fills the container width
|
||||||
// When `selected`/`onToggleSelect` are provided, tiles get a select checkmark;
|
// exactly. No cropping, no stretch, and the last row simply renders at the
|
||||||
// selection state lives in the parent so it survives lightbox open/close.
|
// target height instead of being padded by a spacer.
|
||||||
|
function layoutRows(photos, containerWidth, targetHeight, gap) {
|
||||||
|
const rows = []
|
||||||
|
let row = []
|
||||||
|
let arSum = 0
|
||||||
|
let index = 0
|
||||||
|
for (const photo of photos) {
|
||||||
|
const ar = photo.width && photo.height ? photo.width / photo.height : 1.5
|
||||||
|
row.push({ photo, ar, index: index++ })
|
||||||
|
arSum += ar
|
||||||
|
const gaps = (row.length - 1) * gap
|
||||||
|
if (arSum * targetHeight + gaps >= containerWidth) {
|
||||||
|
rows.push({ items: row, height: (containerWidth - gaps) / arSum })
|
||||||
|
row = []
|
||||||
|
arSum = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (row.length > 0) {
|
||||||
|
const gaps = (row.length - 1) * gap
|
||||||
|
rows.push({
|
||||||
|
items: row,
|
||||||
|
height: Math.min(targetHeight, (containerWidth - gaps) / arSum),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
export default function Gallery({ photos, onOpen, overlay, selected, onToggleSelect }) {
|
export default function Gallery({ photos, onOpen, overlay, selected, onToggleSelect }) {
|
||||||
|
const containerRef = useRef(null)
|
||||||
|
const [width, setWidth] = useState(0)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const el = containerRef.current
|
||||||
|
if (!el) return
|
||||||
|
const observer = new ResizeObserver((entries) => setWidth(entries[0].contentRect.width))
|
||||||
|
observer.observe(el)
|
||||||
|
return () => observer.disconnect()
|
||||||
|
}, [])
|
||||||
|
|
||||||
if (photos.length === 0) return null
|
if (photos.length === 0) return null
|
||||||
|
const gap = 6
|
||||||
|
const targetHeight = width < 700 ? 170 : 240
|
||||||
|
const rows = width > 0 ? layoutRows(photos, width, targetHeight, gap) : []
|
||||||
const selecting = selected && selected.size > 0
|
const selecting = selected && selected.size > 0
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`gallery${selecting ? ' selecting' : ''}`}>
|
<div ref={containerRef} className={`gallery${selecting ? ' selecting' : ''}`}>
|
||||||
{photos.map((p, i) => {
|
{rows.map((row) => (
|
||||||
const ar = p.width && p.height ? p.width / p.height : 1.5
|
<div key={row.items[0].photo.id} className="g-row" style={{ height: row.height }}>
|
||||||
const isSelected = selected ? selected.has(p.id) : false
|
{row.items.map(({ photo: p, ar, index }) => {
|
||||||
return (
|
const isSelected = selected ? selected.has(p.id) : false
|
||||||
<div
|
return (
|
||||||
key={p.id}
|
<div
|
||||||
className={`g-item${isSelected ? ' selected' : ''}`}
|
key={p.id}
|
||||||
style={{ '--ar': ar }}
|
className={`g-item${isSelected ? ' selected' : ''}`}
|
||||||
onClick={() => onOpen && onOpen(i)}
|
style={{ width: ar * row.height }}
|
||||||
>
|
onClick={() => onOpen && onOpen(index)}
|
||||||
<img src={imgUrl(p, 'thumb')} loading="lazy" alt={p.filename} />
|
|
||||||
{onToggleSelect && (
|
|
||||||
<button
|
|
||||||
className="g-check"
|
|
||||||
title={isSelected ? 'Deselect' : 'Select'}
|
|
||||||
onClick={(e) => {
|
|
||||||
e.stopPropagation()
|
|
||||||
onToggleSelect(p.id)
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
✓
|
<img src={imgUrl(p, 'thumb')} loading="lazy" alt={p.filename} />
|
||||||
</button>
|
{onToggleSelect && (
|
||||||
)}
|
<button
|
||||||
{overlay && overlay(p)}
|
className="g-check"
|
||||||
</div>
|
title={isSelected ? 'Deselect' : 'Select'}
|
||||||
)
|
onClick={(e) => {
|
||||||
})}
|
e.stopPropagation()
|
||||||
<div className="g-spacer" />
|
onToggleSelect(p.id)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
✓
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{overlay && overlay(p)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,71 @@
|
|||||||
import { useEffect } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { imgUrl } from '../api'
|
import { imgUrl } from '../api'
|
||||||
|
|
||||||
export default function Lightbox({ photos, index, onClose, onNav, footer }) {
|
const BASE_SHORTCUTS = [
|
||||||
|
['← / →', 'previous / next photo'],
|
||||||
|
['Space', 'next photo (Shift+Space back)'],
|
||||||
|
['?', 'show / hide shortcuts'],
|
||||||
|
['Esc', 'close'],
|
||||||
|
]
|
||||||
|
|
||||||
|
// `actions` defines the page's shortcuts as one table — display and dispatch
|
||||||
|
// come from the same entry, so the help overlay can't drift from behavior:
|
||||||
|
// { keys: ['p'], help: ['P', 'accept…'], run: (photo, key) => … }.
|
||||||
|
// Keys fire only outside text inputs and while the help overlay is closed.
|
||||||
|
export default function Lightbox({ photos, index, onClose, onNav, footer, actions }) {
|
||||||
const photo = photos[index]
|
const photo = photos[index]
|
||||||
|
const [showHelp, setShowHelp] = useState(false)
|
||||||
|
|
||||||
|
// Handlers and view state live in a ref, updated every render, so the
|
||||||
|
// window listener is attached once yet always dispatches against current
|
||||||
|
// values — re-subscribing per render leaves a gap until effects re-run in
|
||||||
|
// which a fast second keystroke hits a stale closure (and e.g. re-votes
|
||||||
|
// the previous photo).
|
||||||
|
const live = useRef({})
|
||||||
|
live.current = { index, count: photos.length, photo, onClose, onNav, actions, showHelp }
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onKey = (e) => {
|
// Only text entry captures keys (Escape leaves the field); focus on a
|
||||||
if (e.key === 'Escape') onClose()
|
// checkbox or button must not disable lightbox navigation.
|
||||||
if (e.key === 'ArrowRight' && index < photos.length - 1) onNav(index + 1)
|
const isTyping = (el) =>
|
||||||
if (e.key === 'ArrowLeft' && index > 0) onNav(index - 1)
|
el.tagName === 'TEXTAREA' ||
|
||||||
|
el.isContentEditable ||
|
||||||
|
(el.tagName === 'INPUT' && !['checkbox', 'radio', 'button'].includes(el.type))
|
||||||
|
const handler = (e) => {
|
||||||
|
const s = live.current
|
||||||
|
if (isTyping(e.target)) {
|
||||||
|
if (e.key === 'Escape') e.target.blur()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (e.metaKey || e.ctrlKey || e.altKey) return
|
||||||
|
if (e.key === 'Escape') {
|
||||||
|
if (s.showHelp) setShowHelp(false)
|
||||||
|
else s.onClose()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (e.key === '?') {
|
||||||
|
setShowHelp((h) => !h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// With the help overlay up, keys must not act on the photo behind it.
|
||||||
|
if (s.showHelp) return
|
||||||
|
if (e.key === 'ArrowRight' || (e.key === ' ' && !e.shiftKey)) {
|
||||||
|
e.preventDefault()
|
||||||
|
if (s.index < s.count - 1) s.onNav(s.index + 1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (e.key === 'ArrowLeft' || (e.key === ' ' && e.shiftKey)) {
|
||||||
|
e.preventDefault()
|
||||||
|
if (s.index > 0) s.onNav(s.index - 1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const key = e.key.toLowerCase()
|
||||||
|
const action = s.actions?.find((a) => a.keys.includes(key))
|
||||||
|
if (action && s.photo) action.run(s.photo, key)
|
||||||
}
|
}
|
||||||
window.addEventListener('keydown', onKey)
|
window.addEventListener('keydown', handler)
|
||||||
return () => window.removeEventListener('keydown', onKey)
|
return () => window.removeEventListener('keydown', handler)
|
||||||
}, [index, photos.length, onClose, onNav])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.body.style.overflow = 'hidden'
|
document.body.style.overflow = 'hidden'
|
||||||
@@ -30,6 +83,13 @@ export default function Lightbox({ photos, index, onClose, onNav, footer }) {
|
|||||||
<span className="lb-count">
|
<span className="lb-count">
|
||||||
{index + 1} / {photos.length}
|
{index + 1} / {photos.length}
|
||||||
</span>
|
</span>
|
||||||
|
<button
|
||||||
|
className="lb-btn"
|
||||||
|
onClick={() => setShowHelp((h) => !h)}
|
||||||
|
title="Keyboard shortcuts (?)"
|
||||||
|
>
|
||||||
|
?
|
||||||
|
</button>
|
||||||
<button className="lb-btn" onClick={onClose} title="Close (Esc)">
|
<button className="lb-btn" onClick={onClose} title="Close (Esc)">
|
||||||
✕
|
✕
|
||||||
</button>
|
</button>
|
||||||
@@ -67,6 +127,25 @@ export default function Lightbox({ photos, index, onClose, onNav, footer }) {
|
|||||||
{footer(photo)}
|
{footer(photo)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{showHelp && (
|
||||||
|
<div
|
||||||
|
className="lb-help"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
setShowHelp(false)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="lb-help-card">
|
||||||
|
<h3>Keyboard shortcuts</h3>
|
||||||
|
{[...(actions?.map((a) => a.help) || []), ...BASE_SHORTCUTS].map(([keys, label]) => (
|
||||||
|
<div key={keys} className="lb-help-row">
|
||||||
|
<kbd>{keys}</kbd>
|
||||||
|
<span className="muted">{label}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// Accept/reject vote. `value` is 'accept', 'reject' or null; clicking the
|
||||||
|
// active thumb clears it. Without onChange it renders read-only.
|
||||||
|
export default function Thumbs({ value, onChange, small }) {
|
||||||
|
const thumb = (verdict, glyph, label) => (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`thumb${value === verdict ? ' active' : ''}`}
|
||||||
|
disabled={!onChange}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
onChange(value === verdict ? null : verdict)
|
||||||
|
}}
|
||||||
|
title={onChange ? label : undefined}
|
||||||
|
>
|
||||||
|
{glyph}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
<span className={`thumbs${small ? ' thumbs-small' : ''}`}>
|
||||||
|
{thumb('accept', '👍', 'Accept (P)')}
|
||||||
|
{thumb('reject', '👎', 'Reject (X)')}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,21 +1,59 @@
|
|||||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||||
import { Link, useNavigate, useParams } from 'react-router-dom'
|
import { Link, useNavigate, useParams } from 'react-router-dom'
|
||||||
import { api, postDownload, uploadFile } from '../api'
|
import { api, postDownload, sha256Hex, uploadFile } from '../api'
|
||||||
import Gallery from '../components/Gallery'
|
import Gallery from '../components/Gallery'
|
||||||
import Lightbox from '../components/Lightbox'
|
import Lightbox from '../components/Lightbox'
|
||||||
import SelectionBar from '../components/SelectionBar'
|
import SelectionBar, { fmtBytes } from '../components/SelectionBar'
|
||||||
import Stars from '../components/Stars'
|
import Stars from '../components/Stars'
|
||||||
|
import Thumbs from '../components/Thumbs'
|
||||||
|
import useLightbox from '../useLightbox'
|
||||||
import useSelection from '../useSelection'
|
import useSelection from '../useSelection'
|
||||||
|
|
||||||
|
function fmtEta(seconds) {
|
||||||
|
if (!isFinite(seconds) || seconds < 0) return ''
|
||||||
|
if (seconds < 60) return `${Math.ceil(seconds)}s`
|
||||||
|
if (seconds < 3600) return `${Math.ceil(seconds / 60)} min`
|
||||||
|
return `${Math.floor(seconds / 3600)}h ${Math.ceil((seconds % 3600) / 60)} min`
|
||||||
|
}
|
||||||
|
|
||||||
const UPLOAD_CONCURRENCY = 3
|
const UPLOAD_CONCURRENCY = 3
|
||||||
|
|
||||||
function UploadZone({ albumId, onUploaded }) {
|
function UploadZone({ albumId, onUploaded }) {
|
||||||
const [queue, setQueue] = useState([])
|
const [queue, setQueue] = useState([])
|
||||||
const [dragging, setDragging] = useState(false)
|
const [dragging, setDragging] = useState(false)
|
||||||
|
const [speed, setSpeed] = useState(0)
|
||||||
const inputRef = useRef(null)
|
const inputRef = useRef(null)
|
||||||
const running = useRef(0)
|
const running = useRef(0)
|
||||||
const pending = useRef([])
|
const pending = useRef([])
|
||||||
const lastRefresh = useRef(0)
|
const lastRefresh = useRef(0)
|
||||||
|
const loadedRef = useRef(0)
|
||||||
|
|
||||||
|
const totalBytes = queue.reduce((sum, item) => sum + item.file.size, 0)
|
||||||
|
const loadedBytes = queue.reduce(
|
||||||
|
(sum, item) =>
|
||||||
|
sum + (item.status === 'done' ? item.file.size : (item.progress || 0) * item.file.size),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
loadedRef.current = loadedBytes
|
||||||
|
const active = queue.some((item) =>
|
||||||
|
['uploading', 'queued', 'checking'].includes(item.status),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sample throughput once a second (EMA-smoothed) while uploads run.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!active) {
|
||||||
|
setSpeed(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let last = { loaded: loadedRef.current, time: Date.now() }
|
||||||
|
const timer = setInterval(() => {
|
||||||
|
const now = Date.now()
|
||||||
|
const instant = (loadedRef.current - last.loaded) / ((now - last.time) / 1000)
|
||||||
|
last = { loaded: loadedRef.current, time: now }
|
||||||
|
setSpeed((prev) => (prev > 0 ? prev * 0.7 + instant * 0.3 : instant))
|
||||||
|
}, 1000)
|
||||||
|
return () => clearInterval(timer)
|
||||||
|
}, [active])
|
||||||
|
|
||||||
// Refresh the album at most every 5s during a bulk upload (the processing
|
// Refresh the album at most every 5s during a bulk upload (the processing
|
||||||
// poll keeps it fresh anyway), plus once when the queue drains.
|
// poll keeps it fresh anyway), plus once when the queue drains.
|
||||||
@@ -31,23 +69,27 @@ function UploadZone({ albumId, onUploaded }) {
|
|||||||
while (running.current < UPLOAD_CONCURRENCY && pending.current.length > 0) {
|
while (running.current < UPLOAD_CONCURRENCY && pending.current.length > 0) {
|
||||||
const item = pending.current.shift()
|
const item = pending.current.shift()
|
||||||
running.current += 1
|
running.current += 1
|
||||||
setQueue((q) =>
|
const update = (patch) =>
|
||||||
q.map((x) => (x.key === item.key ? { ...x, status: 'uploading' } : x)),
|
setQueue((q) => q.map((x) => (x.key === item.key ? { ...x, ...patch } : x)))
|
||||||
)
|
const transfer = async () => {
|
||||||
const url = `/api/albums/${albumId}/photos?filename=${encodeURIComponent(item.file.name)}`
|
// Hash locally first: content the album already has is skipped
|
||||||
uploadFile(url, item.file, (p) =>
|
// without transferring a single byte.
|
||||||
setQueue((q) => q.map((x) => (x.key === item.key ? { ...x, progress: p } : x))),
|
update({ status: 'checking' })
|
||||||
)
|
try {
|
||||||
.then(() =>
|
const hash = await sha256Hex(item.file)
|
||||||
setQueue((q) =>
|
await api(`/api/albums/${albumId}/photos/by-hash/${hash}`)
|
||||||
q.map((x) => (x.key === item.key ? { ...x, status: 'done', progress: 1 } : x)),
|
update({ status: 'skipped', progress: 1 })
|
||||||
),
|
return
|
||||||
)
|
} catch {
|
||||||
.catch((e) =>
|
// 404 (not there yet) or hashing unavailable — upload normally.
|
||||||
setQueue((q) =>
|
}
|
||||||
q.map((x) => (x.key === item.key ? { ...x, status: 'error', error: e.message } : x)),
|
update({ status: 'uploading' })
|
||||||
),
|
const url = `/api/albums/${albumId}/photos?filename=${encodeURIComponent(item.file.name)}`
|
||||||
)
|
await uploadFile(url, item.file, (p) => update({ progress: p }))
|
||||||
|
update({ status: 'done', progress: 1 })
|
||||||
|
}
|
||||||
|
transfer()
|
||||||
|
.catch((e) => update({ status: 'error', error: e.message }))
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
running.current -= 1
|
running.current -= 1
|
||||||
refresh()
|
refresh()
|
||||||
@@ -95,6 +137,22 @@ function UploadZone({ albumId, onUploaded }) {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<p>Drop RAWs or JPGs here, or click to select</p>
|
<p>Drop RAWs or JPGs here, or click to select</p>
|
||||||
|
{queue.length > 0 && (
|
||||||
|
<div className="upload-summary" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<progress className="upload-total" value={loadedBytes} max={totalBytes || 1} />
|
||||||
|
<span className="muted">
|
||||||
|
{queue.filter((i) => i.status === 'done' || i.status === 'skipped').length} /{' '}
|
||||||
|
{queue.length} files ·{' '}
|
||||||
|
{fmtBytes(loadedBytes)} of {fmtBytes(totalBytes)}
|
||||||
|
{active && speed > 0 && (
|
||||||
|
<>
|
||||||
|
{' · '}
|
||||||
|
{fmtBytes(speed)}/s · ~{fmtEta((totalBytes - loadedBytes) / speed)} left
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{queue.length > 0 && (
|
{queue.length > 0 && (
|
||||||
<ul className="upload-list" onClick={(e) => e.stopPropagation()}>
|
<ul className="upload-list" onClick={(e) => e.stopPropagation()}>
|
||||||
{queue.map((item) => (
|
{queue.map((item) => (
|
||||||
@@ -102,6 +160,10 @@ function UploadZone({ albumId, onUploaded }) {
|
|||||||
<span className="upload-name">{item.file.name}</span>
|
<span className="upload-name">{item.file.name}</span>
|
||||||
{item.status === 'error' ? (
|
{item.status === 'error' ? (
|
||||||
<span className="error">{item.error}</span>
|
<span className="error">{item.error}</span>
|
||||||
|
) : item.status === 'skipped' ? (
|
||||||
|
<span className="muted">already uploaded</span>
|
||||||
|
) : item.status === 'checking' ? (
|
||||||
|
<span className="muted">checking…</span>
|
||||||
) : (
|
) : (
|
||||||
<progress value={item.progress} max="1" />
|
<progress value={item.progress} max="1" />
|
||||||
)}
|
)}
|
||||||
@@ -176,7 +238,8 @@ function SharesPanel({ albumId }) {
|
|||||||
? ` · expires ${new Date(s.expires_at).toLocaleDateString()}`
|
? ` · expires ${new Date(s.expires_at).toLocaleDateString()}`
|
||||||
: ' · never expires'}
|
: ' · never expires'}
|
||||||
{' · '}
|
{' · '}
|
||||||
{s.rating_count} ratings, {s.tag_count} tags
|
{s.rating_count} ratings, {s.tag_count} tags, 👍 {s.accept_count} 👎{' '}
|
||||||
|
{s.reject_count}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="row">
|
<div className="row">
|
||||||
@@ -260,9 +323,6 @@ export default function AlbumPage() {
|
|||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [detail, setDetail] = useState(null)
|
const [detail, setDetail] = useState(null)
|
||||||
const [error, setError] = useState(null)
|
const [error, setError] = useState(null)
|
||||||
// Track the open photo by id, not index — the polling refetch can reorder
|
|
||||||
// the array underneath an open lightbox.
|
|
||||||
const [lightboxId, setLightboxId] = useState(null)
|
|
||||||
|
|
||||||
const load = useCallback(
|
const load = useCallback(
|
||||||
() => api(`/api/albums/${id}`).then(setDetail).catch((e) => setError(e.message)),
|
() => api(`/api/albums/${id}`).then(setDetail).catch((e) => setError(e.message)),
|
||||||
@@ -282,14 +342,7 @@ export default function AlbumPage() {
|
|||||||
|
|
||||||
const ready = (detail?.photos ?? []).filter((p) => p.status === 'ready')
|
const ready = (detail?.photos ?? []).filter((p) => p.status === 'ready')
|
||||||
const { selected, toggle, selectAll, clear, selectedBytes, totalBytes } = useSelection(ready)
|
const { selected, toggle, selectAll, clear, selectedBytes, totalBytes } = useSelection(ready)
|
||||||
const lightboxIndex = ready.findIndex((p) => p.id === lightboxId)
|
const lightbox = useLightbox(ready)
|
||||||
|
|
||||||
// If the open photo leaves the ready list (deleted elsewhere, reprocess),
|
|
||||||
// close for good — otherwise the lightbox would pop back open when the
|
|
||||||
// photo returns to ready.
|
|
||||||
useEffect(() => {
|
|
||||||
if (lightboxId && lightboxIndex < 0) setLightboxId(null)
|
|
||||||
}, [lightboxId, lightboxIndex])
|
|
||||||
|
|
||||||
if (error) return <p className="error">{error}</p>
|
if (error) return <p className="error">{error}</p>
|
||||||
if (!detail) return <p className="muted">Loading…</p>
|
if (!detail) return <p className="muted">Loading…</p>
|
||||||
@@ -319,7 +372,7 @@ export default function AlbumPage() {
|
|||||||
|
|
||||||
const removePhoto = async (photoId) => {
|
const removePhoto = async (photoId) => {
|
||||||
if (!confirm('Delete this photo?')) return
|
if (!confirm('Delete this photo?')) return
|
||||||
setLightboxId(null)
|
lightbox.close()
|
||||||
await api(`/api/photos/${photoId}`, { method: 'DELETE' })
|
await api(`/api/photos/${photoId}`, { method: 'DELETE' })
|
||||||
load()
|
load()
|
||||||
}
|
}
|
||||||
@@ -376,15 +429,20 @@ export default function AlbumPage() {
|
|||||||
|
|
||||||
<Gallery
|
<Gallery
|
||||||
photos={ready}
|
photos={ready}
|
||||||
onOpen={(i) => setLightboxId(ready[i].id)}
|
onOpen={lightbox.openAt}
|
||||||
selected={selected}
|
selected={selected}
|
||||||
onToggleSelect={toggle}
|
onToggleSelect={toggle}
|
||||||
overlay={(p) => {
|
overlay={(p) => {
|
||||||
const avg = avgRating(p.id)
|
const avg = avgRating(p.id)
|
||||||
const tagCount = feedback[p.id]?.tags.length || 0
|
const tagCount = feedback[p.id]?.tags.length || 0
|
||||||
if (avg === null && tagCount === 0) return null
|
const verdicts = feedback[p.id]?.verdicts || []
|
||||||
|
const accepts = verdicts.filter((v) => v.verdict === 'accept').length
|
||||||
|
const rejects = verdicts.length - accepts
|
||||||
|
if (avg === null && tagCount === 0 && accepts === 0 && rejects === 0) return null
|
||||||
return (
|
return (
|
||||||
<div className="g-overlay">
|
<div className="g-overlay">
|
||||||
|
{accepts > 0 && <span>👍 {accepts}</span>}
|
||||||
|
{rejects > 0 && <span>👎 {rejects}</span>}
|
||||||
{avg !== null && <span>★ {avg.toFixed(1)}</span>}
|
{avg !== null && <span>★ {avg.toFixed(1)}</span>}
|
||||||
{tagCount > 0 && <span># {tagCount}</span>}
|
{tagCount > 0 && <span># {tagCount}</span>}
|
||||||
</div>
|
</div>
|
||||||
@@ -400,26 +458,34 @@ export default function AlbumPage() {
|
|||||||
total={ready.length}
|
total={ready.length}
|
||||||
selectedBytes={selectedBytes}
|
selectedBytes={selectedBytes}
|
||||||
totalBytes={totalBytes}
|
totalBytes={totalBytes}
|
||||||
onSelectAll={selectAll}
|
onSelectAll={() => selectAll(ready)}
|
||||||
onClear={clear}
|
onClear={clear}
|
||||||
onDownload={() => postDownload(`/api/albums/${id}/zip`, [...selected].join(','))}
|
onDownload={() => postDownload(`/api/albums/${id}/zip`, [...selected].join(','))}
|
||||||
onDownloadAll={() => postDownload(`/api/albums/${id}/zip`)}
|
onDownloadAll={() => postDownload(`/api/albums/${id}/zip`)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{lightboxIndex >= 0 && (
|
{lightbox.index >= 0 && (
|
||||||
<Lightbox
|
<Lightbox
|
||||||
photos={ready}
|
photos={ready}
|
||||||
index={lightboxIndex}
|
index={lightbox.index}
|
||||||
onClose={() => setLightboxId(null)}
|
onClose={lightbox.close}
|
||||||
onNav={(i) => setLightboxId(ready[i].id)}
|
onNav={lightbox.openAt}
|
||||||
|
actions={[
|
||||||
|
{ keys: ['s'], help: ['S', 'select for download'], run: (p) => toggle(p.id) },
|
||||||
|
]}
|
||||||
footer={(p) => {
|
footer={(p) => {
|
||||||
const fb = feedback[p.id] || { ratings: [], tags: [] }
|
const fb = feedback[p.id] || { ratings: [], verdicts: [], tags: [] }
|
||||||
return (
|
return (
|
||||||
<div className="admin-footer">
|
<div className="admin-footer">
|
||||||
<div className="feedback">
|
<div className="feedback">
|
||||||
{fb.ratings.length === 0 && fb.tags.length === 0 && (
|
{fb.ratings.length === 0 && fb.verdicts.length === 0 && fb.tags.length === 0 && (
|
||||||
<span className="muted">No client feedback yet</span>
|
<span className="muted">No client feedback yet</span>
|
||||||
)}
|
)}
|
||||||
|
{fb.verdicts.map((v, i) => (
|
||||||
|
<span key={`v${i}`} className="feedback-item">
|
||||||
|
{v.share_label || 'client'}: <Thumbs value={v.verdict} small />
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
{fb.ratings.map((r, i) => (
|
{fb.ratings.map((r, i) => (
|
||||||
<span key={`r${i}`} className="feedback-item">
|
<span key={`r${i}`} className="feedback-item">
|
||||||
{r.share_label || 'client'}: <Stars value={r.rating} small />
|
{r.share_label || 'client'}: <Stars value={r.rating} small />
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react'
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
import { api, postDownload } from '../api'
|
import { api, postDownload } from '../api'
|
||||||
import Gallery from '../components/Gallery'
|
import Gallery from '../components/Gallery'
|
||||||
@@ -6,18 +6,43 @@ import Lightbox from '../components/Lightbox'
|
|||||||
import SelectionBar from '../components/SelectionBar'
|
import SelectionBar from '../components/SelectionBar'
|
||||||
import Stars from '../components/Stars'
|
import Stars from '../components/Stars'
|
||||||
import TagEditor from '../components/TagEditor'
|
import TagEditor from '../components/TagEditor'
|
||||||
|
import Thumbs from '../components/Thumbs'
|
||||||
|
import useLightbox from '../useLightbox'
|
||||||
import useSelection from '../useSelection'
|
import useSelection from '../useSelection'
|
||||||
|
|
||||||
|
const FILTERS = [
|
||||||
|
{ key: 'all', label: 'All' },
|
||||||
|
{ key: 'accept', label: '👍' },
|
||||||
|
{ key: 'reject', label: '👎' },
|
||||||
|
{ key: 'undecided', label: 'Undecided' },
|
||||||
|
]
|
||||||
|
|
||||||
|
const matchesFilter = (photo, key) =>
|
||||||
|
key === 'all' || (key === 'undecided' ? !photo.my_verdict : photo.my_verdict === key)
|
||||||
|
|
||||||
export default function SharePage() {
|
export default function SharePage() {
|
||||||
const { token } = useParams()
|
const { token } = useParams()
|
||||||
const [view, setView] = useState(null)
|
const [view, setView] = useState(null)
|
||||||
const [error, setError] = useState(null)
|
const [error, setError] = useState(null)
|
||||||
const [password, setPassword] = useState('')
|
const [password, setPassword] = useState('')
|
||||||
const [unlockError, setUnlockError] = useState(null)
|
const [unlockError, setUnlockError] = useState(null)
|
||||||
const [lightbox, setLightbox] = useState(-1)
|
const [filter, setFilter] = useState('all')
|
||||||
const { selected, toggle, selectAll, clear, selectedBytes, totalBytes } = useSelection(
|
|
||||||
view?.photos ?? [],
|
const photos = view?.photos ?? []
|
||||||
)
|
const visible = useMemo(() => photos.filter((p) => matchesFilter(p, filter)), [photos, filter])
|
||||||
|
const filterCounts = useMemo(() => {
|
||||||
|
const counts = { all: photos.length, accept: 0, reject: 0, undecided: 0 }
|
||||||
|
for (const p of photos) counts[p.my_verdict ?? 'undecided'] += 1
|
||||||
|
return counts
|
||||||
|
}, [photos])
|
||||||
|
|
||||||
|
// Selection spans the whole album (so switching filters keeps it), while
|
||||||
|
// the selection bar describes only the current view: its counts, bytes and
|
||||||
|
// downloads cover the visible photos, and "select all" adds them.
|
||||||
|
const { selected, toggle, selectAll, clear } = useSelection(photos)
|
||||||
|
const visibleSelected = visible.filter((p) => selected.has(p.id))
|
||||||
|
const sumBytes = (list) => list.reduce((sum, p) => sum + p.size_bytes, 0)
|
||||||
|
const lightbox = useLightbox(visible)
|
||||||
|
|
||||||
const load = useCallback(
|
const load = useCallback(
|
||||||
() => api(`/api/share/${token}`).then(setView).catch((e) => setError(e.message)),
|
() => api(`/api/share/${token}`).then(setView).catch((e) => setError(e.message)),
|
||||||
@@ -45,29 +70,75 @@ export default function SharePage() {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
const setRating = async (photo, rating) => {
|
// Optimistic write: patch local state, PUT, reload from the server on
|
||||||
patchPhoto(photo.id, { my_rating: rating || null })
|
// failure to undo the patch.
|
||||||
|
const saveFeedback = async (photo, patch, endpoint, body) => {
|
||||||
|
patchPhoto(photo.id, patch)
|
||||||
try {
|
try {
|
||||||
await api(`/api/share/${token}/photos/${photo.id}/rating`, {
|
await api(`/api/share/${token}/photos/${photo.id}/${endpoint}`, { method: 'PUT', body })
|
||||||
method: 'PUT',
|
|
||||||
body: { rating },
|
|
||||||
})
|
|
||||||
} catch {
|
} catch {
|
||||||
load()
|
load()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const setRating = (photo, rating) =>
|
||||||
|
saveFeedback(photo, { my_rating: rating || null }, 'rating', { rating })
|
||||||
|
const setVerdict = (photo, verdict) =>
|
||||||
|
saveFeedback(photo, { my_verdict: verdict }, 'verdict', { verdict })
|
||||||
|
const setTags = (photo, tags) => saveFeedback(photo, { my_tags: tags }, 'tags', { tags })
|
||||||
|
|
||||||
|
// Keyboard votes navigate away from the photo they change, so a transient
|
||||||
|
// toast names what just happened to it — without it the jump reads as
|
||||||
|
// "did that register?".
|
||||||
|
const [flash, setFlash] = useState(null)
|
||||||
|
const flashSeq = useRef(0)
|
||||||
|
const flashTimer = useRef()
|
||||||
|
const showFlash = (text) => {
|
||||||
|
flashSeq.current += 1
|
||||||
|
setFlash({ text, key: flashSeq.current })
|
||||||
|
clearTimeout(flashTimer.current)
|
||||||
|
flashTimer.current = setTimeout(() => setFlash(null), 1400)
|
||||||
|
}
|
||||||
|
useEffect(() => () => clearTimeout(flashTimer.current), [])
|
||||||
|
|
||||||
|
// Culling flow: vote, then advance to the photo that was next in the
|
||||||
|
// current view. Under a filter that hides the voted photo, the advance
|
||||||
|
// target stays visible, so the run continues seamlessly.
|
||||||
|
const voteAndAdvance = (photo, verdict) => {
|
||||||
|
const next = visible[lightbox.index + 1]
|
||||||
|
setVerdict(photo, verdict)
|
||||||
|
showFlash(
|
||||||
|
verdict === 'accept'
|
||||||
|
? `👍 ${photo.filename}`
|
||||||
|
: verdict === 'reject'
|
||||||
|
? `👎 ${photo.filename}`
|
||||||
|
: `↺ ${photo.filename} cleared`,
|
||||||
|
)
|
||||||
|
if (next) lightbox.show(next.id)
|
||||||
|
}
|
||||||
|
|
||||||
const setTags = async (photo, tags) => {
|
const keyActions = [
|
||||||
patchPhoto(photo.id, { my_tags: tags })
|
{ keys: ['p'], help: ['P', 'accept and go to next'], run: (p) => voteAndAdvance(p, 'accept') },
|
||||||
try {
|
{ keys: ['x'], help: ['X', 'reject and go to next'], run: (p) => voteAndAdvance(p, 'reject') },
|
||||||
await api(`/api/share/${token}/photos/${photo.id}/tags`, {
|
{
|
||||||
method: 'PUT',
|
keys: ['u'],
|
||||||
body: { tags },
|
help: ['U', 'clear accept / reject'],
|
||||||
})
|
// When clearing hides the photo from the current filter, advance like
|
||||||
} catch {
|
// a vote so the lightbox doesn't just close.
|
||||||
load()
|
run: (p) =>
|
||||||
}
|
matchesFilter({ my_verdict: null }, filter)
|
||||||
}
|
? setVerdict(p, null)
|
||||||
|
: voteAndAdvance(p, null),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
keys: ['1', '2', '3', '4', '5'],
|
||||||
|
help: ['1–5', 'star rating'],
|
||||||
|
run: (p, key) => setRating(p, Number(key)),
|
||||||
|
},
|
||||||
|
{ keys: ['0'], help: ['0', 'clear star rating'], run: (p) => setRating(p, 0) },
|
||||||
|
...(view?.allow_download
|
||||||
|
? [{ keys: ['s'], help: ['S', 'select for download'], run: (p) => toggle(p.id) }]
|
||||||
|
: []),
|
||||||
|
]
|
||||||
|
|
||||||
if (error) return <div className="center-page">{error}</div>
|
if (error) return <div className="center-page">{error}</div>
|
||||||
if (!view) return <div className="center-page">Loading…</div>
|
if (!view) return <div className="center-page">Loading…</div>
|
||||||
@@ -100,49 +171,76 @@ export default function SharePage() {
|
|||||||
<h1>{view.album_name}</h1>
|
<h1>{view.album_name}</h1>
|
||||||
{view.album_description && <p className="muted">{view.album_description}</p>}
|
{view.album_description && <p className="muted">{view.album_description}</p>}
|
||||||
<p className="muted">
|
<p className="muted">
|
||||||
{view.photos.length} photo{view.photos.length === 1 ? '' : 's'} · click a photo to view,
|
{photos.length} photo{photos.length === 1 ? '' : 's'} · click a photo to view, rate and
|
||||||
rate and tag
|
tag · press <kbd>?</kbd> in the viewer for shortcuts
|
||||||
</p>
|
</p>
|
||||||
|
{photos.length > 0 && (
|
||||||
|
<div className="filter-bar">
|
||||||
|
{FILTERS.map((f) => (
|
||||||
|
<button
|
||||||
|
key={f.key}
|
||||||
|
className={`filter-chip${filter === f.key ? ' active' : ''}`}
|
||||||
|
onClick={() => setFilter(f.key)}
|
||||||
|
>
|
||||||
|
{f.label} {filterCounts[f.key]}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</header>
|
</header>
|
||||||
<main className="page">
|
<main className="page">
|
||||||
<Gallery
|
<Gallery
|
||||||
photos={view.photos}
|
photos={visible}
|
||||||
onOpen={setLightbox}
|
onOpen={lightbox.openAt}
|
||||||
selected={view.allow_download ? selected : undefined}
|
selected={view.allow_download ? selected : undefined}
|
||||||
onToggleSelect={view.allow_download ? toggle : undefined}
|
onToggleSelect={view.allow_download ? toggle : undefined}
|
||||||
overlay={(p) =>
|
overlay={(p) =>
|
||||||
p.my_rating || p.my_tags.length > 0 ? (
|
p.my_verdict || p.my_rating || p.my_tags.length > 0 ? (
|
||||||
<div className="g-overlay">
|
<div className="g-overlay">
|
||||||
|
{p.my_verdict && <span>{p.my_verdict === 'accept' ? '👍' : '👎'}</span>}
|
||||||
{p.my_rating && <span>★ {p.my_rating}</span>}
|
{p.my_rating && <span>★ {p.my_rating}</span>}
|
||||||
{p.my_tags.length > 0 && <span># {p.my_tags.length}</span>}
|
{p.my_tags.length > 0 && <span># {p.my_tags.length}</span>}
|
||||||
</div>
|
</div>
|
||||||
) : null
|
) : null
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
{view.photos.length === 0 && (
|
{photos.length === 0 && (
|
||||||
<p className="center-page muted">Nothing here yet — check back soon.</p>
|
<p className="center-page muted">Nothing here yet — check back soon.</p>
|
||||||
)}
|
)}
|
||||||
|
{photos.length > 0 && visible.length === 0 && (
|
||||||
|
<p className="center-page muted">No photos match this filter.</p>
|
||||||
|
)}
|
||||||
</main>
|
</main>
|
||||||
{view.allow_download && (
|
{view.allow_download && (
|
||||||
<SelectionBar
|
<SelectionBar
|
||||||
count={selected.size}
|
count={visibleSelected.length}
|
||||||
total={view.photos.length}
|
total={visible.length}
|
||||||
selectedBytes={selectedBytes}
|
selectedBytes={sumBytes(visibleSelected)}
|
||||||
totalBytes={totalBytes}
|
totalBytes={sumBytes(visible)}
|
||||||
onSelectAll={selectAll}
|
onSelectAll={() => selectAll(visible)}
|
||||||
onClear={clear}
|
onClear={clear}
|
||||||
onDownload={() => postDownload(`/api/share/${token}/zip`, [...selected].join(','))}
|
onDownload={() =>
|
||||||
onDownloadAll={() => postDownload(`/api/share/${token}/zip`)}
|
postDownload(`/api/share/${token}/zip`, visibleSelected.map((p) => p.id).join(','))
|
||||||
|
}
|
||||||
|
onDownloadAll={() =>
|
||||||
|
// Under a filter, "download all" means all photos shown.
|
||||||
|
postDownload(
|
||||||
|
`/api/share/${token}/zip`,
|
||||||
|
filter === 'all' ? '' : visible.map((p) => p.id).join(','),
|
||||||
|
)
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{lightbox >= 0 && (
|
{lightbox.index >= 0 && (
|
||||||
<Lightbox
|
<Lightbox
|
||||||
photos={view.photos}
|
photos={visible}
|
||||||
index={lightbox}
|
index={lightbox.index}
|
||||||
onClose={() => setLightbox(-1)}
|
onClose={lightbox.close}
|
||||||
onNav={setLightbox}
|
onNav={lightbox.openAt}
|
||||||
|
actions={keyActions}
|
||||||
footer={(p) => (
|
footer={(p) => (
|
||||||
<div className="client-footer">
|
<div className="client-footer">
|
||||||
|
<Thumbs value={p.my_verdict} onChange={(v) => setVerdict(p, v)} />
|
||||||
<Stars value={p.my_rating || 0} onChange={(r) => setRating(p, r)} />
|
<Stars value={p.my_rating || 0} onChange={(r) => setRating(p, r)} />
|
||||||
<TagEditor tags={p.my_tags} onChange={(tags) => setTags(p, tags)} />
|
<TagEditor tags={p.my_tags} onChange={(tags) => setTags(p, tags)} />
|
||||||
{view.allow_download && (
|
{view.allow_download && (
|
||||||
@@ -164,6 +262,11 @@ export default function SharePage() {
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{flash && (
|
||||||
|
<div key={flash.key} className="action-flash">
|
||||||
|
{flash.text}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+142
-13
@@ -191,15 +191,17 @@ input:focus {
|
|||||||
/* justified gallery */
|
/* justified gallery */
|
||||||
.gallery {
|
.gallery {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-wrap: wrap;
|
flex-direction: column;
|
||||||
gap: 6px;
|
gap: 6px;
|
||||||
margin: 1rem 0;
|
margin: 1rem 0;
|
||||||
}
|
}
|
||||||
|
.g-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
.g-item {
|
.g-item {
|
||||||
position: relative;
|
position: relative;
|
||||||
height: 240px;
|
flex: none;
|
||||||
flex-grow: calc(var(--ar) * 100);
|
|
||||||
flex-basis: calc(var(--ar) * 240px);
|
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
@@ -211,11 +213,6 @@ input:focus {
|
|||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
.g-spacer {
|
|
||||||
flex-grow: 1000000;
|
|
||||||
flex-basis: 0;
|
|
||||||
height: 0;
|
|
||||||
}
|
|
||||||
.g-overlay {
|
.g-overlay {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
@@ -310,6 +307,18 @@ input:focus {
|
|||||||
.upload-zone p {
|
.upload-zone p {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
.upload-summary {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.3rem;
|
||||||
|
margin-top: 1rem;
|
||||||
|
cursor: default;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
.upload-total {
|
||||||
|
width: 100%;
|
||||||
|
accent-color: var(--accent);
|
||||||
|
}
|
||||||
.upload-list {
|
.upload-list {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
margin: 1rem 0 0;
|
margin: 1rem 0 0;
|
||||||
@@ -525,6 +534,130 @@ progress {
|
|||||||
cursor: default;
|
cursor: default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* thumbs (accept / reject) */
|
||||||
|
.thumbs {
|
||||||
|
display: inline-flex;
|
||||||
|
gap: 0.15rem;
|
||||||
|
}
|
||||||
|
.thumb {
|
||||||
|
background: none;
|
||||||
|
border: none;
|
||||||
|
font-size: 1.35rem;
|
||||||
|
line-height: 1;
|
||||||
|
padding: 0 0.15rem;
|
||||||
|
cursor: pointer;
|
||||||
|
filter: grayscale(1);
|
||||||
|
opacity: 0.4;
|
||||||
|
transition: opacity 0.12s;
|
||||||
|
}
|
||||||
|
.thumb:hover:enabled {
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
.thumb.active {
|
||||||
|
filter: none;
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
.thumb:disabled {
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
.thumbs-small .thumb {
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* verdict filter */
|
||||||
|
.filter-bar {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-top: 0.9rem;
|
||||||
|
}
|
||||||
|
.filter-chip {
|
||||||
|
background: var(--panel);
|
||||||
|
border: 1px solid var(--panel-2);
|
||||||
|
border-radius: 999px;
|
||||||
|
color: var(--muted);
|
||||||
|
padding: 0.25rem 0.8rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.filter-chip.active {
|
||||||
|
background: var(--panel-2);
|
||||||
|
color: var(--text);
|
||||||
|
border-color: var(--accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* transient action feedback (keyboard votes that navigate away) */
|
||||||
|
.action-flash {
|
||||||
|
position: fixed;
|
||||||
|
top: 3rem;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
z-index: 110;
|
||||||
|
background: var(--panel);
|
||||||
|
border: 1px solid var(--panel-2);
|
||||||
|
border-radius: 999px;
|
||||||
|
padding: 0.35rem 1rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
white-space: nowrap;
|
||||||
|
pointer-events: none;
|
||||||
|
animation: flash-fade 1.4s ease forwards;
|
||||||
|
}
|
||||||
|
@keyframes flash-fade {
|
||||||
|
0% {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translate(-50%, -6px);
|
||||||
|
}
|
||||||
|
8%,
|
||||||
|
70% {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translate(-50%, 0);
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translate(-50%, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* lightbox shortcut help */
|
||||||
|
.lb-help {
|
||||||
|
position: absolute;
|
||||||
|
inset: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
background: rgba(8, 9, 11, 0.6);
|
||||||
|
z-index: 102;
|
||||||
|
}
|
||||||
|
.lb-help-card {
|
||||||
|
background: var(--panel);
|
||||||
|
border: 1px solid var(--panel-2);
|
||||||
|
border-radius: 12px;
|
||||||
|
padding: 1.1rem 1.5rem 1.25rem;
|
||||||
|
min-width: 280px;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
.lb-help-card h3 {
|
||||||
|
font-size: 0.95rem;
|
||||||
|
margin: 0 0 0.6rem;
|
||||||
|
}
|
||||||
|
.lb-help-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: baseline;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1.5rem;
|
||||||
|
padding: 0.18rem 0;
|
||||||
|
font-size: 0.88rem;
|
||||||
|
}
|
||||||
|
kbd {
|
||||||
|
background: var(--panel-2);
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 0.08rem 0.45rem;
|
||||||
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
/* tags */
|
/* tags */
|
||||||
.tag-editor {
|
.tag-editor {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
@@ -556,10 +689,6 @@ progress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 700px) {
|
@media (max-width: 700px) {
|
||||||
.g-item {
|
|
||||||
height: 160px;
|
|
||||||
flex-basis: calc(var(--ar) * 160px);
|
|
||||||
}
|
|
||||||
.lb-stage {
|
.lb-stage {
|
||||||
padding: 0 0.5rem;
|
padding: 0 0.5rem;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
// Lightbox state tracked by photo id, not index — the list can reorder
|
||||||
|
// (polling refetch) or shrink (filter change, delete) underneath an open
|
||||||
|
// lightbox. When the open photo leaves the list, close for good — otherwise
|
||||||
|
// the lightbox would pop back open when the photo returns to the list.
|
||||||
|
export default function useLightbox(photos) {
|
||||||
|
const [openId, setOpenId] = useState(null)
|
||||||
|
const index = openId ? photos.findIndex((p) => p.id === openId) : -1
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (openId && index < 0) setOpenId(null)
|
||||||
|
}, [openId, index])
|
||||||
|
|
||||||
|
return {
|
||||||
|
index,
|
||||||
|
openAt: (i) => setOpenId(photos[i].id),
|
||||||
|
show: (id) => setOpenId(id),
|
||||||
|
close: () => setOpenId(null),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,7 +23,10 @@ export default function useSelection(photos) {
|
|||||||
return next
|
return next
|
||||||
})
|
})
|
||||||
|
|
||||||
const selectAll = () => setSelected(new Set(photos.map((p) => p.id)))
|
// Adds `list` (the caller's currently visible photos) to the selection —
|
||||||
|
// additive, so selecting all of one filtered view keeps picks from another.
|
||||||
|
const selectAll = (list) =>
|
||||||
|
setSelected((prev) => new Set([...prev, ...list.map((p) => p.id)]))
|
||||||
const clear = () => setSelected(new Set())
|
const clear = () => setSelected(new Set())
|
||||||
const selectedBytes = photos.reduce((sum, p) => sum + (selected.has(p.id) ? p.size_bytes : 0), 0)
|
const selectedBytes = photos.reduce((sum, p) => sum + (selected.has(p.id) ? p.size_bytes : 0), 0)
|
||||||
const totalBytes = photos.reduce((sum, p) => sum + p.size_bytes, 0)
|
const totalBytes = photos.reduce((sum, p) => sum + p.size_bytes, 0)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
-- Content hashes: sha256 powers duplicate-upload detection (same content in
|
||||||
|
-- the same album is returned instead of copied); crc32 lets zip downloads
|
||||||
|
-- stream originals straight from S3 without a local spool pass.
|
||||||
|
-- Both are null for photos uploaded before this migration; they self-heal on
|
||||||
|
-- reprocess (sha256 + crc32) and on zip download (crc32).
|
||||||
|
alter table photos add column sha256 text;
|
||||||
|
alter table photos add column crc32 bigint;
|
||||||
|
|
||||||
|
create unique index photos_album_sha_uidx on photos (album_id, sha256)
|
||||||
|
where sha256 is not null;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
-- Client accept/reject votes, one per (link, photo) — a separate axis from
|
||||||
|
-- the 1-5 star rating so a photo can be e.g. accepted but unrated.
|
||||||
|
create table verdicts (
|
||||||
|
share_id uuid not null references shares(id) on delete cascade,
|
||||||
|
photo_id uuid not null references photos(id) on delete cascade,
|
||||||
|
verdict text not null check (verdict in ('accept', 'reject')),
|
||||||
|
updated_at timestamptz not null default now(),
|
||||||
|
primary key (share_id, photo_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Cascaded photo deletes fire per-row FK triggers; without this each one
|
||||||
|
-- sequential-scans the table.
|
||||||
|
create index verdicts_photo_idx on verdicts (photo_id);
|
||||||
+37
-4
@@ -62,7 +62,24 @@ pub async fn process_photo(state: &AppState, photo_id: Uuid) -> anyhow::Result<(
|
|||||||
.unwrap_or("bin")
|
.unwrap_or("bin")
|
||||||
.to_lowercase();
|
.to_lowercase();
|
||||||
let src_path = dir.path().join(format!("original.{extension}"));
|
let src_path = dir.path().join(format!("original.{extension}"));
|
||||||
download(state, &s3::original_key(photo_id, &photo.filename), &src_path).await?;
|
let (sha256, crc32) =
|
||||||
|
download(state, &s3::original_key(photo_id, &photo.filename), &src_path).await?;
|
||||||
|
if photo.sha256.is_none() || photo.crc32.is_none() {
|
||||||
|
// Best-effort backfill for pre-hash photos; a duplicate in the same
|
||||||
|
// album trips the unique index, which is fine — skip silently.
|
||||||
|
if let Err(e) = sqlx::query(
|
||||||
|
"update photos set sha256 = coalesce(sha256, $2), crc32 = coalesce(crc32, $3)
|
||||||
|
where id = $1",
|
||||||
|
)
|
||||||
|
.bind(photo_id)
|
||||||
|
.bind(&sha256)
|
||||||
|
.bind(crc32)
|
||||||
|
.execute(&state.db)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
tracing::debug!("hash backfill skipped for {photo_id}: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let meta = exif_metadata(&src_path).await?;
|
let meta = exif_metadata(&src_path).await?;
|
||||||
|
|
||||||
@@ -103,7 +120,11 @@ pub async fn process_photo(state: &AppState, photo_id: Uuid) -> anyhow::Result<(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn download(state: &AppState, key: &str, path: &Path) -> anyhow::Result<()> {
|
/// Download the original, hashing along the way so legacy photos (uploaded
|
||||||
|
/// before hashes existed) can be backfilled.
|
||||||
|
async fn download(state: &AppState, key: &str, path: &Path) -> anyhow::Result<(String, i64)> {
|
||||||
|
use sha2::Digest;
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
let object = state
|
let object = state
|
||||||
.s3
|
.s3
|
||||||
.get_object()
|
.get_object()
|
||||||
@@ -114,8 +135,20 @@ async fn download(state: &AppState, key: &str, path: &Path) -> anyhow::Result<()
|
|||||||
.with_context(|| format!("fetching s3://{}/{key}", state.config.s3_bucket))?;
|
.with_context(|| format!("fetching s3://{}/{key}", state.config.s3_bucket))?;
|
||||||
let mut reader = object.body.into_async_read();
|
let mut reader = object.body.into_async_read();
|
||||||
let mut file = tokio::fs::File::create(path).await?;
|
let mut file = tokio::fs::File::create(path).await?;
|
||||||
tokio::io::copy(&mut reader, &mut file).await?;
|
let mut sha = sha2::Sha256::new();
|
||||||
Ok(())
|
let mut crc = crc32fast::Hasher::new();
|
||||||
|
let mut buf = vec![0u8; 128 * 1024];
|
||||||
|
loop {
|
||||||
|
let n = reader.read(&mut buf).await?;
|
||||||
|
if n == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sha.update(&buf[..n]);
|
||||||
|
crc.update(&buf[..n]);
|
||||||
|
file.write_all(&buf[..n]).await?;
|
||||||
|
}
|
||||||
|
file.flush().await?;
|
||||||
|
Ok((hex::encode(sha.finalize()), i64::from(crc.finalize())))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|||||||
+24
-1
@@ -1,5 +1,5 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// Stored as text in Postgres; decoded via TryFrom so an unknown value is a
|
/// Stored as text in Postgres; decoded via TryFrom so an unknown value is a
|
||||||
@@ -47,6 +47,25 @@ impl TryFrom<String> for PhotoStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Client accept/reject vote. Same convention as PhotoStatus: text in
|
||||||
|
/// Postgres (check-constrained), this enum everywhere Rust touches the value
|
||||||
|
/// — serde rejects anything but "accept"/"reject" at the API boundary.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum Verdict {
|
||||||
|
Accept,
|
||||||
|
Reject,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Verdict {
|
||||||
|
pub fn as_str(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Accept => "accept",
|
||||||
|
Self::Reject => "reject",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum JobKind {
|
pub enum JobKind {
|
||||||
ProcessPhoto,
|
ProcessPhoto,
|
||||||
@@ -115,6 +134,10 @@ pub struct Photo {
|
|||||||
pub height: Option<i32>,
|
pub height: Option<i32>,
|
||||||
pub taken_at: Option<DateTime<Utc>>,
|
pub taken_at: Option<DateTime<Utc>>,
|
||||||
pub processed_at: Option<DateTime<Utc>>,
|
pub processed_at: Option<DateTime<Utc>>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub sha256: Option<String>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub crc32: Option<i64>,
|
||||||
pub created_at: DateTime<Utc>,
|
pub created_at: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+70
-34
@@ -79,12 +79,40 @@ pub struct ShareTag {
|
|||||||
pub tag: String,
|
pub tag: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct ShareVerdict {
|
||||||
|
pub share_label: String,
|
||||||
|
pub verdict: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Default)]
|
#[derive(Serialize, Default)]
|
||||||
pub struct PhotoFeedback {
|
pub struct PhotoFeedback {
|
||||||
pub ratings: Vec<ShareRating>,
|
pub ratings: Vec<ShareRating>,
|
||||||
|
pub verdicts: Vec<ShareVerdict>,
|
||||||
pub tags: Vec<ShareTag>,
|
pub tags: Vec<ShareTag>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn feedback_rows<T>(
|
||||||
|
db: &sqlx::PgPool,
|
||||||
|
sql: &str,
|
||||||
|
album_id: Uuid,
|
||||||
|
) -> Result<Vec<(Uuid, String, T)>, sqlx::Error>
|
||||||
|
where
|
||||||
|
(Uuid, String, T): for<'r> sqlx::FromRow<'r, sqlx::postgres::PgRow> + Send + Unpin,
|
||||||
|
{
|
||||||
|
sqlx::query_as(sql).bind(album_id).fetch_all(db).await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fold_feedback<T>(
|
||||||
|
feedback: &mut HashMap<Uuid, PhotoFeedback>,
|
||||||
|
rows: Vec<(Uuid, String, T)>,
|
||||||
|
push: impl Fn(&mut PhotoFeedback, String, T),
|
||||||
|
) {
|
||||||
|
for (photo_id, share_label, value) in rows {
|
||||||
|
push(feedback.entry(photo_id).or_default(), share_label, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct AlbumDetail {
|
pub struct AlbumDetail {
|
||||||
pub album: Album,
|
pub album: Album,
|
||||||
@@ -108,41 +136,49 @@ pub async fn get_one(
|
|||||||
.fetch_all(&state.db)
|
.fetch_all(&state.db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// The three feedback kinds are independent (photo_id, share label, value)
|
||||||
|
// queries — run them concurrently and fold with one shared shape.
|
||||||
|
let (ratings, verdicts, tags) = tokio::try_join!(
|
||||||
|
feedback_rows::<i32>(
|
||||||
|
&state.db,
|
||||||
|
"select r.photo_id, s.label, r.rating
|
||||||
|
from ratings r join shares s on s.id = r.share_id
|
||||||
|
where s.album_id = $1",
|
||||||
|
album_id,
|
||||||
|
),
|
||||||
|
feedback_rows::<String>(
|
||||||
|
&state.db,
|
||||||
|
"select v.photo_id, s.label, v.verdict
|
||||||
|
from verdicts v join shares s on s.id = v.share_id
|
||||||
|
where s.album_id = $1",
|
||||||
|
album_id,
|
||||||
|
),
|
||||||
|
feedback_rows::<String>(
|
||||||
|
&state.db,
|
||||||
|
"select t.photo_id, s.label, t.tag
|
||||||
|
from tags t join shares s on s.id = t.share_id
|
||||||
|
where s.album_id = $1
|
||||||
|
order by t.created_at",
|
||||||
|
album_id,
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
|
||||||
let mut feedback: HashMap<Uuid, PhotoFeedback> = HashMap::new();
|
let mut feedback: HashMap<Uuid, PhotoFeedback> = HashMap::new();
|
||||||
let ratings: Vec<(Uuid, String, i32)> = sqlx::query_as(
|
fold_feedback(&mut feedback, ratings, |f, share_label, rating| {
|
||||||
"select r.photo_id, s.label, r.rating
|
f.ratings.push(ShareRating {
|
||||||
from ratings r join shares s on s.id = r.share_id
|
share_label,
|
||||||
where s.album_id = $1",
|
rating,
|
||||||
)
|
})
|
||||||
.bind(album_id)
|
});
|
||||||
.fetch_all(&state.db)
|
fold_feedback(&mut feedback, verdicts, |f, share_label, verdict| {
|
||||||
.await?;
|
f.verdicts.push(ShareVerdict {
|
||||||
for (photo_id, share_label, rating) in ratings {
|
share_label,
|
||||||
feedback
|
verdict,
|
||||||
.entry(photo_id)
|
})
|
||||||
.or_default()
|
});
|
||||||
.ratings
|
fold_feedback(&mut feedback, tags, |f, share_label, tag| {
|
||||||
.push(ShareRating {
|
f.tags.push(ShareTag { share_label, tag })
|
||||||
share_label,
|
});
|
||||||
rating,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let tags: Vec<(Uuid, String, String)> = sqlx::query_as(
|
|
||||||
"select t.photo_id, s.label, t.tag
|
|
||||||
from tags t join shares s on s.id = t.share_id
|
|
||||||
where s.album_id = $1
|
|
||||||
order by t.created_at",
|
|
||||||
)
|
|
||||||
.bind(album_id)
|
|
||||||
.fetch_all(&state.db)
|
|
||||||
.await?;
|
|
||||||
for (photo_id, share_label, tag) in tags {
|
|
||||||
feedback
|
|
||||||
.entry(photo_id)
|
|
||||||
.or_default()
|
|
||||||
.tags
|
|
||||||
.push(ShareTag { share_label, tag });
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Json(AlbumDetail {
|
Ok(Json(AlbumDetail {
|
||||||
album,
|
album,
|
||||||
|
|||||||
+40
-2
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::error::{ApiError, ApiResult};
|
use crate::error::{ApiError, ApiResult};
|
||||||
use crate::models::{PhotoStatus, Share};
|
use crate::models::{PhotoStatus, Share, Verdict};
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
|
||||||
pub async fn load_share(state: &AppState, token: &str) -> Result<Share, ApiError> {
|
pub async fn load_share(state: &AppState, token: &str) -> Result<Share, ApiError> {
|
||||||
@@ -137,6 +137,7 @@ struct ClientPhotoRow {
|
|||||||
taken_at: Option<DateTime<Utc>>,
|
taken_at: Option<DateTime<Utc>>,
|
||||||
processed_at: Option<DateTime<Utc>>,
|
processed_at: Option<DateTime<Utc>>,
|
||||||
my_rating: Option<i32>,
|
my_rating: Option<i32>,
|
||||||
|
my_verdict: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -185,9 +186,10 @@ pub async fn get_share(
|
|||||||
let jar = grant_access(&state, jar, share.id);
|
let jar = grant_access(&state, jar, share.id);
|
||||||
|
|
||||||
let rows: Vec<ClientPhotoRow> = sqlx::query_as(
|
let rows: Vec<ClientPhotoRow> = sqlx::query_as(
|
||||||
"select p.id, p.filename, p.size_bytes, p.width, p.height, p.taken_at, p.processed_at, r.rating as my_rating
|
"select p.id, p.filename, p.size_bytes, p.width, p.height, p.taken_at, p.processed_at, r.rating as my_rating, v.verdict as my_verdict
|
||||||
from photos p
|
from photos p
|
||||||
left join ratings r on r.photo_id = p.id and r.share_id = $2
|
left join ratings r on r.photo_id = p.id and r.share_id = $2
|
||||||
|
left join verdicts v on v.photo_id = p.id and v.share_id = $2
|
||||||
where p.album_id = $1 and p.status = $3
|
where p.album_id = $1 and p.status = $3
|
||||||
order by coalesce(p.taken_at, p.created_at), p.filename",
|
order by coalesce(p.taken_at, p.created_at), p.filename",
|
||||||
)
|
)
|
||||||
@@ -352,6 +354,42 @@ pub async fn set_rating(
|
|||||||
Ok(Json(serde_json::json!({ "ok": true })))
|
Ok(Json(serde_json::json!({ "ok": true })))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct VerdictBody {
|
||||||
|
verdict: Option<Verdict>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_verdict(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path((token, photo_id)): Path<(String, Uuid)>,
|
||||||
|
jar: SignedCookieJar,
|
||||||
|
Json(body): Json<VerdictBody>,
|
||||||
|
) -> ApiResult<Json<serde_json::Value>> {
|
||||||
|
let share = share_photo(&state, &jar, &token, photo_id).await?;
|
||||||
|
match body.verdict {
|
||||||
|
None => {
|
||||||
|
sqlx::query("delete from verdicts where share_id = $1 and photo_id = $2")
|
||||||
|
.bind(share.id)
|
||||||
|
.bind(photo_id)
|
||||||
|
.execute(&state.db)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
Some(verdict) => {
|
||||||
|
sqlx::query(
|
||||||
|
"insert into verdicts (share_id, photo_id, verdict) values ($1, $2, $3)
|
||||||
|
on conflict (share_id, photo_id)
|
||||||
|
do update set verdict = excluded.verdict, updated_at = now()",
|
||||||
|
)
|
||||||
|
.bind(share.id)
|
||||||
|
.bind(photo_id)
|
||||||
|
.bind(verdict.as_str())
|
||||||
|
.execute(&state.db)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Json(serde_json::json!({ "ok": true })))
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct TagsBody {
|
pub struct TagsBody {
|
||||||
tags: Vec<String>,
|
tags: Vec<String>,
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ pub fn router(state: &AppState) -> Router<AppState> {
|
|||||||
get(shares::list).post(shares::create),
|
get(shares::list).post(shares::create),
|
||||||
)
|
)
|
||||||
.route("/api/albums/{id}/zip", post(zip::album_zip))
|
.route("/api/albums/{id}/zip", post(zip::album_zip))
|
||||||
|
.route("/api/albums/{id}/photos/by-hash/{sha256}", get(photos::by_hash))
|
||||||
.route("/api/photos/{id}", delete(photos::delete))
|
.route("/api/photos/{id}", delete(photos::delete))
|
||||||
.route("/api/photos/{id}/reprocess", post(photos::reprocess))
|
.route("/api/photos/{id}/reprocess", post(photos::reprocess))
|
||||||
.route("/api/shares/{id}", delete(shares::delete))
|
.route("/api/shares/{id}", delete(shares::delete))
|
||||||
@@ -99,6 +100,10 @@ pub fn router(state: &AppState) -> Router<AppState> {
|
|||||||
"/api/share/{token}/photos/{photo_id}/rating",
|
"/api/share/{token}/photos/{photo_id}/rating",
|
||||||
put(client::set_rating),
|
put(client::set_rating),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/api/share/{token}/photos/{photo_id}/verdict",
|
||||||
|
put(client::set_verdict),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/api/share/{token}/photos/{photo_id}/tags",
|
"/api/share/{token}/photos/{photo_id}/tags",
|
||||||
put(client::set_tags),
|
put(client::set_tags),
|
||||||
|
|||||||
+82
-26
@@ -5,6 +5,7 @@ use axum::http::HeaderMap;
|
|||||||
use axum::Json;
|
use axum::Json;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use sha2::Digest;
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -60,17 +61,23 @@ pub async fn upload(
|
|||||||
.unwrap_or("application/octet-stream")
|
.unwrap_or("application/octet-stream")
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
// Stream the request body to a temp file so large raws never sit in memory.
|
// Stream the request body to a temp file so large raws never sit in
|
||||||
|
// memory, hashing as it flows: sha256 for duplicate detection, crc32 for
|
||||||
|
// spool-free zip downloads.
|
||||||
let dir = tempfile::tempdir().map_err(anyhow::Error::from)?;
|
let dir = tempfile::tempdir().map_err(anyhow::Error::from)?;
|
||||||
let path = dir.path().join("upload.bin");
|
let path = dir.path().join("upload.bin");
|
||||||
let mut file = tokio::fs::File::create(&path)
|
let mut file = tokio::fs::File::create(&path)
|
||||||
.await
|
.await
|
||||||
.map_err(anyhow::Error::from)?;
|
.map_err(anyhow::Error::from)?;
|
||||||
let mut stream = body.into_data_stream();
|
let mut stream = body.into_data_stream();
|
||||||
|
let mut sha = sha2::Sha256::new();
|
||||||
|
let mut crc = crc32fast::Hasher::new();
|
||||||
let mut size: i64 = 0;
|
let mut size: i64 = 0;
|
||||||
while let Some(chunk) = stream.next().await {
|
while let Some(chunk) = stream.next().await {
|
||||||
let chunk = chunk.map_err(|e| ApiError::bad_request(format!("upload aborted: {e}")))?;
|
let chunk = chunk.map_err(|e| ApiError::bad_request(format!("upload aborted: {e}")))?;
|
||||||
size += chunk.len() as i64;
|
size += chunk.len() as i64;
|
||||||
|
sha.update(&chunk);
|
||||||
|
crc.update(&chunk);
|
||||||
file.write_all(&chunk).await.map_err(anyhow::Error::from)?;
|
file.write_all(&chunk).await.map_err(anyhow::Error::from)?;
|
||||||
}
|
}
|
||||||
file.flush().await.map_err(anyhow::Error::from)?;
|
file.flush().await.map_err(anyhow::Error::from)?;
|
||||||
@@ -78,6 +85,20 @@ pub async fn upload(
|
|||||||
if size == 0 {
|
if size == 0 {
|
||||||
return Err(ApiError::bad_request("empty upload"));
|
return Err(ApiError::bad_request("empty upload"));
|
||||||
}
|
}
|
||||||
|
let sha256 = hex::encode(sha.finalize());
|
||||||
|
let crc32 = i64::from(crc.finalize());
|
||||||
|
|
||||||
|
// Same content already in this album? Return it — re-dragging a folder
|
||||||
|
// after a partial upload just fills the gaps instead of duplicating.
|
||||||
|
let existing: Option<Photo> =
|
||||||
|
sqlx::query_as("select * from photos where album_id = $1 and sha256 = $2")
|
||||||
|
.bind(album_id)
|
||||||
|
.bind(&sha256)
|
||||||
|
.fetch_optional(&state.db)
|
||||||
|
.await?;
|
||||||
|
if let Some(existing) = existing {
|
||||||
|
return Ok(Json(existing));
|
||||||
|
}
|
||||||
|
|
||||||
// Upload to S3 first, then create the row and enqueue processing in one
|
// Upload to S3 first, then create the row and enqueue processing in one
|
||||||
// transaction — a photo row can never exist without its job, and a failed
|
// transaction — a photo row can never exist without its job, and a failed
|
||||||
@@ -86,30 +107,33 @@ pub async fn upload(
|
|||||||
let key = s3::original_key(photo_id, &filename);
|
let key = s3::original_key(photo_id, &filename);
|
||||||
s3::put_file(&state, &key, &path, &content_type).await?;
|
s3::put_file(&state, &key, &path, &content_type).await?;
|
||||||
|
|
||||||
let result: ApiResult<(sqlx::Transaction<'static, sqlx::Postgres>, Photo)> = async {
|
let result: Result<(sqlx::Transaction<'static, sqlx::Postgres>, Photo), sqlx::Error> =
|
||||||
let mut tx = state.db.begin().await?;
|
async {
|
||||||
let photo: Photo = sqlx::query_as(
|
let mut tx = state.db.begin().await?;
|
||||||
"insert into photos (id, album_id, filename, content_type, size_bytes, status)
|
let photo: Photo = sqlx::query_as(
|
||||||
values ($1, $2, $3, $4, $5, $6)
|
"insert into photos (id, album_id, filename, content_type, size_bytes, status, sha256, crc32)
|
||||||
returning *",
|
values ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
)
|
returning *",
|
||||||
.bind(photo_id)
|
)
|
||||||
.bind(album_id)
|
.bind(photo_id)
|
||||||
.bind(&filename)
|
.bind(album_id)
|
||||||
.bind(&content_type)
|
.bind(&filename)
|
||||||
.bind(size)
|
.bind(&content_type)
|
||||||
.bind(PhotoStatus::Uploaded.as_str())
|
.bind(size)
|
||||||
.fetch_one(&mut *tx)
|
.bind(PhotoStatus::Uploaded.as_str())
|
||||||
.await?;
|
.bind(&sha256)
|
||||||
jobs::enqueue(
|
.bind(crc32)
|
||||||
&mut *tx,
|
.fetch_one(&mut *tx)
|
||||||
JobKind::ProcessPhoto,
|
.await?;
|
||||||
serde_json::json!({ "photo_id": photo_id }),
|
jobs::enqueue(
|
||||||
)
|
&mut *tx,
|
||||||
.await?;
|
JobKind::ProcessPhoto,
|
||||||
Ok((tx, photo))
|
serde_json::json!({ "photo_id": photo_id }),
|
||||||
}
|
)
|
||||||
.await;
|
.await?;
|
||||||
|
Ok((tx, photo))
|
||||||
|
}
|
||||||
|
.await;
|
||||||
|
|
||||||
let (tx, photo) = match result {
|
let (tx, photo) = match result {
|
||||||
Ok(pair) => pair,
|
Ok(pair) => pair,
|
||||||
@@ -118,7 +142,24 @@ pub async fn upload(
|
|||||||
if let Err(cleanup) = s3::delete_prefix(&state, &s3::photo_prefix(photo_id)).await {
|
if let Err(cleanup) = s3::delete_prefix(&state, &s3::photo_prefix(photo_id)).await {
|
||||||
tracing::error!("failed to clean up s3 after aborted upload: {cleanup:#}");
|
tracing::error!("failed to clean up s3 after aborted upload: {cleanup:#}");
|
||||||
}
|
}
|
||||||
return Err(e);
|
// Concurrent identical upload beat us to the unique index — hand
|
||||||
|
// back the winner instead of an error.
|
||||||
|
let unique_violation = matches!(
|
||||||
|
&e,
|
||||||
|
sqlx::Error::Database(db) if db.code().as_deref() == Some("23505")
|
||||||
|
);
|
||||||
|
if unique_violation {
|
||||||
|
let winner: Option<Photo> =
|
||||||
|
sqlx::query_as("select * from photos where album_id = $1 and sha256 = $2")
|
||||||
|
.bind(album_id)
|
||||||
|
.bind(&sha256)
|
||||||
|
.fetch_optional(&state.db)
|
||||||
|
.await?;
|
||||||
|
if let Some(winner) = winner {
|
||||||
|
return Ok(Json(winner));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err(e.into());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Err(e) = tx.commit().await {
|
if let Err(e) = tx.commit().await {
|
||||||
@@ -133,6 +174,21 @@ pub async fn upload(
|
|||||||
Ok(Json(photo))
|
Ok(Json(photo))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Client-side dedup support: lets the uploader skip transferring files whose
|
||||||
|
/// content already exists in the album.
|
||||||
|
pub async fn by_hash(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path((album_id, sha256)): Path<(Uuid, String)>,
|
||||||
|
) -> ApiResult<Json<Photo>> {
|
||||||
|
let photo: Option<Photo> =
|
||||||
|
sqlx::query_as("select * from photos where album_id = $1 and sha256 = $2")
|
||||||
|
.bind(album_id)
|
||||||
|
.bind(&sha256)
|
||||||
|
.fetch_optional(&state.db)
|
||||||
|
.await?;
|
||||||
|
photo.map(Json).ok_or_else(ApiError::not_found)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn delete(
|
pub async fn delete(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(photo_id): Path<Uuid>,
|
Path(photo_id): Path<Uuid>,
|
||||||
|
|||||||
+20
-9
@@ -23,6 +23,8 @@ struct ShareAdminRow {
|
|||||||
created_at: DateTime<Utc>,
|
created_at: DateTime<Utc>,
|
||||||
rating_count: i64,
|
rating_count: i64,
|
||||||
tag_count: i64,
|
tag_count: i64,
|
||||||
|
accept_count: i64,
|
||||||
|
reject_count: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn share_json(state: &AppState, row: &ShareAdminRow) -> serde_json::Value {
|
fn share_json(state: &AppState, row: &ShareAdminRow) -> serde_json::Value {
|
||||||
@@ -38,20 +40,30 @@ fn share_json(state: &AppState, row: &ShareAdminRow) -> serde_json::Value {
|
|||||||
"created_at": row.created_at,
|
"created_at": row.created_at,
|
||||||
"rating_count": row.rating_count,
|
"rating_count": row.rating_count,
|
||||||
"tag_count": row.tag_count,
|
"tag_count": row.tag_count,
|
||||||
|
"accept_count": row.accept_count,
|
||||||
|
"reject_count": row.reject_count,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const SHARE_COLUMNS: &str = "s.id, s.token, s.label, s.password_hash, s.allow_download,
|
// Aggregates run as laterals so each feedback table is scanned once per
|
||||||
|
// share (the verdict lateral yields both counts from a single pass).
|
||||||
|
const SHARE_SELECT: &str = "select s.id, s.token, s.label, s.password_hash, s.allow_download,
|
||||||
s.expires_at, s.locked_until, s.created_at,
|
s.expires_at, s.locked_until, s.created_at,
|
||||||
(select count(*) from ratings r where r.share_id = s.id) as rating_count,
|
rc.rating_count, tc.tag_count, vc.accept_count, vc.reject_count
|
||||||
(select count(*) from tags t where t.share_id = s.id) as tag_count";
|
from shares s
|
||||||
|
cross join lateral (select count(*) as rating_count from ratings r where r.share_id = s.id) rc
|
||||||
|
cross join lateral (select count(*) as tag_count from tags t where t.share_id = s.id) tc
|
||||||
|
cross join lateral (
|
||||||
|
select count(*) filter (where v.verdict = 'accept') as accept_count,
|
||||||
|
count(*) filter (where v.verdict = 'reject') as reject_count
|
||||||
|
from verdicts v where v.share_id = s.id) vc";
|
||||||
|
|
||||||
pub async fn list(
|
pub async fn list(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(album_id): Path<Uuid>,
|
Path(album_id): Path<Uuid>,
|
||||||
) -> ApiResult<Json<Vec<serde_json::Value>>> {
|
) -> ApiResult<Json<Vec<serde_json::Value>>> {
|
||||||
let rows: Vec<ShareAdminRow> = sqlx::query_as(&format!(
|
let rows: Vec<ShareAdminRow> = sqlx::query_as(&format!(
|
||||||
"select {SHARE_COLUMNS} from shares s where s.album_id = $1 order by s.created_at desc"
|
"{SHARE_SELECT} where s.album_id = $1 order by s.created_at desc"
|
||||||
))
|
))
|
||||||
.bind(album_id)
|
.bind(album_id)
|
||||||
.fetch_all(&state.db)
|
.fetch_all(&state.db)
|
||||||
@@ -119,11 +131,10 @@ pub async fn create(
|
|||||||
.fetch_one(&state.db)
|
.fetch_one(&state.db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let row: ShareAdminRow =
|
let row: ShareAdminRow = sqlx::query_as(&format!("{SHARE_SELECT} where s.id = $1"))
|
||||||
sqlx::query_as(&format!("select {SHARE_COLUMNS} from shares s where s.id = $1"))
|
.bind(share_id)
|
||||||
.bind(share_id)
|
.fetch_one(&state.db)
|
||||||
.fetch_one(&state.db)
|
.await?;
|
||||||
.await?;
|
|
||||||
Ok(Json(share_json(&state, &row)))
|
Ok(Json(share_json(&state, &row)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+62
-11
@@ -136,6 +136,9 @@ struct Entry {
|
|||||||
offset: u64,
|
offset: u64,
|
||||||
dos_time: u16,
|
dos_time: u16,
|
||||||
dos_date: u16,
|
dos_date: u16,
|
||||||
|
/// Stored at upload/processing time; photos from before hashes existed
|
||||||
|
/// have None and take the slower spool path (which backfills it).
|
||||||
|
crc: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ZipPlan {
|
struct ZipPlan {
|
||||||
@@ -176,6 +179,7 @@ fn plan_zip(photos: &[Photo]) -> Result<ZipPlan, ApiError> {
|
|||||||
offset: entry_offset,
|
offset: entry_offset,
|
||||||
dos_time,
|
dos_time,
|
||||||
dos_date,
|
dos_date,
|
||||||
|
crc: photo.crc32.map(|v| v as u32),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let cd_offset = offset;
|
let cd_offset = offset;
|
||||||
@@ -277,9 +281,23 @@ fn stream_zip(state: AppState, photos: Vec<Photo>, album_name: &str) -> ApiResul
|
|||||||
.map_err(|e| anyhow::anyhow!("building response: {e}").into())
|
.map_err(|e| anyhow::anyhow!("building response: {e}").into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Download an original into an anonymous temp file, computing its CRC-32 and
|
enum Fetched {
|
||||||
/// verifying the byte count matches what the zip plan promised.
|
/// CRC already known — the body streams straight into the response.
|
||||||
fn spool(state: &AppState, key: String, expected_size: u64) -> JoinHandle<anyhow::Result<(tokio::fs::File, u32)>> {
|
Direct(Box<aws_sdk_s3::operation::get_object::GetObjectOutput>),
|
||||||
|
/// Pre-hash photo: spooled to a temp file to compute the CRC first.
|
||||||
|
Spooled(tokio::fs::File, u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start fetching an original. With a known CRC this only opens the S3
|
||||||
|
/// response (the body is consumed later, straight into the zip stream);
|
||||||
|
/// otherwise the object is spooled to an anonymous temp file to compute the
|
||||||
|
/// CRC, verifying the byte count the zip plan promised.
|
||||||
|
fn fetch_entry(
|
||||||
|
state: &AppState,
|
||||||
|
key: String,
|
||||||
|
expected_size: u64,
|
||||||
|
crc_known: bool,
|
||||||
|
) -> JoinHandle<anyhow::Result<Fetched>> {
|
||||||
let state = state.clone();
|
let state = state.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let object = state
|
let object = state
|
||||||
@@ -290,6 +308,9 @@ fn spool(state: &AppState, key: String, expected_size: u64) -> JoinHandle<anyhow
|
|||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| anyhow::anyhow!("fetching {key}: {e}"))?;
|
.map_err(|e| anyhow::anyhow!("fetching {key}: {e}"))?;
|
||||||
|
if crc_known {
|
||||||
|
return Ok(Fetched::Direct(Box::new(object)));
|
||||||
|
}
|
||||||
let mut file = tokio::fs::File::from_std(tempfile::tempfile()?);
|
let mut file = tokio::fs::File::from_std(tempfile::tempfile()?);
|
||||||
let mut reader = object.body.into_async_read();
|
let mut reader = object.body.into_async_read();
|
||||||
let mut hasher = crc32fast::Hasher::new();
|
let mut hasher = crc32fast::Hasher::new();
|
||||||
@@ -310,7 +331,7 @@ fn spool(state: &AppState, key: String, expected_size: u64) -> JoinHandle<anyhow
|
|||||||
);
|
);
|
||||||
file.flush().await?;
|
file.flush().await?;
|
||||||
file.seek(std::io::SeekFrom::Start(0)).await?;
|
file.seek(std::io::SeekFrom::Start(0)).await?;
|
||||||
Ok((file, hasher.finalize()))
|
Ok(Fetched::Spooled(file, hasher.finalize()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,20 +344,29 @@ async fn write_zip(
|
|||||||
const FLAGS: u16 = 0x0800;
|
const FLAGS: u16 = 0x0800;
|
||||||
let mut crcs = Vec::with_capacity(plan.entries.len());
|
let mut crcs = Vec::with_capacity(plan.entries.len());
|
||||||
|
|
||||||
// Prefetch: spool the next object from S3 while streaming the current one.
|
// Prefetch: start fetching the next object while streaming the current one.
|
||||||
let mut pending: Option<JoinHandle<anyhow::Result<(tokio::fs::File, u32)>>> = None;
|
let mut pending: Option<JoinHandle<anyhow::Result<Fetched>>> = None;
|
||||||
for (i, entry) in plan.entries.iter().enumerate() {
|
for (i, entry) in plan.entries.iter().enumerate() {
|
||||||
let current = match pending.take() {
|
let current = match pending.take() {
|
||||||
Some(handle) => handle,
|
Some(handle) => handle,
|
||||||
None => spool(state, entry.s3_key.clone(), entry.size),
|
None => fetch_entry(state, entry.s3_key.clone(), entry.size, entry.crc.is_some()),
|
||||||
};
|
};
|
||||||
if let Some(next) = plan.entries.get(i + 1) {
|
if let Some(next) = plan.entries.get(i + 1) {
|
||||||
pending = Some(spool(state, next.s3_key.clone(), next.size));
|
pending = Some(fetch_entry(
|
||||||
|
state,
|
||||||
|
next.s3_key.clone(),
|
||||||
|
next.size,
|
||||||
|
next.crc.is_some(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
let (mut file, crc) = current
|
let fetched = current
|
||||||
.await
|
.await
|
||||||
.map_err(|e| anyhow::anyhow!("spool task failed: {e}"))?
|
.map_err(|e| anyhow::anyhow!("fetch task failed: {e}"))?
|
||||||
.map_err(|e| anyhow::anyhow!("photo {}: {e:#}", entry.photo_id))?;
|
.map_err(|e| anyhow::anyhow!("photo {}: {e:#}", entry.photo_id))?;
|
||||||
|
let crc = match &fetched {
|
||||||
|
Fetched::Direct(_) => entry.crc.expect("direct fetch implies known crc"),
|
||||||
|
Fetched::Spooled(_, crc) => *crc,
|
||||||
|
};
|
||||||
crcs.push(crc);
|
crcs.push(crc);
|
||||||
|
|
||||||
let mut lfh = Vec::with_capacity(30 + entry.name.len());
|
let mut lfh = Vec::with_capacity(30 + entry.name.len());
|
||||||
@@ -353,7 +383,28 @@ async fn write_zip(
|
|||||||
lfh.extend_from_slice(&0u16.to_le_bytes()); // extra len
|
lfh.extend_from_slice(&0u16.to_le_bytes()); // extra len
|
||||||
lfh.extend_from_slice(&entry.name);
|
lfh.extend_from_slice(&entry.name);
|
||||||
out.write_all(&lfh).await?;
|
out.write_all(&lfh).await?;
|
||||||
tokio::io::copy(&mut file, &mut out).await?;
|
match fetched {
|
||||||
|
Fetched::Direct(object) => {
|
||||||
|
let mut reader = object.body.into_async_read();
|
||||||
|
let copied = tokio::io::copy(&mut reader, &mut out).await?;
|
||||||
|
anyhow::ensure!(
|
||||||
|
copied == entry.size,
|
||||||
|
"{} is {copied} bytes in s3 but {} in the database",
|
||||||
|
entry.s3_key,
|
||||||
|
entry.size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Fetched::Spooled(mut file, crc) => {
|
||||||
|
tokio::io::copy(&mut file, &mut out).await?;
|
||||||
|
// Self-heal: store the freshly computed crc so the next
|
||||||
|
// download of this photo streams directly.
|
||||||
|
let _ = sqlx::query("update photos set crc32 = coalesce(crc32, $2) where id = $1")
|
||||||
|
.bind(entry.photo_id)
|
||||||
|
.bind(i64::from(crc))
|
||||||
|
.execute(&state.db)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Central directory.
|
// Central directory.
|
||||||
|
|||||||
Reference in New Issue
Block a user