Revert to Docker-only source paths, fix config validation, improve transcoding info

- handlers_sources.go: revert to relative paths rooted at /media (remove standalone absolute-path mode)
- settings.html: remove manual path input, restore auto-loading source tree from /media
- config.go: remove filesystem existence checks from Validate() — paths may be temporarily unavailable
- transcoder.go: always specify fps in ffmpeg args when MaxFPS is set, preserving source fps if lower than limit
- copier.go: include source codec/format and target codec/format in transcoding task message

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-21 21:28:28 +03:00
parent e885e49647
commit 0a17d11bd1
5 changed files with 91 additions and 196 deletions

View File

@@ -3,6 +3,7 @@ package api
import ( import (
"errors" "errors"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -10,19 +11,20 @@ import (
) )
func (s *Server) handleSources(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSources(w http.ResponseWriter, r *http.Request) {
absPath, err := normalizeSourcePathQuery(r.URL.Query().Get("path")) relPath, err := normalizeSourcePath(r.URL.Query().Get("path"))
if err != nil { if err != nil {
jsonErr(w, http.StatusBadRequest, err.Error()) jsonErr(w, http.StatusBadRequest, err.Error())
return return
} }
if absPath == "" {
jsonOK(w, map[string]any{"path": "", "items": []map[string]string{}}) absPath := s.deps.Config.MediaPath
return if relPath != "" {
absPath = filepath.Join(absPath, relPath)
} }
entries, err := os.ReadDir(absPath) entries, err := os.ReadDir(absPath)
if err != nil { if err != nil {
jsonOK(w, map[string]any{"path": absPath, "items": []map[string]string{}}) jsonOK(w, map[string]any{"path": relPath, "items": []map[string]string{}})
return return
} }
@@ -36,10 +38,13 @@ func (s *Server) handleSources(w http.ResponseWriter, r *http.Request) {
if !e.IsDir() || strings.HasPrefix(e.Name(), ".") { if !e.IsDir() || strings.HasPrefix(e.Name(), ".") {
continue continue
} }
childPath := filepath.Join(absPath, e.Name()) childPath := e.Name()
if relPath != "" {
childPath = filepath.Join(relPath, childPath)
}
items = append(items, item{ items = append(items, item{
Name: e.Name(), Name: e.Name(),
Path: childPath, Path: filepath.ToSlash(childPath),
}) })
} }
@@ -48,18 +53,26 @@ func (s *Server) handleSources(w http.ResponseWriter, r *http.Request) {
}) })
jsonOK(w, map[string]any{ jsonOK(w, map[string]any{
"path": absPath, "path": relPath,
"items": items, "items": items,
}) })
} }
func normalizeSourcePathQuery(raw string) (string, error) { func normalizeSourcePath(raw string) (string, error) {
raw, _ = url.QueryUnescape(raw)
raw = strings.TrimSpace(raw) raw = strings.TrimSpace(raw)
raw = filepath.ToSlash(raw)
raw = strings.TrimPrefix(raw, "/")
if raw == "" || raw == "." { if raw == "" || raw == "." {
return "", nil return "", nil
} }
clean := filepath.Clean(raw) clean := filepath.Clean(raw)
if !filepath.IsAbs(clean) { clean = filepath.ToSlash(clean)
if clean == "." {
return "", nil
}
if clean == ".." || strings.HasPrefix(clean, "../") {
return "", errors.New("invalid source path") return "", errors.New("invalid source path")
} }
return clean, nil return clean, nil

View File

@@ -116,25 +116,7 @@ func Save(path string, cfg *Config) error {
func (c *Config) Validate() error { func (c *Config) Validate() error {
c.MediaPath = NormalizeMediaPath(c.MediaPath) c.MediaPath = NormalizeMediaPath(c.MediaPath)
if c.MediaPath != "" {
info, err := os.Stat(c.MediaPath)
if err != nil {
return errors.New("media_path is not accessible")
}
if !info.IsDir() {
return errors.New("media_path must be a directory")
}
}
c.Sources = NormalizeSources(c.Sources, c.MediaPath) c.Sources = NormalizeSources(c.Sources, c.MediaPath)
for _, source := range c.Sources {
info, err := os.Stat(source.Path)
if err != nil {
return errors.New("source path is not accessible: " + source.Path)
}
if !info.IsDir() {
return errors.New("source path must be a directory: " + source.Path)
}
}
if c.ReserveFreeGB < 0 { if c.ReserveFreeGB < 0 {
return errors.New("reserve_free_gb must be >= 0") return errors.New("reserve_free_gb must be >= 0")
} }

View File

@@ -392,9 +392,15 @@ func (c *Copier) processVideo(ctx context.Context, taskID string, database *db.D
ext := transcoder.OutputExt(profile.OutputFormat) ext := transcoder.OutputExt(profile.OutputFormat)
dstTranscoded := strings.TrimSuffix(dst, filepath.Ext(dst)) + ext dstTranscoded := strings.TrimSuffix(dst, filepath.Ext(dst)) + ext
srcFPS := fmt.Sprintf("%.2f", info.FPS)
msg := fmt.Sprintf("Transcoding %s (%s/%dch/%sfps → %s/%s/%dfps %s)",
filepath.Base(src),
info.Codec, info.AudioChannels, srcFPS,
profile.VideoCodec, profile.AudioCodec, profile.MaxFPS, profile.OutputFormat,
)
c.tasks.Update(taskID, func(t *task.Task) { c.tasks.Update(taskID, func(t *task.Task) {
t.Phase = task.PhaseTranscoding t.Phase = task.PhaseTranscoding
t.Message = "Transcoding " + filepath.Base(src) t.Message = msg
}) })
if t, ok := c.tasks.Get(taskID); ok { if t, ok := c.tasks.Get(taskID); ok {
_ = database.UpdateTask(*t) _ = database.UpdateTask(*t)

View File

@@ -97,8 +97,14 @@ func buildArgs(opts Options) []string {
if maxH := maxHeight(p.MaxResolution); maxH > 0 { if maxH := maxHeight(p.MaxResolution); maxH > 0 {
filters = append(filters, fmt.Sprintf("scale=-2:min(%d\\,ih)", maxH)) filters = append(filters, fmt.Sprintf("scale=-2:min(%d\\,ih)", maxH))
} }
if p.MaxFPS > 0 && src.FPS > float64(p.MaxFPS)+0.01 { if p.MaxFPS > 0 {
filters = append(filters, fmt.Sprintf("fps=%d", p.MaxFPS)) targetFPS := p.MaxFPS
if src.FPS > 0 && src.FPS < float64(targetFPS) {
// Источник медленнее лимита — сохраняем исходный FPS
filters = append(filters, fmt.Sprintf("fps=%.3f", src.FPS))
} else {
filters = append(filters, fmt.Sprintf("fps=%d", targetFPS))
}
} }
if len(filters) > 0 { if len(filters) > 0 {
args = append(args, "-vf", strings.Join(filters, ",")) args = append(args, "-vf", strings.Join(filters, ","))

View File

@@ -4,18 +4,16 @@
<section class="panel"> <section class="panel">
<h2>Copy Sources</h2> <h2>Copy Sources</h2>
<div class="panel-body"> <div class="panel-body">
<div class="form-hint">Add one or more root folders with source files. After that, expand each root and enable or disable individual nested folders with checkboxes.</div> <div class="form-hint">Select top-level folders or expand branches and choose individual nested directories.</div>
</div>
<div class="btn-row">
<input class="form-input" type="text" id="newSourcePath" placeholder="/media/movies">
<button type="button" class="button-primary" onclick="addSourceRoot()">Add</button>
<button type="button" class="button-secondary button-sm" onclick="reloadAllSourceTrees()">Refresh trees</button>
</div> </div>
<div class="source-list"> <div class="source-list">
<div class="source-tree" id="sourceTree"> <div class="source-tree" id="sourceTree">
<div class="text-muted source-tree-empty">No source folders added yet.</div> <div class="text-muted source-tree-empty">Loading...</div>
</div> </div>
</div> </div>
<div class="btn-row">
<button type="button" class="button-secondary button-sm" onclick="reloadSourceTree()">Refresh list</button>
</div>
</section> </section>
<section class="panel"> <section class="panel">
@@ -124,7 +122,6 @@ const builtInMediaTypes = {
video: ['.3gp', '.avi', '.m2ts', '.m4v', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.mts', '.ts', '.webm', '.wmv'], video: ['.3gp', '.avi', '.m2ts', '.m4v', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.mts', '.ts', '.webm', '.wmv'],
photo: ['.bmp', '.gif', '.heic', '.heif', '.jpeg', '.jpg', '.png', '.tif', '.tiff', '.webp'], photo: ['.bmp', '.gif', '.heic', '.heif', '.jpeg', '.jpg', '.png', '.tif', '.tiff', '.webp'],
}; };
let sourceRoots = [];
let sourceConfig = {}; let sourceConfig = {};
let allowedFilesMode = 'media_types'; let allowedFilesMode = 'media_types';
@@ -138,43 +135,13 @@ function escapeHTML(value) {
}[char])); }[char]));
} }
function pathSegments(path) { function pathDepth(path) {
return String(path || '').split(/[\\/]+/).filter(Boolean); return path ? path.split('/').length : 0;
}
function nodeName(path) {
const parts = pathSegments(path);
return parts.length ? parts[parts.length - 1] : path;
}
function normalizeComparePath(path) {
return String(path || '').replace(/\\/g, '/').replace(/\/+$/, '').toLowerCase();
}
function isSamePath(a, b) {
return normalizeComparePath(a) === normalizeComparePath(b);
}
function isPathWithin(base, candidate) {
const baseNorm = normalizeComparePath(base);
const candidateNorm = normalizeComparePath(candidate);
return candidateNorm === baseNorm || candidateNorm.startsWith(baseNorm + '/');
} }
function parentPath(path) { function parentPath(path) {
const value = String(path || '').replace(/[\\/]+$/, ''); if (!path || !path.includes('/')) return '';
const slash = Math.max(value.lastIndexOf('/'), value.lastIndexOf('\\')); return path.slice(0, path.lastIndexOf('/'));
if (slash < 0) return '';
if (slash === 2 && /^[A-Za-z]:/.test(value)) return value.slice(0, slash + 1);
if (slash === 0) return value.slice(0, 1);
return value.slice(0, slash);
}
function relativeDepth(root, path) {
if (isSamePath(root, path)) return 0;
const rootParts = pathSegments(root);
const pathParts = pathSegments(path);
return Math.max(0, pathParts.length - rootParts.length);
} }
function effectiveSourceState(path) { function effectiveSourceState(path) {
@@ -183,36 +150,37 @@ function effectiveSourceState(path) {
if (Object.prototype.hasOwnProperty.call(sourceConfig, current)) { if (Object.prototype.hasOwnProperty.call(sourceConfig, current)) {
return sourceConfig[current]; return sourceConfig[current];
} }
current = parentPath(current);
if (!current) return true; if (!current) return true;
current = parentPath(current);
} }
} }
function collectSourcesForSave() { function collectSourcesForSave() {
const items = []; const items = [];
const seen = new Set(); const seen = new Set();
const roots = sourceTree.get('') || [];
sourceRoots.forEach((root) => { for (const item of roots) {
items.push({ path: root, enabled: effectiveSourceState(root), root: true }); items.push({ path: item.path, enabled: effectiveSourceState(item.path) });
seen.add(normalizeComparePath(root)); seen.add(item.path);
}); }
Object.entries(sourceConfig).forEach(([path, enabled]) => { Object.entries(sourceConfig).forEach(([path, enabled]) => {
const key = normalizeComparePath(path); if (seen.has(path)) return;
if (seen.has(key)) return; items.push({ path, enabled });
items.push({ path, enabled, root: false });
}); });
return items.sort((a, b) => a.path.localeCompare(b.path)); return items.sort((a, b) => a.path.localeCompare(b.path));
} }
async function loadSourceChildren(path) { async function loadSourceChildren(path = '') {
if (!path || loadingNodes.has(path)) return; if (loadingNodes.has(path)) return;
loadingNodes.add(path); loadingNodes.add(path);
renderSources(); renderSources();
try { try {
const response = await fetch('/api/sources?path=' + encodeURIComponent(path)); const query = path ? '?path=' + encodeURIComponent(path) : '';
const response = await fetch('/api/sources' + query);
if (!response.ok) return; if (!response.ok) return;
const payload = await response.json(); const payload = await response.json();
sourceTree.set(path, payload.items || []); sourceTree.set(path, payload.items || []);
@@ -237,57 +205,15 @@ function toggleSource(path, checked) {
renderSources(); renderSources();
} }
function removeRoot(path) { function renderSourceNodes(parent = '') {
sourceRoots = sourceRoots.filter((root) => !isSamePath(root, path)); const items = sourceTree.get(parent) || [];
sourceTree.delete(path);
expandedNodes.delete(path);
loadingNodes.delete(path);
Object.keys(sourceConfig).forEach((key) => {
if (isPathWithin(path, key)) {
delete sourceConfig[key];
}
});
renderSources();
}
async function addSourceRoot() {
const input = document.getElementById('newSourcePath');
const path = (input?.value || '').trim();
if (!path) return;
if (sourceRoots.some((root) => isSamePath(root, path))) {
toast('This source folder is already added', 'error');
return;
}
sourceRoots.push(path);
sourceRoots.sort((a, b) => a.localeCompare(b));
sourceConfig[path] = true;
expandedNodes.add(path);
if (input) input.value = '';
await loadSourceChildren(path);
}
async function reloadAllSourceTrees() {
const roots = [...sourceRoots];
sourceTree.clear();
for (const root of roots) {
if (expandedNodes.has(root)) {
await loadSourceChildren(root);
}
}
renderSources();
}
function renderSourceNodes(root, parentPathValue) {
const items = sourceTree.get(parentPathValue) || [];
return items.map((item) => { return items.map((item) => {
const checked = effectiveSourceState(item.path); const checked = effectiveSourceState(item.path);
const expanded = expandedNodes.has(item.path); const expanded = expandedNodes.has(item.path);
const childrenKnown = sourceTree.has(item.path); const childrenKnown = sourceTree.has(item.path);
const children = childrenKnown ? sourceTree.get(item.path) : []; const children = childrenKnown ? sourceTree.get(item.path) : [];
const hasChildren = !childrenKnown || children.length > 0; const hasChildren = !childrenKnown || children.length > 0;
const pad = 16 + (relativeDepth(root, item.path) + 1) * 20; const pad = 16 + pathDepth(item.path) * 20;
return ` return `
<div class="source-node"> <div class="source-node">
@@ -302,11 +228,11 @@ function renderSourceNodes(root, parentPathValue) {
<input class="source-check" type="checkbox" data-action="toggle-check" data-path="${escapeHTML(item.path)}" ${checked ? 'checked' : ''}> <input class="source-check" type="checkbox" data-action="toggle-check" data-path="${escapeHTML(item.path)}" ${checked ? 'checked' : ''}>
<div class="source-label"> <div class="source-label">
<span class="source-item-name">${escapeHTML(item.name)}</span> <span class="source-item-name">${escapeHTML(item.name)}</span>
<span class="source-item-path">${escapeHTML(item.path)}</span> <span class="source-item-path">/media/${escapeHTML(item.path)}</span>
</div> </div>
</div> </div>
${expanded && loadingNodes.has(item.path) ? '<div class="source-loading">Loading...</div>' : ''} ${expanded && loadingNodes.has(item.path) ? '<div class="source-loading">Loading...</div>' : ''}
${expanded && childrenKnown && children.length ? `<div class="source-children">${renderSourceNodes(root, item.path)}</div>` : ''} ${expanded && childrenKnown && children.length ? `<div class="source-children">${renderSourceNodes(item.path)}</div>` : ''}
</div> </div>
`; `;
}).join(''); }).join('');
@@ -314,54 +240,24 @@ function renderSourceNodes(root, parentPathValue) {
function renderSources() { function renderSources() {
const el = document.getElementById('sourceTree'); const el = document.getElementById('sourceTree');
if (!sourceRoots.length) { const roots = sourceTree.get('');
el.innerHTML = '<div class="text-muted source-tree-empty">No source folders added yet.</div>';
if (loadingNodes.has('') && !roots) {
el.innerHTML = '<div class="text-muted source-tree-empty">Loading...</div>';
return;
}
if (!roots || !roots.length) {
el.innerHTML = '<div class="text-muted source-tree-empty">No folders found in /media.</div>';
return; return;
} }
el.innerHTML = sourceRoots.map((root) => { el.innerHTML = renderSourceNodes('');
const checked = effectiveSourceState(root);
const expanded = expandedNodes.has(root);
const childrenKnown = sourceTree.has(root);
const children = childrenKnown ? sourceTree.get(root) : [];
const hasChildren = !childrenKnown || children.length > 0;
return `
<div class="source-root-card">
<div class="source-row source-root-row">
<button
type="button"
class="source-toggle ${hasChildren ? '' : 'source-toggle-empty'}"
data-action="toggle-expand"
data-path="${escapeHTML(root)}"
${hasChildren ? '' : 'tabindex="-1" aria-hidden="true"'}
>${expanded ? '▾' : '▸'}</button>
<input class="source-check" type="checkbox" data-action="toggle-check" data-path="${escapeHTML(root)}" ${checked ? 'checked' : ''}>
<div class="source-label">
<div class="source-root-title">
<span class="source-item-name">${escapeHTML(nodeName(root))}</span>
<span class="source-root-badge">Root</span>
</div>
<span class="source-item-path">${escapeHTML(root)}</span>
</div>
<button type="button" class="button-secondary button-sm" data-action="remove-root" data-path="${escapeHTML(root)}">Remove</button>
</div>
${expanded && loadingNodes.has(root) ? '<div class="source-loading">Loading...</div>' : ''}
${expanded && childrenKnown && children.length ? `<div class="source-children">${renderSourceNodes(root, root)}</div>` : ''}
</div>
`;
}).join('');
} }
function deriveRootsFromSources(sources) { async function reloadSourceTree() {
const explicitRoots = sources.filter((source) => source.root).map((source) => source.path); sourceTree.clear();
if (explicitRoots.length) { expandedNodes.clear();
return explicitRoots; await loadSourceChildren('');
}
return sources
.map((source) => source.path)
.filter((path, index, all) => !all.some((other, otherIndex) => otherIndex !== index && isPathWithin(other, path) && !isSamePath(other, path)));
} }
function defaultAllowedExtensions() { function defaultAllowedExtensions() {
@@ -435,10 +331,7 @@ async function loadSettings() {
(cfg.sources || []).forEach((source) => { (cfg.sources || []).forEach((source) => {
sourceConfig[source.path] = !!source.enabled; sourceConfig[source.path] = !!source.enabled;
}); });
sourceRoots = deriveRootsFromSources(cfg.sources || []).sort((a, b) => a.localeCompare(b)); renderSources();
expandedNodes.clear();
sourceTree.clear();
await reloadAllSourceTrees();
} catch (error) {} } catch (error) {}
} }
@@ -446,15 +339,15 @@ async function saveSettings(event) {
event.preventDefault(); event.preventDefault();
const body = { const body = {
reserve_free_gb: parseFloat(document.getElementById('reserveGB').value) || 2, reserve_free_gb: parseFloat(document.getElementById('reserveGB').value) || 2,
dest_folder: document.getElementById('destFolder').value.trim() || 'media', dest_folder: document.getElementById('destFolder').value.trim() || 'media',
file_select_mode: document.getElementById('fileSelectMode').value, file_select_mode: document.getElementById('fileSelectMode').value,
allowed_files_mode: allowedFilesMode, allowed_files_mode: allowedFilesMode,
enabled_media_types: selectedMediaTypes(), enabled_media_types: selectedMediaTypes(),
allowed_extensions: parseExtensionsInput(document.getElementById('allowedExtensions').value), allowed_extensions: parseExtensionsInput(document.getElementById('allowedExtensions').value),
overwrite_mode: document.getElementById('overwriteMode').value, overwrite_mode: document.getElementById('overwriteMode').value,
auto_copy: document.getElementById('autoCopy').checked, auto_copy: document.getElementById('autoCopy').checked,
sources: collectSourcesForSave(), sources: collectSourcesForSave(),
}; };
try { try {
@@ -476,22 +369,16 @@ async function saveSettings(event) {
} }
document.getElementById('sourceTree').addEventListener('click', async (event) => { document.getElementById('sourceTree').addEventListener('click', async (event) => {
const button = event.target.closest('button[data-action]'); const button = event.target.closest('[data-action="toggle-expand"]');
if (!button) return; if (!button) return;
const action = button.dataset.action;
const path = button.dataset.path; const path = button.dataset.path;
if (action === 'toggle-expand') { if (expandedNodes.has(path)) {
if (expandedNodes.has(path)) { expandedNodes.delete(path);
expandedNodes.delete(path); renderSources();
renderSources(); return;
return;
}
await ensureExpanded(path);
}
if (action === 'remove-root') {
removeRoot(path);
} }
await ensureExpanded(path);
}); });
document.getElementById('sourceTree').addEventListener('change', (event) => { document.getElementById('sourceTree').addEventListener('change', (event) => {
@@ -502,5 +389,6 @@ document.getElementById('sourceTree').addEventListener('change', (event) => {
renderMediaTypeHints(); renderMediaTypeHints();
loadSettings(); loadSettings();
loadSourceChildren('');
</script> </script>
{{end}} {{end}}