Compare commits
8
Commits
2143b95657
...
trunk-ng
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
431ac2e610 | ||
|
|
504b96ed31 | ||
|
|
bb8c7109fa | ||
|
|
f1e946228a | ||
|
|
3614cfe1f8 | ||
|
|
43075f00d4 | ||
|
|
3f719359de | ||
|
|
f1abd45f24 |
No files matched your search
@@ -69,7 +69,8 @@ and deleting posts; it is **not linked from the public site** (reach it by URL).
|
||||
Its API, all behind the same Basic auth and handled in `manage.go`:
|
||||
|
||||
- `GET /api/photos` — every post as JSON (`slug, caption, date, tags, thumb, url`), newest first.
|
||||
- `GET /api/thumb/{slug}` — serves that post's thumbnail JPEG from `public/thumbs/`, behind auth. Non-browser clients (the Android app) use this instead of the static `/thumbs/<slug>.jpg` path, because a front-door botwall on `location /` can block a non-browser client from the static site while `/api/` is proxied straight to the app.
|
||||
- `GET /api/thumb/{slug}` — serves that post's thumbnail JPEG from `public/thumbs/`. **Unguarded**: thumbnails are the same public data as the static `/thumbs/`, so nothing is protected, and keeping it off the auth path means it can never 401 and so can never trip the fail2ban auth jail over a per-row thumbnail load. Non-browser clients (the Android app) use it instead of the static `/thumbs/<slug>.jpg` path, because a front-door botwall on `location /` can block a non-browser client from the static site while `/api/` is proxied straight to the app.
|
||||
- `GET /api/tags.json` — serves the generated `public/tags.json` for the Android app's tag autocomplete. **Unguarded** (like `/api/session`): the tag list is the same public data as the static `/tags.json`, so nothing is protected, and keeping it off the auth path means it can never return 401 — otherwise an nginx auth-fail jail (fail2ban) would ban the app over a mere autocomplete poll. It lives under `/api/` only so nginx proxies it past a botwall; browsers read the static `/tags.json`.
|
||||
- `POST /api/edit` — rewrite one post's sidecar (`slug, caption, date, tags`). The slug (and URL) never changes; editing only touches the `.toml`.
|
||||
- `POST /api/tags` — bulk add/remove tags across posts (`slugs` CSV + `add`/`remove` CSV), leaving each post's caption and date intact.
|
||||
- `POST /api/delete` — move posts (`slug` repeated, or `slugs` CSV) to `content/.trash/` (timestamp-suffixed, reversible), **not** a hard delete.
|
||||
@@ -192,7 +193,7 @@ go run . --serve
|
||||
|
||||
## Android app
|
||||
|
||||
Kotlin + Jetpack Compose uploader: pick or share one or many photos, add caption/date/tags, post via a durable WorkManager queue. It also has a **Manage screen** (from the Post screen's "Manage" button) that lists published posts with thumbnails and edits caption/date/tags or deletes (to trash) via the same `/api/photos`, `/api/edit`, `/api/delete` endpoints the web `/manage` uses. The manage calls go through `UploadClient` (OkHttp + `org.json`, no extra deps); sending no `Origin`/`Sec-Fetch-Site` header, the app passes the server's same-origin CSRF check as a non-browser client. Thumbnails are fetched from `/api/thumb/<slug>` (authed, app-served) rather than the static `/thumbs/` path, so a botwall on `location /` doesn't blank them. The multi-photo picker is constructed with an explicit `maxItems` so it reliably opens in multi-select mode. Credentials (HTTP Basic) are sealed with an Android Keystore AES-GCM key. See `android/README.md` for the detail.
|
||||
Kotlin + Jetpack Compose uploader: pick or share one or many photos, add caption/date/tags, post via a durable WorkManager queue. It also has a **Manage screen** (from the Post screen's "Manage" button) that lists published posts with thumbnails and edits caption/date/tags or deletes (to trash) via the same `/api/photos`, `/api/edit`, `/api/delete` endpoints the web `/manage` uses. The manage calls go through `UploadClient` (OkHttp + `org.json`, no extra deps); sending no `Origin`/`Sec-Fetch-Site` header, the app passes the server's same-origin CSRF check as a non-browser client. Thumbnails are fetched from `/api/thumb/<slug>` (unguarded public data, app-served) rather than the static `/thumbs/` path, so a botwall on `location /` doesn't blank them. Both tag fields (post and edit) autocomplete from the known tag list, fetched from `/api/tags.json` (unguarded public data, proxied past the botwall); suggestions filter on the token after the last comma and tapping one completes it. The multi-photo picker is constructed with an explicit `maxItems` so it reliably opens in multi-select mode. Credentials (HTTP Basic) are sealed with an Android Keystore AES-GCM key. See `android/README.md` for the detail.
|
||||
|
||||
Its own flake, deliberately separate from the root one so NixOS consumers of `nixosModules.default` don't pull the Android SDK into their lock:
|
||||
|
||||
@@ -265,7 +266,7 @@ nix build # succeeds
|
||||
## What's not done yet
|
||||
|
||||
- ActivityPub federation (planned later — start with RSS/Atom)
|
||||
- Edit/delete in the **Qt desktop** app (web `/manage` and the Android app have it; the `/api/` endpoints are there for Qt to adopt). Restoring from trash is still web-only on all clients.
|
||||
- Restoring from trash is still web-only: the `/manage` page has it, but neither the Android nor the Qt desktop app exposes `/api/restore` or `/api/trash` yet. (Edit/delete and tag autocomplete now exist in all three clients.)
|
||||
- Any auth beyond the app's htpasswd Basic auth (no sessions, no per-user scopes)
|
||||
- The Android app has never been run on real hardware
|
||||
- Photo-page swipe has never been tested on a real touchscreen; the iOS Safari edge-gesture interaction is the untested part
|
||||
@@ -4,6 +4,9 @@ Native uploader and manager for the photogallery server. Pick or share one or
|
||||
many photos, add caption/date/tags, and post. A **Manage** screen lists the
|
||||
published posts and edits their caption/date/tags or deletes them (to the
|
||||
server's trash) over the same `/api/*` endpoints the web `/manage` uses.
|
||||
Both tag fields autocomplete from the existing tags (fetched from
|
||||
`/api/tags.json`, unguarded public data proxied past the botwall), filtering on
|
||||
the token after the last comma.
|
||||
Credentials are entered once and sealed with a key held in the Android Keystore.
|
||||
|
||||
The server is not modified by this app. Everything it needs, it does client
|
||||
|
||||
@@ -78,6 +78,7 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
showManage -> ManageScreen(
|
||||
state = viewModel.manage,
|
||||
tagSuggestions = viewModel.tagSuggestions,
|
||||
onBack = { showManage = false },
|
||||
onRefresh = viewModel::loadManage,
|
||||
onSave = viewModel::saveEdit,
|
||||
@@ -87,6 +88,7 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
else -> PostScreen(
|
||||
state = viewModel.post,
|
||||
tagSuggestions = viewModel.tagSuggestions,
|
||||
onPickImages = {
|
||||
picker.launch(
|
||||
PickVisualMediaRequest(
|
||||
|
||||
@@ -234,6 +234,36 @@ class UploadClient(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the known tag names for autocomplete, alphabetical. Reads
|
||||
* /api/tags.json (not the static /tags.json) so a front-door botwall on
|
||||
* `location /` can't blank it for this non-browser client, mirroring how
|
||||
* thumbnails go through /api/thumb/. That endpoint is unguarded on the
|
||||
* server (public tag data), so it never 401s and can't get the app banned
|
||||
* by an auth-fail jail; the Basic header still rides along harmlessly.
|
||||
* Returns an empty list on any failure — autocomplete is a convenience,
|
||||
* never a hard dependency.
|
||||
*/
|
||||
fun fetchTags(): List<String> {
|
||||
val request = Request.Builder().url("$base/api/tags.json").get().build()
|
||||
return try {
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) return emptyList()
|
||||
val arr = JSONArray(response.body?.string().orEmpty())
|
||||
val out = ArrayList<String>(arr.length())
|
||||
for (i in 0 until arr.length()) {
|
||||
val name = arr.getJSONObject(i).optString("tag")
|
||||
if (name.isNotEmpty()) out.add(name)
|
||||
}
|
||||
out
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
emptyList()
|
||||
} catch (e: JSONException) {
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/** Fetches a thumbnail by its site-relative [path], or null on any failure. */
|
||||
fun fetchThumbnail(path: String): Bitmap? {
|
||||
val request = Request.Builder().url("$base$path").get().build()
|
||||
|
||||
@@ -49,6 +49,7 @@ private val DISPLAY_DATE: DateTimeFormatter = DateTimeFormatter.ofPattern("d MMM
|
||||
@Composable
|
||||
fun ManageScreen(
|
||||
state: ManageUiState,
|
||||
tagSuggestions: List<String>,
|
||||
onBack: () -> Unit,
|
||||
onRefresh: () -> Unit,
|
||||
onSave: (slug: String, caption: String, date: LocalDate, tags: String) -> Unit,
|
||||
@@ -120,6 +121,7 @@ fun ManageScreen(
|
||||
editing?.let { post ->
|
||||
EditDialog(
|
||||
post = post,
|
||||
tagSuggestions = tagSuggestions,
|
||||
onDismiss = { editing = null },
|
||||
onSave = { caption, date, tags ->
|
||||
onSave(post.slug, caption, date, tags)
|
||||
@@ -209,6 +211,7 @@ private fun PostRow(post: ManagePost, onEdit: () -> Unit, onDelete: () -> Unit)
|
||||
@Composable
|
||||
private fun EditDialog(
|
||||
post: ManagePost,
|
||||
tagSuggestions: List<String>,
|
||||
onDismiss: () -> Unit,
|
||||
onSave: (caption: String, date: LocalDate, tags: String) -> Unit,
|
||||
) {
|
||||
@@ -243,12 +246,11 @@ private fun EditDialog(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
OutlinedTextField(
|
||||
TagField(
|
||||
value = tags,
|
||||
onValueChange = { tags = it },
|
||||
label = { Text("Tags") },
|
||||
placeholder = { Text("comma-separated") },
|
||||
singleLine = true,
|
||||
suggestions = tagSuggestions,
|
||||
enabled = true,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
@@ -49,6 +49,7 @@ private val DISPLAY_DATE: DateTimeFormatter = DateTimeFormatter.ofPattern("d MMM
|
||||
@Composable
|
||||
fun PostScreen(
|
||||
state: PostUiState,
|
||||
tagSuggestions: List<String>,
|
||||
onPickImages: () -> Unit,
|
||||
onCaptionChange: (id: String, value: String) -> Unit,
|
||||
onTagsChange: (id: String, value: String) -> Unit,
|
||||
@@ -111,6 +112,7 @@ fun PostScreen(
|
||||
items(state.items, key = { it.id }) { item ->
|
||||
ItemCard(
|
||||
item = item,
|
||||
tagSuggestions = tagSuggestions,
|
||||
onCaptionChange = { onCaptionChange(item.id, it) },
|
||||
onTagsChange = { onTagsChange(item.id, it) },
|
||||
onEditDate = { dateEditFor = item },
|
||||
@@ -190,6 +192,7 @@ fun PostScreen(
|
||||
@Composable
|
||||
private fun ItemCard(
|
||||
item: PickedItem,
|
||||
tagSuggestions: List<String>,
|
||||
onCaptionChange: (String) -> Unit,
|
||||
onTagsChange: (String) -> Unit,
|
||||
onEditDate: () -> Unit,
|
||||
@@ -227,12 +230,10 @@ private fun ItemCard(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.height(6.dp))
|
||||
OutlinedTextField(
|
||||
TagField(
|
||||
value = item.tags,
|
||||
onValueChange = onTagsChange,
|
||||
label = { Text("Tags") },
|
||||
placeholder = { Text("comma-separated") },
|
||||
singleLine = true,
|
||||
suggestions = tagSuggestions,
|
||||
enabled = item.status !is ItemStatus.Uploading,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package ws.inflo.photogallery.ui
|
||||
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.SuggestionChip
|
||||
import androidx.compose.material3.SuggestionChipDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
private const val MAX_SUGGESTIONS = 8
|
||||
|
||||
/**
|
||||
* A comma-separated tags input with inline autocomplete. Suggestions filter on
|
||||
* the token currently being typed (the text after the last comma) and exclude
|
||||
* tags already entered; tapping one completes that token and starts the next.
|
||||
* The raw string is passed straight through — the server normalizes it.
|
||||
*/
|
||||
@Composable
|
||||
fun TagField(
|
||||
value: String,
|
||||
onValueChange: (String) -> Unit,
|
||||
suggestions: List<String>,
|
||||
enabled: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
var focused by remember { mutableStateOf(false) }
|
||||
|
||||
val already = remember(value) {
|
||||
value.split(',').map { it.trim().lowercase() }.filter { it.isNotEmpty() }.toSet()
|
||||
}
|
||||
val token = value.substringAfterLast(',').trim()
|
||||
val matches = remember(token, suggestions, already) {
|
||||
if (token.isEmpty()) {
|
||||
emptyList()
|
||||
} else {
|
||||
suggestions.asSequence()
|
||||
.filter { it.lowercase().contains(token.lowercase()) }
|
||||
.filter { it.lowercase() !in already }
|
||||
.distinct()
|
||||
.take(MAX_SUGGESTIONS)
|
||||
.toList()
|
||||
}
|
||||
}
|
||||
|
||||
Column(modifier = modifier) {
|
||||
OutlinedTextField(
|
||||
value = value,
|
||||
onValueChange = onValueChange,
|
||||
label = { Text("Tags") },
|
||||
placeholder = { Text("comma-separated") },
|
||||
singleLine = true,
|
||||
enabled = enabled,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.onFocusChanged { focused = it.isFocused },
|
||||
)
|
||||
if (enabled && focused && matches.isNotEmpty()) {
|
||||
Spacer(Modifier.height(6.dp))
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.horizontalScroll(rememberScrollState()),
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
) {
|
||||
for (tag in matches) {
|
||||
SuggestionChip(
|
||||
onClick = { onValueChange(completeToken(value, tag)) },
|
||||
label = { Text(tag) },
|
||||
colors = SuggestionChipDefaults.suggestionChipColors(
|
||||
labelColor = GruvAqua,
|
||||
containerColor = GruvBg1,
|
||||
),
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.width(2.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Replaces the in-progress token (after the last comma) with [chosen] + ", ". */
|
||||
private fun completeToken(current: String, chosen: String): String {
|
||||
val lastComma = current.lastIndexOf(',')
|
||||
val head = if (lastComma >= 0) current.substring(0, lastComma + 1) + " " else ""
|
||||
return head + chosen + ", "
|
||||
}
|
||||
@@ -109,11 +109,30 @@ class UploadViewModel(app: Application) : AndroidViewModel(app) {
|
||||
|
||||
val isConfigured: Boolean get() = settings.isConfigured
|
||||
|
||||
// Known tag names for autocomplete, shared by the Post and Manage screens.
|
||||
// Best-effort: fetched from /api/tags.json, empty until it arrives.
|
||||
var tagSuggestions by mutableStateOf<List<String>>(emptyList())
|
||||
private set
|
||||
|
||||
init {
|
||||
refreshTags()
|
||||
}
|
||||
|
||||
/** Refreshes the autocomplete tag list; silent on any failure. */
|
||||
fun refreshTags() {
|
||||
if (!settings.isConfigured) return
|
||||
viewModelScope.launch {
|
||||
val tags = withContext(Dispatchers.IO) { client().fetchTags() }
|
||||
if (tags.isNotEmpty()) tagSuggestions = tags
|
||||
}
|
||||
}
|
||||
|
||||
// ── Post screen ─────────────────────────────────────────────────────────
|
||||
|
||||
/** Replaces the current selection with [uris] and loads each preview. */
|
||||
fun selectImages(uris: List<Uri>) {
|
||||
if (uris.isEmpty()) return
|
||||
refreshTags()
|
||||
val items = uris.map { PickedItem(source = it) }
|
||||
post = post.copy(items = items)
|
||||
|
||||
@@ -264,6 +283,7 @@ class UploadViewModel(app: Application) : AndroidViewModel(app) {
|
||||
return
|
||||
}
|
||||
manage = manage.copy(loading = true, error = null, message = null)
|
||||
refreshTags()
|
||||
viewModelScope.launch {
|
||||
val result = withContext(Dispatchers.IO) { client().listPhotos() }
|
||||
when (result) {
|
||||
@@ -283,9 +303,11 @@ class UploadViewModel(app: Application) : AndroidViewModel(app) {
|
||||
private fun loadThumbnails(photos: List<RemotePhoto>) {
|
||||
for (p in photos) {
|
||||
viewModelScope.launch {
|
||||
// Fetch via the authed /api/thumb/<slug> endpoint, not the static
|
||||
// Fetch via the /api/thumb/<slug> endpoint, not the static
|
||||
// /thumbs/ path: a front-door botwall can block this non-browser
|
||||
// client from the static site while /api/ is proxied to the app.
|
||||
// The endpoint is unguarded (public image data), so it never
|
||||
// 401s and can't get the app banned by an auth-fail jail.
|
||||
val bmp = withContext(Dispatchers.IO) { client().fetchThumbnail("/api/thumb/" + p.slug) }
|
||||
if (bmp != null) updateManage(p.slug) { it.copy(preview = bmp) }
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ add_executable(uploader
|
||||
main.cpp
|
||||
mainwindow.cpp
|
||||
photoitemwidget.cpp
|
||||
managepostwidget.cpp
|
||||
tagcompleter.cpp
|
||||
settings.cpp
|
||||
uploadclient.cpp
|
||||
imageprep.cpp
|
||||
|
||||
+241
-1
@@ -1,17 +1,25 @@
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include "imageprep.h"
|
||||
#include "managepostwidget.h"
|
||||
#include "photoitemwidget.h"
|
||||
#include "tagcompleter.h"
|
||||
#include "uploadclient.h"
|
||||
|
||||
#include <QDate>
|
||||
#include <QDateEdit>
|
||||
#include <QDateTime>
|
||||
#include <QDialog>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QFileDialog>
|
||||
#include <QFileInfo>
|
||||
#include <QFormLayout>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QScrollArea>
|
||||
#include <QSpinBox>
|
||||
@@ -29,6 +37,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
m_stack = new QStackedWidget(this);
|
||||
m_stack->addWidget(buildSettingsPage()); // index 0
|
||||
m_stack->addWidget(buildPostPage()); // index 1
|
||||
m_stack->addWidget(buildManagePage()); // index 2
|
||||
setCentralWidget(m_stack);
|
||||
|
||||
if (m_settings.isConfigured())
|
||||
@@ -112,9 +121,51 @@ QWidget *MainWindow::buildPostPage()
|
||||
m_lastPost->setWordWrap(true);
|
||||
outer->addWidget(m_lastPost);
|
||||
|
||||
auto *bottomRow = new QHBoxLayout;
|
||||
auto *manageButton = new QPushButton(QStringLiteral("Manage"));
|
||||
connect(manageButton, &QPushButton::clicked, this, &MainWindow::showManage);
|
||||
bottomRow->addWidget(manageButton);
|
||||
|
||||
auto *settingsButton = new QPushButton(QStringLiteral("Settings"));
|
||||
connect(settingsButton, &QPushButton::clicked, this, &MainWindow::showSettings);
|
||||
outer->addWidget(settingsButton);
|
||||
bottomRow->addWidget(settingsButton);
|
||||
outer->addLayout(bottomRow);
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
QWidget *MainWindow::buildManagePage()
|
||||
{
|
||||
auto *page = new QWidget;
|
||||
auto *outer = new QVBoxLayout(page);
|
||||
outer->setContentsMargins(24, 24, 24, 24);
|
||||
|
||||
auto *header = new QHBoxLayout;
|
||||
auto *title = new QLabel(QStringLiteral("<b>Manage posts</b>"));
|
||||
header->addWidget(title);
|
||||
header->addStretch();
|
||||
m_manageRefresh = new QPushButton(QStringLiteral("Refresh"));
|
||||
connect(m_manageRefresh, &QPushButton::clicked, this, &MainWindow::loadManage);
|
||||
header->addWidget(m_manageRefresh);
|
||||
auto *back = new QPushButton(QStringLiteral("Back"));
|
||||
connect(back, &QPushButton::clicked, this, &MainWindow::showPost);
|
||||
header->addWidget(back);
|
||||
outer->addLayout(header);
|
||||
|
||||
m_manageStatus = new QLabel;
|
||||
m_manageStatus->setWordWrap(true);
|
||||
outer->addWidget(m_manageStatus);
|
||||
|
||||
auto *scroll = new QScrollArea;
|
||||
scroll->setWidgetResizable(true);
|
||||
auto *container = new QWidget;
|
||||
m_manageLayout = new QVBoxLayout(container);
|
||||
m_manageLayout->setAlignment(Qt::AlignTop);
|
||||
m_manageEmptyHint = new QLabel(QStringLiteral("No posts."));
|
||||
m_manageEmptyHint->setAlignment(Qt::AlignCenter);
|
||||
m_manageLayout->addWidget(m_manageEmptyHint);
|
||||
scroll->setWidget(container);
|
||||
outer->addWidget(scroll, 1);
|
||||
|
||||
return page;
|
||||
}
|
||||
@@ -139,9 +190,21 @@ void MainWindow::showPost()
|
||||
} else {
|
||||
m_lastPost->clear();
|
||||
}
|
||||
refreshTags();
|
||||
m_stack->setCurrentIndex(1);
|
||||
}
|
||||
|
||||
void MainWindow::showManage()
|
||||
{
|
||||
if (!m_settings.isConfigured()) {
|
||||
showSettings();
|
||||
return;
|
||||
}
|
||||
m_stack->setCurrentIndex(2);
|
||||
refreshTags();
|
||||
loadManage();
|
||||
}
|
||||
|
||||
void MainWindow::saveAndTest()
|
||||
{
|
||||
// Concurrency is not a credential; persist it regardless of URL validity.
|
||||
@@ -162,6 +225,13 @@ void MainWindow::saveAndTest()
|
||||
m_settings.writePassword(m_password->text());
|
||||
m_baseUrl->setText(m_settings.baseUrl());
|
||||
|
||||
// The manage client caches the credentials it was built with; drop it so
|
||||
// the next manage action rebuilds with the ones just saved.
|
||||
if (m_manageClient) {
|
||||
m_manageClient->deleteLater();
|
||||
m_manageClient = nullptr;
|
||||
}
|
||||
|
||||
m_saveButton->setEnabled(false);
|
||||
m_settingsStatus->setText(QStringLiteral("Testing…"));
|
||||
|
||||
@@ -192,6 +262,7 @@ void MainWindow::addItem(const QString &path)
|
||||
{
|
||||
m_emptyHint->hide();
|
||||
auto *item = new PhotoItemWidget(path);
|
||||
item->setTagSuggestions(m_tagSuggestions);
|
||||
connect(item, &PhotoItemWidget::removeRequested, this, &MainWindow::removeItem);
|
||||
m_items.append(item);
|
||||
m_itemsLayout->addWidget(item);
|
||||
@@ -359,3 +430,172 @@ void MainWindow::refreshPosting()
|
||||
m_postSummary->setText(summary);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Manage page -----------------------------------------------------------
|
||||
|
||||
UploadClient *MainWindow::manageClient()
|
||||
{
|
||||
if (!m_manageClient) {
|
||||
m_manageClient = new UploadClient(m_settings.baseUrl(), m_settings.username(),
|
||||
m_settings.readPassword(), this);
|
||||
|
||||
// Thumbnails arrive one per request; route each to its row by slug.
|
||||
connect(m_manageClient, &UploadClient::thumbnailReady, this,
|
||||
[this](const QString &slug, const QImage &image) {
|
||||
for (ManagePostWidget *w : std::as_const(m_manageItems))
|
||||
if (w->slug() == slug)
|
||||
w->setThumbnail(image);
|
||||
});
|
||||
|
||||
// The tag list feeds both the post cards and the edit dialog.
|
||||
connect(m_manageClient, &UploadClient::tagsFetched, this,
|
||||
[this](const QStringList &tags) {
|
||||
if (tags.isEmpty())
|
||||
return; // best-effort; keep whatever we had
|
||||
m_tagSuggestions = tags;
|
||||
applyTagSuggestions();
|
||||
});
|
||||
}
|
||||
return m_manageClient;
|
||||
}
|
||||
|
||||
void MainWindow::refreshTags()
|
||||
{
|
||||
if (m_settings.isConfigured())
|
||||
manageClient()->fetchTags();
|
||||
}
|
||||
|
||||
void MainWindow::applyTagSuggestions()
|
||||
{
|
||||
for (PhotoItemWidget *item : std::as_const(m_items))
|
||||
item->setTagSuggestions(m_tagSuggestions);
|
||||
}
|
||||
|
||||
void MainWindow::clearManageItems()
|
||||
{
|
||||
for (ManagePostWidget *item : std::as_const(m_manageItems))
|
||||
item->deleteLater();
|
||||
m_manageItems.clear();
|
||||
m_manageEmptyHint->show();
|
||||
}
|
||||
|
||||
void MainWindow::loadManage()
|
||||
{
|
||||
if (!m_settings.isConfigured()) {
|
||||
m_manageStatus->setText(QStringLiteral("Set the server URL and credentials first"));
|
||||
return;
|
||||
}
|
||||
m_manageStatus->setText(QStringLiteral("Loading…"));
|
||||
m_manageRefresh->setEnabled(false);
|
||||
|
||||
connect(
|
||||
manageClient(), &UploadClient::photosListed, this,
|
||||
[this](const QList<RemotePhoto> &photos, const QString &error) {
|
||||
m_manageRefresh->setEnabled(true);
|
||||
clearManageItems();
|
||||
if (!error.isEmpty()) {
|
||||
m_manageStatus->setText(error);
|
||||
return;
|
||||
}
|
||||
m_manageStatus->clear();
|
||||
if (photos.isEmpty())
|
||||
return;
|
||||
m_manageEmptyHint->hide();
|
||||
for (const RemotePhoto &p : photos) {
|
||||
auto *item = new ManagePostWidget(p.slug, p.caption, p.date, p.tags,
|
||||
m_settings.baseUrl() + p.url);
|
||||
connect(item, &ManagePostWidget::editRequested, this, &MainWindow::editItem);
|
||||
connect(item, &ManagePostWidget::deleteRequested, this, &MainWindow::deleteItem);
|
||||
m_manageItems.append(item);
|
||||
m_manageLayout->addWidget(item);
|
||||
manageClient()->fetchThumbnail(p.slug);
|
||||
}
|
||||
},
|
||||
Qt::SingleShotConnection);
|
||||
manageClient()->listPhotos();
|
||||
}
|
||||
|
||||
void MainWindow::editItem(ManagePostWidget *item)
|
||||
{
|
||||
QDialog dialog(this);
|
||||
dialog.setWindowTitle(QStringLiteral("Edit %1").arg(item->slug()));
|
||||
auto *layout = new QVBoxLayout(&dialog);
|
||||
|
||||
auto *form = new QFormLayout;
|
||||
auto *caption = new QLineEdit(item->caption());
|
||||
auto *date = new QDateEdit(QDate::fromString(item->date(), QStringLiteral("yyyy-MM-dd")));
|
||||
date->setCalendarPopup(true);
|
||||
date->setDisplayFormat(QStringLiteral("yyyy-MM-dd"));
|
||||
if (!date->date().isValid())
|
||||
date->setDate(QDate::currentDate());
|
||||
auto *tags = new QLineEdit(item->tags().join(QStringLiteral(", ")));
|
||||
auto *completer = new TagCompleter(&dialog);
|
||||
completer->setSuggestions(m_tagSuggestions);
|
||||
tags->setCompleter(completer);
|
||||
form->addRow(QStringLiteral("Caption"), caption);
|
||||
form->addRow(QStringLiteral("Date"), date);
|
||||
form->addRow(QStringLiteral("Tags"), tags);
|
||||
layout->addLayout(form);
|
||||
|
||||
auto *buttons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
|
||||
connect(buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
|
||||
connect(buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
|
||||
layout->addWidget(buttons);
|
||||
|
||||
if (dialog.exec() != QDialog::Accepted)
|
||||
return;
|
||||
|
||||
const QString newCaption = caption->text();
|
||||
const QString newDate = date->date().toString(QStringLiteral("yyyy-MM-dd"));
|
||||
const QString newTags = tags->text();
|
||||
|
||||
item->setBusy(true);
|
||||
connect(
|
||||
manageClient(), &UploadClient::editFinished, this,
|
||||
[this, item, newCaption, newDate, newTags](const QString &slug, const QString &error) {
|
||||
item->setBusy(false);
|
||||
if (!error.isEmpty()) {
|
||||
m_manageStatus->setText(QStringLiteral("Edit failed: %1").arg(error));
|
||||
return;
|
||||
}
|
||||
QStringList tagList;
|
||||
const auto parts = newTags.split(QLatin1Char(','), Qt::SkipEmptyParts);
|
||||
for (const QString &t : parts) {
|
||||
const QString trimmed = t.trimmed();
|
||||
if (!trimmed.isEmpty())
|
||||
tagList << trimmed;
|
||||
}
|
||||
item->applyEdit(newCaption, newDate, tagList);
|
||||
m_manageStatus->setText(QStringLiteral("Saved %1").arg(slug));
|
||||
},
|
||||
Qt::SingleShotConnection);
|
||||
manageClient()->editPost(item->slug(), newCaption, newDate, newTags);
|
||||
}
|
||||
|
||||
void MainWindow::deleteItem(ManagePostWidget *item)
|
||||
{
|
||||
const QString name = item->caption().isEmpty() ? item->slug() : item->caption();
|
||||
const auto choice = QMessageBox::question(
|
||||
this, QStringLiteral("Move to trash?"),
|
||||
QStringLiteral("\"%1\" will be moved to the server's trash.").arg(name));
|
||||
if (choice != QMessageBox::Yes)
|
||||
return;
|
||||
|
||||
item->setBusy(true);
|
||||
connect(
|
||||
manageClient(), &UploadClient::deleteFinished, this,
|
||||
[this, item](const QString &, const QString &error) {
|
||||
if (!error.isEmpty()) {
|
||||
item->setBusy(false);
|
||||
m_manageStatus->setText(QStringLiteral("Delete failed: %1").arg(error));
|
||||
return;
|
||||
}
|
||||
m_manageItems.removeOne(item);
|
||||
item->deleteLater();
|
||||
m_manageStatus->setText(QStringLiteral("Moved to trash"));
|
||||
if (m_manageItems.isEmpty())
|
||||
m_manageEmptyHint->show();
|
||||
},
|
||||
Qt::SingleShotConnection);
|
||||
manageClient()->deletePost(item->slug());
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <QList>
|
||||
#include <QMainWindow>
|
||||
#include <QStringList>
|
||||
|
||||
#include "settings.h"
|
||||
|
||||
@@ -13,6 +14,8 @@ class QSpinBox;
|
||||
class QStackedWidget;
|
||||
class QVBoxLayout;
|
||||
class PhotoItemWidget;
|
||||
class ManagePostWidget;
|
||||
class UploadClient;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
@@ -23,9 +26,11 @@ public:
|
||||
private:
|
||||
QWidget *buildSettingsPage();
|
||||
QWidget *buildPostPage();
|
||||
QWidget *buildManagePage();
|
||||
|
||||
void showSettings();
|
||||
void showPost();
|
||||
void showManage();
|
||||
|
||||
void saveAndTest();
|
||||
void chooseImages();
|
||||
@@ -38,6 +43,17 @@ private:
|
||||
int inFlightCount() const; // items currently preparing or uploading
|
||||
void refreshPosting(); // updates the Post button + summary from item states
|
||||
|
||||
// Manage page.
|
||||
UploadClient *manageClient(); // lazily-built, lives as long as the window
|
||||
void loadManage(); // fetches the post list and thumbnails
|
||||
void clearManageItems();
|
||||
void editItem(ManagePostWidget *item);
|
||||
void deleteItem(ManagePostWidget *item);
|
||||
|
||||
// Tag autocomplete.
|
||||
void refreshTags(); // fetches /api/tags.json
|
||||
void applyTagSuggestions(); // pushes the list onto every post card
|
||||
|
||||
Settings m_settings;
|
||||
|
||||
QStackedWidget *m_stack = nullptr;
|
||||
@@ -66,4 +82,16 @@ private:
|
||||
QString m_batchUsername;
|
||||
QString m_batchPassword;
|
||||
QString m_batchCacheDir;
|
||||
|
||||
// Manage page
|
||||
QVBoxLayout *m_manageLayout = nullptr; // holds the ManagePostWidgets
|
||||
QList<ManagePostWidget *> m_manageItems;
|
||||
QLabel *m_manageEmptyHint = nullptr;
|
||||
QLabel *m_manageStatus = nullptr;
|
||||
QPushButton *m_manageRefresh = nullptr;
|
||||
UploadClient *m_manageClient = nullptr; // lazily created, reused for manage ops
|
||||
|
||||
// Known tag names for autocomplete, shared by the post cards and the edit
|
||||
// dialog. Best-effort: empty until /api/tags.json arrives.
|
||||
QStringList m_tagSuggestions;
|
||||
};
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "managepostwidget.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QImage>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <QPushButton>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
namespace {
|
||||
constexpr int kThumbSize = 72;
|
||||
}
|
||||
|
||||
ManagePostWidget::ManagePostWidget(const QString &slug, const QString &caption,
|
||||
const QString &date, const QStringList &tags,
|
||||
const QString &url, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_slug(slug)
|
||||
, m_caption(caption)
|
||||
, m_date(date)
|
||||
, m_tags(tags)
|
||||
, m_url(url)
|
||||
{
|
||||
auto *row = new QHBoxLayout(this);
|
||||
row->setContentsMargins(0, 4, 0, 4);
|
||||
|
||||
m_thumb = new QLabel;
|
||||
m_thumb->setFixedSize(kThumbSize, kThumbSize);
|
||||
m_thumb->setAlignment(Qt::AlignCenter);
|
||||
m_thumb->setFrameShape(QFrame::Box);
|
||||
row->addWidget(m_thumb, 0, Qt::AlignTop);
|
||||
|
||||
auto *fields = new QVBoxLayout;
|
||||
m_captionLabel = new QLabel;
|
||||
m_captionLabel->setTextFormat(Qt::RichText);
|
||||
m_captionLabel->setOpenExternalLinks(true);
|
||||
m_captionLabel->setWordWrap(true);
|
||||
fields->addWidget(m_captionLabel);
|
||||
|
||||
m_metaLabel = new QLabel;
|
||||
m_metaLabel->setWordWrap(true);
|
||||
m_metaLabel->setStyleSheet(QStringLiteral("color:#a89984;"));
|
||||
fields->addWidget(m_metaLabel);
|
||||
row->addLayout(fields, 1);
|
||||
|
||||
m_edit = new QPushButton(QStringLiteral("Edit"));
|
||||
connect(m_edit, &QPushButton::clicked, this, [this]() { emit editRequested(this); });
|
||||
row->addWidget(m_edit, 0, Qt::AlignTop);
|
||||
|
||||
m_delete = new QPushButton(QStringLiteral("Delete"));
|
||||
connect(m_delete, &QPushButton::clicked, this, [this]() { emit deleteRequested(this); });
|
||||
row->addWidget(m_delete, 0, Qt::AlignTop);
|
||||
|
||||
refreshSummary();
|
||||
}
|
||||
|
||||
void ManagePostWidget::setThumbnail(const QImage &image)
|
||||
{
|
||||
if (image.isNull()) {
|
||||
m_thumb->setText(QStringLiteral("?"));
|
||||
return;
|
||||
}
|
||||
m_thumb->setPixmap(QPixmap::fromImage(image).scaled(
|
||||
kThumbSize, kThumbSize, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
|
||||
}
|
||||
|
||||
void ManagePostWidget::applyEdit(const QString &caption, const QString &date,
|
||||
const QStringList &tags)
|
||||
{
|
||||
m_caption = caption;
|
||||
m_date = date;
|
||||
m_tags = tags;
|
||||
refreshSummary();
|
||||
}
|
||||
|
||||
void ManagePostWidget::setBusy(bool busy)
|
||||
{
|
||||
m_edit->setEnabled(!busy);
|
||||
m_delete->setEnabled(!busy);
|
||||
}
|
||||
|
||||
void ManagePostWidget::refreshSummary()
|
||||
{
|
||||
const QString shown = m_caption.isEmpty() ? m_slug : m_caption;
|
||||
m_captionLabel->setText(
|
||||
QStringLiteral("<a href=\"%1\">%2</a>").arg(m_url, shown.toHtmlEscaped()));
|
||||
|
||||
QString meta = m_date;
|
||||
if (!m_tags.isEmpty())
|
||||
meta += QStringLiteral(" · ") + m_tags.join(QStringLiteral(", "));
|
||||
m_metaLabel->setText(meta);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QWidget>
|
||||
|
||||
class QImage;
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
|
||||
// One published post on the Manage page: thumbnail, caption/date/tags summary,
|
||||
// and Edit / Delete buttons. Mirrors a row of the web /manage list and the
|
||||
// Android manage screen.
|
||||
class ManagePostWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ManagePostWidget(const QString &slug, const QString &caption, const QString &date,
|
||||
const QStringList &tags, const QString &url, QWidget *parent = nullptr);
|
||||
|
||||
QString slug() const { return m_slug; }
|
||||
QString caption() const { return m_caption; }
|
||||
QString date() const { return m_date; }
|
||||
QStringList tags() const { return m_tags; }
|
||||
|
||||
void setThumbnail(const QImage &image);
|
||||
void applyEdit(const QString &caption, const QString &date, const QStringList &tags);
|
||||
void setBusy(bool busy); // a save/delete is in flight
|
||||
|
||||
signals:
|
||||
void editRequested(ManagePostWidget *self);
|
||||
void deleteRequested(ManagePostWidget *self);
|
||||
|
||||
private:
|
||||
void refreshSummary();
|
||||
|
||||
QString m_slug;
|
||||
QString m_caption;
|
||||
QString m_date;
|
||||
QStringList m_tags;
|
||||
QString m_url;
|
||||
|
||||
QLabel *m_thumb = nullptr;
|
||||
QLabel *m_captionLabel = nullptr;
|
||||
QLabel *m_metaLabel = nullptr;
|
||||
QPushButton *m_edit = nullptr;
|
||||
QPushButton *m_delete = nullptr;
|
||||
};
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "photoitemwidget.h"
|
||||
|
||||
#include "tagcompleter.h"
|
||||
|
||||
#include <QDate>
|
||||
#include <QDateEdit>
|
||||
#include <QHBoxLayout>
|
||||
@@ -49,6 +51,8 @@ PhotoItemWidget::PhotoItemWidget(QString sourcePath, QWidget *parent)
|
||||
|
||||
m_tags = new QLineEdit;
|
||||
m_tags->setPlaceholderText(QStringLiteral("tags (comma-separated)"));
|
||||
m_tagCompleter = new TagCompleter(this);
|
||||
m_tags->setCompleter(m_tagCompleter);
|
||||
fields->addWidget(m_tags);
|
||||
|
||||
m_statusLabel = new QLabel;
|
||||
@@ -79,6 +83,11 @@ QString PhotoItemWidget::tags() const
|
||||
return m_tags->text();
|
||||
}
|
||||
|
||||
void PhotoItemWidget::setTagSuggestions(const QStringList &tags)
|
||||
{
|
||||
m_tagCompleter->setSuggestions(tags);
|
||||
}
|
||||
|
||||
void PhotoItemWidget::setStatus(Status status, const QString &message, const QString &url)
|
||||
{
|
||||
m_status = status;
|
||||
|
||||
@@ -7,6 +7,7 @@ class QDateEdit;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
class QPushButton;
|
||||
class TagCompleter;
|
||||
|
||||
// One photo in the batch: thumbnail, caption, date, and its own upload status.
|
||||
// A single-photo post is just a batch of one, so the window runs one code path.
|
||||
@@ -26,6 +27,9 @@ public:
|
||||
|
||||
void setStatus(Status status, const QString &message = {}, const QString &url = {});
|
||||
|
||||
// Fills the tags field's autocomplete with the known tag names.
|
||||
void setTagSuggestions(const QStringList &tags);
|
||||
|
||||
signals:
|
||||
void removeRequested(PhotoItemWidget *self);
|
||||
|
||||
@@ -39,4 +43,5 @@ private:
|
||||
QLineEdit *m_tags = nullptr;
|
||||
QLabel *m_statusLabel = nullptr;
|
||||
QPushButton *m_remove = nullptr;
|
||||
TagCompleter *m_tagCompleter = nullptr;
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "tagcompleter.h"
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QStringListModel>
|
||||
|
||||
TagCompleter::TagCompleter(QObject *parent)
|
||||
: QCompleter(parent)
|
||||
, m_model(new QStringListModel(this))
|
||||
{
|
||||
setModel(m_model);
|
||||
setCaseSensitivity(Qt::CaseInsensitive);
|
||||
setFilterMode(Qt::MatchContains); // match anywhere in the tag, like Android
|
||||
setCompletionMode(QCompleter::PopupCompletion);
|
||||
}
|
||||
|
||||
void TagCompleter::setSuggestions(const QStringList &tags)
|
||||
{
|
||||
m_model->setStringList(tags);
|
||||
}
|
||||
|
||||
// Complete against the token after the last comma, ignoring the tags already
|
||||
// entered before it.
|
||||
QStringList TagCompleter::splitPath(const QString &path) const
|
||||
{
|
||||
return QStringList(path.section(QLatin1Char(','), -1).trimmed());
|
||||
}
|
||||
|
||||
// Rebuild the full field value: everything up to and including the last comma,
|
||||
// then the chosen tag and a trailing ", " ready for the next one.
|
||||
QString TagCompleter::pathFromIndex(const QModelIndex &index) const
|
||||
{
|
||||
const QString completion = QCompleter::pathFromIndex(index);
|
||||
const auto *edit = qobject_cast<const QLineEdit *>(widget());
|
||||
if (!edit)
|
||||
return completion;
|
||||
|
||||
const QString text = edit->text();
|
||||
const int lastComma = text.lastIndexOf(QLatin1Char(','));
|
||||
const QString head =
|
||||
lastComma >= 0 ? text.left(lastComma + 1) + QLatin1Char(' ') : QString();
|
||||
return head + completion + QStringLiteral(", ");
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <QCompleter>
|
||||
#include <QStringList>
|
||||
|
||||
class QStringListModel;
|
||||
|
||||
// A QCompleter for a comma-separated tags line edit. It completes only the
|
||||
// token currently being typed (the text after the last comma) and, on
|
||||
// selection, splices the chosen tag back in place of that token — the same
|
||||
// per-token behaviour as the web and Android tag fields. Attach with
|
||||
// QLineEdit::setCompleter; call setSuggestions() to (re)fill the tag list.
|
||||
class TagCompleter : public QCompleter
|
||||
{
|
||||
public:
|
||||
explicit TagCompleter(QObject *parent = nullptr);
|
||||
|
||||
void setSuggestions(const QStringList &tags);
|
||||
|
||||
protected:
|
||||
QStringList splitPath(const QString &path) const override;
|
||||
QString pathFromIndex(const QModelIndex &index) const override;
|
||||
|
||||
private:
|
||||
QStringListModel *m_model;
|
||||
};
|
||||
@@ -4,9 +4,13 @@
|
||||
#include <QFileInfo>
|
||||
#include <QHttpMultiPart>
|
||||
#include <QHttpPart>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QUrlQuery>
|
||||
|
||||
UploadClient::UploadClient(QString baseUrl, QString username, QString password, QObject *parent)
|
||||
: QObject(parent)
|
||||
@@ -125,3 +129,152 @@ void UploadClient::testConnection()
|
||||
emit testFinished(QStringLiteral("Server returned %1").arg(status));
|
||||
});
|
||||
}
|
||||
|
||||
// --- Manage API ------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
QString httpError(QNetworkReply *reply, int status)
|
||||
{
|
||||
if (status == 401 || status == 403)
|
||||
return QStringLiteral("Wrong username or password");
|
||||
if (status == 0)
|
||||
return reply->errorString();
|
||||
const QString body = QString::fromUtf8(reply->readAll()).trimmed();
|
||||
return body.isEmpty() ? QStringLiteral("Server returned %1").arg(status) : body;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void UploadClient::listPhotos()
|
||||
{
|
||||
QNetworkRequest request(QUrl(m_base + QStringLiteral("/api/photos")));
|
||||
request.setRawHeader("Authorization", authHeader());
|
||||
|
||||
QNetworkReply *reply = m_net->get(request);
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
reply->deleteLater();
|
||||
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
|
||||
emit photosListed({}, httpError(reply, status));
|
||||
return;
|
||||
}
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
||||
if (!doc.isArray()) {
|
||||
emit photosListed({}, QStringLiteral("Could not read the server's response"));
|
||||
return;
|
||||
}
|
||||
QList<RemotePhoto> photos;
|
||||
const QJsonArray arr = doc.array();
|
||||
for (const QJsonValue &v : arr) {
|
||||
const QJsonObject o = v.toObject();
|
||||
RemotePhoto p;
|
||||
p.slug = o.value(QStringLiteral("slug")).toString();
|
||||
p.caption = o.value(QStringLiteral("caption")).toString();
|
||||
p.date = o.value(QStringLiteral("date")).toString();
|
||||
p.thumb = o.value(QStringLiteral("thumb")).toString();
|
||||
p.url = o.value(QStringLiteral("url")).toString();
|
||||
const QJsonArray tags = o.value(QStringLiteral("tags")).toArray();
|
||||
for (const QJsonValue &t : tags)
|
||||
p.tags << t.toString();
|
||||
photos << p;
|
||||
}
|
||||
emit photosListed(photos, QString());
|
||||
});
|
||||
}
|
||||
|
||||
void UploadClient::fetchTags()
|
||||
{
|
||||
QNetworkRequest request(QUrl(m_base + QStringLiteral("/api/tags.json")));
|
||||
request.setRawHeader("Authorization", authHeader());
|
||||
|
||||
QNetworkReply *reply = m_net->get(request);
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
reply->deleteLater();
|
||||
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
|
||||
emit tagsFetched({}); // best-effort; autocomplete is a convenience
|
||||
return;
|
||||
}
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
||||
QStringList names;
|
||||
for (const QJsonValue &v : doc.array()) {
|
||||
const QString name = v.toObject().value(QStringLiteral("tag")).toString();
|
||||
if (!name.isEmpty())
|
||||
names << name;
|
||||
}
|
||||
emit tagsFetched(names);
|
||||
});
|
||||
}
|
||||
|
||||
void UploadClient::fetchThumbnail(const QString &slug)
|
||||
{
|
||||
QNetworkRequest request(QUrl(m_base + QStringLiteral("/api/thumb/") + slug));
|
||||
request.setRawHeader("Authorization", authHeader());
|
||||
|
||||
QNetworkReply *reply = m_net->get(request);
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply, slug]() {
|
||||
reply->deleteLater();
|
||||
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
|
||||
emit thumbnailReady(slug, QImage()); // null image on any failure
|
||||
return;
|
||||
}
|
||||
QImage image;
|
||||
image.loadFromData(reply->readAll());
|
||||
emit thumbnailReady(slug, image);
|
||||
});
|
||||
}
|
||||
|
||||
void UploadClient::editPost(const QString &slug, const QString &caption,
|
||||
const QString &date, const QString &tags)
|
||||
{
|
||||
QUrlQuery form;
|
||||
form.addQueryItem(QStringLiteral("slug"), slug);
|
||||
form.addQueryItem(QStringLiteral("caption"), caption);
|
||||
form.addQueryItem(QStringLiteral("date"), date);
|
||||
form.addQueryItem(QStringLiteral("tags"), tags);
|
||||
|
||||
QNetworkRequest request(QUrl(m_base + QStringLiteral("/api/edit")));
|
||||
request.setRawHeader("Authorization", authHeader());
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
||||
QStringLiteral("application/x-www-form-urlencoded"));
|
||||
|
||||
QNetworkReply *reply =
|
||||
m_net->post(request, form.toString(QUrl::FullyEncoded).toUtf8());
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply, slug]() {
|
||||
reply->deleteLater();
|
||||
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (reply->error() == QNetworkReply::NoError && status >= 200 && status < 300)
|
||||
emit editFinished(slug, QString());
|
||||
else
|
||||
emit editFinished(slug, httpError(reply, status));
|
||||
});
|
||||
}
|
||||
|
||||
void UploadClient::deletePost(const QString &slug)
|
||||
{
|
||||
QUrlQuery form;
|
||||
form.addQueryItem(QStringLiteral("slug"), slug);
|
||||
|
||||
QNetworkRequest request(QUrl(m_base + QStringLiteral("/api/delete")));
|
||||
request.setRawHeader("Authorization", authHeader());
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
||||
QStringLiteral("application/x-www-form-urlencoded"));
|
||||
|
||||
QNetworkReply *reply =
|
||||
m_net->post(request, form.toString(QUrl::FullyEncoded).toUtf8());
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply, slug]() {
|
||||
reply->deleteLater();
|
||||
const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (reply->error() != QNetworkReply::NoError || status < 200 || status >= 300) {
|
||||
emit deleteFinished(slug, httpError(reply, status));
|
||||
return;
|
||||
}
|
||||
// The server reports how many it moved; 0 means the slug wasn't found.
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
||||
const int deleted = doc.object().value(QStringLiteral("deleted")).toInt(0);
|
||||
if (deleted > 0)
|
||||
emit deleteFinished(slug, QString());
|
||||
else
|
||||
emit deleteFinished(slug, QStringLiteral("Nothing deleted — not found on the server"));
|
||||
});
|
||||
}
|
||||
@@ -1,10 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <QImage>
|
||||
#include <QList>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
class QNetworkAccessManager;
|
||||
|
||||
// One published post, as returned by GET /api/photos.
|
||||
struct RemotePhoto {
|
||||
QString slug;
|
||||
QString caption;
|
||||
QString date; // yyyy-MM-dd
|
||||
QStringList tags;
|
||||
QString thumb; // site-relative, e.g. "/thumbs/foo.jpg"
|
||||
QString url; // site-relative, e.g. "/photo/foo/"
|
||||
};
|
||||
|
||||
// Talks to the gallery's POST /upload endpoint, which sits behind nginx HTTP
|
||||
// Basic auth. The credentials go out preemptively in the Authorization header
|
||||
// on every request (never via QNetworkAccessManager's authenticationRequired
|
||||
@@ -35,12 +48,29 @@ public:
|
||||
// /health is not usable: nginx only proxies location /upload.
|
||||
void testConnection();
|
||||
|
||||
// Manage API, mirroring the web /manage and the Android app. The desktop
|
||||
// app is a non-browser client: it sends no Origin/Sec-Fetch-Site header, so
|
||||
// the state-changing posts pass the server's same-origin (CSRF) check.
|
||||
void listPhotos(); // GET /api/photos
|
||||
void fetchTags(); // GET /api/tags.json (unguarded)
|
||||
void fetchThumbnail(const QString &slug); // GET /api/thumb/<slug> (unguarded)
|
||||
void editPost(const QString &slug, const QString &caption,
|
||||
const QString &date, const QString &tags); // POST /api/edit
|
||||
void deletePost(const QString &slug); // POST /api/delete (moves to trash)
|
||||
|
||||
signals:
|
||||
void uploadFinished(UploadClient::Result result, const QString &slug,
|
||||
const QString &url, const QString &message);
|
||||
// errorMessage is empty on success.
|
||||
void testFinished(const QString &errorMessage);
|
||||
|
||||
// Manage signals. The error strings are empty on success.
|
||||
void photosListed(const QList<RemotePhoto> &photos, const QString &error);
|
||||
void tagsFetched(const QStringList &tags);
|
||||
void thumbnailReady(const QString &slug, const QImage &image);
|
||||
void editFinished(const QString &slug, const QString &error);
|
||||
void deleteFinished(const QString &slug, const QString &error);
|
||||
|
||||
private:
|
||||
QByteArray authHeader() const;
|
||||
|
||||
|
||||
@@ -59,11 +59,14 @@ func (s *Server) handleSession(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, map[string]bool{"authenticated": false})
|
||||
}
|
||||
|
||||
// handleThumb serves a photo's thumbnail from the generated output, behind the
|
||||
// same Basic auth as the rest of /api/. Non-browser clients (the Android app)
|
||||
// use this instead of the static /thumbs/<slug>.jpg path, because a front-door
|
||||
// botwall on `location /` can block a non-browser client from the static site
|
||||
// while `/api/` is proxied straight to this server.
|
||||
// handleThumb serves a photo's thumbnail from the generated output. Non-browser
|
||||
// clients (the Android app) use this instead of the static /thumbs/<slug>.jpg
|
||||
// path, because a front-door botwall on `location /` can block a non-browser
|
||||
// client from the static site while `/api/` is proxied straight to this server.
|
||||
// It is registered UNGUARDED (see server.go): thumbnails are the same public
|
||||
// data as the static /thumbs/, so there is nothing to protect, and staying off
|
||||
// the auth path means it can never 401 and so can never trip an auth-fail jail
|
||||
// that would ban the app over a per-row thumbnail load.
|
||||
func (s *Server) handleThumb(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.PathValue("slug")
|
||||
if slug == "" || slug != filepath.Base(slug) || strings.HasPrefix(slug, ".") {
|
||||
@@ -85,6 +88,30 @@ func (s *Server) handleThumb(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeContent(w, r, slug+".jpg", fi.ModTime(), f)
|
||||
}
|
||||
|
||||
// handleTagsJSON serves the generated tags.json under /api/ so the Android app
|
||||
// can offer tag autocomplete. It is registered UNGUARDED (see server.go): the
|
||||
// tag list is the same public data as the static /tags.json, so there is
|
||||
// nothing to protect, and staying off the auth path means it can never return
|
||||
// 401 and so can never trip an nginx auth-fail jail that would ban the app
|
||||
// over an autocomplete poll. It lives under /api/ only so nginx proxies it to
|
||||
// the app, reaching non-browser clients past a front-door botwall on
|
||||
// `location /` (the same botwall reason /api/thumb/ exists).
|
||||
func (s *Server) handleTagsJSON(w http.ResponseWriter, r *http.Request) {
|
||||
f, err := os.Open(filepath.Join(s.outputDir, "tags.json"))
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
http.ServeContent(w, r, "tags.json", fi.ModTime(), f)
|
||||
}
|
||||
|
||||
// handleManagePage serves the (auth-gated) edit/delete UI.
|
||||
func (s *Server) handleManagePage(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := assets.ReadFile("templates/manage.html")
|
||||
|
||||
@@ -330,3 +330,39 @@ func TestHandleListPhotos(t *testing.T) {
|
||||
t.Errorf("foo tags = %v", foo.Tags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleTagsJSON(t *testing.T) {
|
||||
s, _, output := newTestServer(t)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
s.handleTagsJSON(rec, httptest.NewRequest(http.MethodGet, "/api/tags.json", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("tags.json: code=%d", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
|
||||
t.Errorf("content-type = %q", ct)
|
||||
}
|
||||
var tags []struct {
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &tags); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
got := map[string]bool{}
|
||||
for _, tg := range tags {
|
||||
got[tg.Tag] = true
|
||||
}
|
||||
if !got["berlin"] || !got["street"] {
|
||||
t.Errorf("tags = %v, want berlin+street", tags)
|
||||
}
|
||||
|
||||
// Missing file → 404.
|
||||
if err := os.Remove(filepath.Join(output, "tags.json")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec = httptest.NewRecorder()
|
||||
s.handleTagsJSON(rec, httptest.NewRequest(http.MethodGet, "/api/tags.json", nil))
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("missing tags.json: code=%d, want 404", rec.Code)
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,16 @@ func (s *Server) Listen() error {
|
||||
mux.HandleFunc("POST /api/edit", s.guard(s.handleEdit))
|
||||
mux.HandleFunc("POST /api/delete", s.guard(s.handleDelete))
|
||||
mux.HandleFunc("POST /api/tags", s.guard(s.handleBulkTags))
|
||||
mux.HandleFunc("GET /api/thumb/{slug}", s.guard(s.handleThumb))
|
||||
// Thumbnails and the tag list — deliberately NOT guarded. Both serve the
|
||||
// same public data as the static site (thumbs under /thumbs/, the tag list
|
||||
// at /tags.json), so there is nothing to protect. Keeping them off the auth
|
||||
// path means they can never return 401 and so can never feed an nginx
|
||||
// auth-fail jail (fail2ban), which would otherwise ban the app over a
|
||||
// per-row thumbnail load or an autocomplete poll. They stay under /api/
|
||||
// only so nginx proxies them to the app, reaching non-browser clients past
|
||||
// a front-door botwall on `location /`.
|
||||
mux.HandleFunc("GET /api/thumb/{slug}", s.handleThumb)
|
||||
mux.HandleFunc("GET /api/tags.json", s.handleTagsJSON)
|
||||
mux.HandleFunc("GET /api/trash", s.guard(s.handleListTrash))
|
||||
mux.HandleFunc("POST /api/restore", s.guard(s.handleRestore))
|
||||
mux.HandleFunc("POST /api/trash/empty", s.guard(s.handleEmptyTrash))
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<meta property="og:type" content="article">
|
||||
<meta property="og:site_name" content="{{.Author}}">
|
||||
<meta property="og:title" content="{{.Photo.Caption}}">
|
||||
<meta property="og:description" content="{{.Author}} — {{.Photo.Date.Format "January 2, 2006"}}">
|
||||
<meta property="og:description" content="{{.Author}} — {{.Photo.Date.Format "January 2, 2006"}}{{if .Photo.Tags}} · {{range $i, $t := .Photo.Tags}}{{if $i}}, {{end}}{{$t.Name}}{{end}}{{end}}">
|
||||
<meta property="og:url" content="{{.BaseURL}}/photo/{{.Photo.Slug}}/">
|
||||
<meta property="og:image" content="{{.BaseURL}}/{{.Photo.OG}}">
|
||||
<meta property="og:image:type" content="image/jpeg">
|
||||
@@ -20,7 +20,7 @@
|
||||
<meta property="og:image:alt" content="{{.Photo.Caption}}">
|
||||
<meta name="twitter:card" content="summary_large_image">
|
||||
<meta name="twitter:title" content="{{.Photo.Caption}}">
|
||||
<meta name="twitter:description" content="{{.Author}} — {{.Photo.Date.Format "January 2, 2006"}}">
|
||||
<meta name="twitter:description" content="{{.Author}} — {{.Photo.Date.Format "January 2, 2006"}}{{if .Photo.Tags}} · {{range $i, $t := .Photo.Tags}}{{if $i}}, {{end}}{{$t.Name}}{{end}}{{end}}">
|
||||
<meta name="twitter:image" content="{{.BaseURL}}/{{.Photo.OG}}">
|
||||
|
||||
<link rel="stylesheet" href="{{.FontsURL}}">
|
||||
|
||||
Reference in new issue
Block a user