feat: implement bulk upload functionality for media assets
Deploy Development / deploy (push) Successful in 34s
Test Suite / pytest-backend (push) Successful in 24s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 6s
Test Suite / playwright-tests (push) Successful in 29s
Deploy Development / deploy (push) Successful in 34s
Test Suite / pytest-backend (push) Successful in 24s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 6s
Test Suite / playwright-tests (push) Successful in 29s
- Added a new API endpoint for bulk uploading media assets, allowing users to upload multiple files in a single request. - Implemented validation for file types and sizes during the upload process, ensuring compliance with allowed formats and limits. - Enhanced the MediaLibraryPage component to support bulk file selection and visibility options, improving user experience. - Updated CSS styles for the upload interface to enhance layout and accessibility. - Added tests to verify the functionality of the new bulk upload feature and its integration with existing media asset management.
This commit is contained in:
1 parent
4f64cb105b
commit
f9e6e61244
5 files changed
+444
-8
No files matched your search
@@ -5546,6 +5546,61 @@ a.analysis-split__nav-item {
|
||||
flex: 1 1 140px;
|
||||
max-width: 240px;
|
||||
}
|
||||
.media-library__upload-row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
.media-library__upload-row .form-input {
|
||||
min-width: 0;
|
||||
flex: 0 1 160px;
|
||||
max-width: 220px;
|
||||
}
|
||||
.media-library__upload-summary {
|
||||
font-size: 0.85rem;
|
||||
color: var(--text2);
|
||||
flex: 1 1 200px;
|
||||
}
|
||||
.media-library__upload-icon {
|
||||
vertical-align: middle;
|
||||
margin-right: 6px;
|
||||
}
|
||||
.media-library__sr-file {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
.media-library__card-type {
|
||||
position: absolute;
|
||||
left: 6px;
|
||||
top: 6px;
|
||||
z-index: 2;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 8px;
|
||||
background: rgba(255, 255, 255, 0.92);
|
||||
color: var(--text2);
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
|
||||
pointer-events: none;
|
||||
}
|
||||
.media-library__card-type--compact {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
left: 3px;
|
||||
top: 3px;
|
||||
border-radius: 6px;
|
||||
}
|
||||
.media-library__card-copyright {
|
||||
position: absolute;
|
||||
right: 6px;
|
||||
@@ -5797,6 +5852,7 @@ a.analysis-split__nav-item {
|
||||
width: 72px;
|
||||
}
|
||||
.media-library__table-thumb {
|
||||
position: relative;
|
||||
width: 56px;
|
||||
height: 56px;
|
||||
border-radius: 8px;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState, useCallback } from 'react'
|
||||
import { useEffect, useState, useCallback, useRef } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import {
|
||||
LayoutGrid,
|
||||
@@ -13,6 +13,11 @@ import {
|
||||
CircleDot,
|
||||
FilePenLine,
|
||||
Copyright,
|
||||
Image,
|
||||
Video,
|
||||
FileText,
|
||||
File,
|
||||
Upload,
|
||||
} from 'lucide-react'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import api from '../utils/api'
|
||||
@@ -195,6 +200,32 @@ function previewDisplayKind(mimeType) {
|
||||
return 'other'
|
||||
}
|
||||
|
||||
const MEDIA_KIND_LABELS = {
|
||||
image: 'Bild',
|
||||
video: 'Video',
|
||||
pdf: 'PDF',
|
||||
other: 'Sonstiges',
|
||||
}
|
||||
|
||||
function MediaTypeGlyph({ mimeType, compact }) {
|
||||
const kind = previewDisplayKind(mimeType)
|
||||
const label = MEDIA_KIND_LABELS[kind] || 'Medium'
|
||||
let Icon = File
|
||||
if (kind === 'image') Icon = Image
|
||||
else if (kind === 'video') Icon = Video
|
||||
else if (kind === 'pdf') Icon = FileText
|
||||
const sz = compact ? 12 : 14
|
||||
return (
|
||||
<span
|
||||
className={`media-library__card-type${compact ? ' media-library__card-type--compact' : ''}`}
|
||||
title={label}
|
||||
aria-label={`Medientyp: ${label}`}
|
||||
>
|
||||
<Icon size={sz} strokeWidth={2} aria-hidden />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
export default function MediaLibraryPage() {
|
||||
const { user } = useAuth()
|
||||
const isPlatformAdmin = user?.role === 'admin' || user?.role === 'superadmin'
|
||||
@@ -222,6 +253,11 @@ export default function MediaLibraryPage() {
|
||||
const [filterClubId, setFilterClubId] = useState('')
|
||||
const [filterUploaderId, setFilterUploaderId] = useState('')
|
||||
const [uploaderFilterOptions, setUploaderFilterOptions] = useState([])
|
||||
const bulkFileInputRef = useRef(null)
|
||||
const [uploadVis, setUploadVis] = useState('private')
|
||||
const [uploadClubId, setUploadClubId] = useState('')
|
||||
const [uploadBusy, setUploadBusy] = useState(false)
|
||||
const [uploadSummary, setUploadSummary] = useState('')
|
||||
|
||||
const loadClubs = useCallback(async () => {
|
||||
try {
|
||||
@@ -428,6 +464,33 @@ export default function MediaLibraryPage() {
|
||||
|
||||
const selCount = selected.size
|
||||
|
||||
const onBulkArchiveFiles = async (e) => {
|
||||
const fl = e.target.files
|
||||
if (!fl?.length) return
|
||||
const list = Array.from(fl)
|
||||
e.target.value = ''
|
||||
if (uploadVis === 'club' && !Number(uploadClubId)) {
|
||||
window.alert('Bitte einen Verein für die Sichtbarkeit „Verein“ wählen.')
|
||||
return
|
||||
}
|
||||
setUploadBusy(true)
|
||||
setUploadSummary('')
|
||||
try {
|
||||
const res = await api.bulkUploadMediaAssets(list, {
|
||||
visibility: uploadVis,
|
||||
...(uploadVis === 'club' ? { club_id: Number(uploadClubId) } : {}),
|
||||
})
|
||||
setUploadSummary(
|
||||
`Archiv-Upload: neu ${res.created_count}, bereits vorhanden ${res.duplicate_count}, fehlgeschlagen ${res.failed_count}.`,
|
||||
)
|
||||
await loadItems()
|
||||
} catch (err) {
|
||||
window.alert(err.message || String(err))
|
||||
} finally {
|
||||
setUploadBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="app-page media-library">
|
||||
{isPlatformAdmin ? <AdminPageNav /> : null}
|
||||
@@ -541,6 +604,61 @@ export default function MediaLibraryPage() {
|
||||
</select>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="media-library__upload-row" aria-label="Archiv hochladen">
|
||||
<input
|
||||
ref={bulkFileInputRef}
|
||||
type="file"
|
||||
className="media-library__sr-file"
|
||||
accept="image/jpeg,image/png,image/gif,video/mp4,application/pdf,.jpg,.jpeg,.png,.gif,.mp4,.pdf"
|
||||
multiple
|
||||
onChange={onBulkArchiveFiles}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
disabled={uploadBusy}
|
||||
onClick={() => bulkFileInputRef.current?.click()}
|
||||
title="Mehrere Dateien ins Archiv laden"
|
||||
>
|
||||
<Upload size={18} aria-hidden className="media-library__upload-icon" />
|
||||
Archiv-Upload
|
||||
</button>
|
||||
<select
|
||||
className="form-input"
|
||||
value={uploadVis}
|
||||
onChange={(e) => {
|
||||
setUploadVis(e.target.value)
|
||||
setUploadSummary('')
|
||||
}}
|
||||
aria-label="Sichtbarkeit für neuen Upload"
|
||||
>
|
||||
{VIS_OPTIONS.map((o) => (
|
||||
<option key={o.value} value={o.value}>
|
||||
Upload: {o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{uploadVis === 'club' ? (
|
||||
<select
|
||||
className="form-input"
|
||||
value={uploadClubId}
|
||||
onChange={(e) => setUploadClubId(e.target.value)}
|
||||
aria-label="Verein für Upload"
|
||||
>
|
||||
<option value="">Verein wählen…</option>
|
||||
{clubs.map((c) => (
|
||||
<option key={c.id} value={String(c.id)}>
|
||||
{c.name || c.id}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
) : null}
|
||||
{uploadSummary ? (
|
||||
<span className="media-library__upload-summary" role="status">
|
||||
{uploadSummary}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="media-library__toolbar-meta">
|
||||
<label className="media-library__check-all">
|
||||
<input type="checkbox" checked={items.length > 0 && selCount === items.length} onChange={selectAll} />
|
||||
@@ -591,6 +709,7 @@ export default function MediaLibraryPage() {
|
||||
title="Vorschau"
|
||||
>
|
||||
<div className="media-library__card-thumb-wrap">
|
||||
<MediaTypeGlyph mimeType={it.mime_type} />
|
||||
<MediaThumb mediaId={it.id} mimeType={it.mime_type} />
|
||||
{(it.copyright_notice || '').trim() ? (
|
||||
<span
|
||||
@@ -658,6 +777,7 @@ export default function MediaLibraryPage() {
|
||||
title="Vorschau"
|
||||
>
|
||||
<div className="media-library__table-thumb">
|
||||
<MediaTypeGlyph mimeType={it.mime_type} compact />
|
||||
<MediaThumb mediaId={it.id} mimeType={it.mime_type} />
|
||||
</div>
|
||||
</button>
|
||||
|
||||
@@ -564,6 +564,49 @@ export async function bulkPatchMediaAssets(data) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Mehrere Dateien ins Medienarchiv (`POST /api/media-assets/bulk-upload`).
|
||||
* @param {File[]} files
|
||||
* @param {{ visibility?: string, club_id?: number }} [options]
|
||||
*/
|
||||
export async function bulkUploadMediaAssets(files, options = {}) {
|
||||
const visibility = options.visibility || 'private'
|
||||
const token = localStorage.getItem('authToken')
|
||||
const headers = mergeActiveClubHeader({})
|
||||
if (token) headers['X-Auth-Token'] = token
|
||||
const formData = new FormData()
|
||||
formData.append('visibility', String(visibility))
|
||||
if (options.club_id != null && options.club_id !== '') {
|
||||
formData.append('club_id', String(options.club_id))
|
||||
}
|
||||
const arr = Array.isArray(files) ? files : [files]
|
||||
for (const f of arr) {
|
||||
if (f) formData.append('files', f)
|
||||
}
|
||||
const url = `${API_URL}/api/media-assets/bulk-upload`
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
})
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
let parsed = null
|
||||
try {
|
||||
parsed = JSON.parse(text)
|
||||
} catch {
|
||||
parsed = null
|
||||
}
|
||||
if (parsed?.detail != null) {
|
||||
const d = parsed.detail
|
||||
throw new Error(typeof d === 'string' ? d : JSON.stringify(d))
|
||||
}
|
||||
const snippet = (text || '').replace(/\s+/g, ' ').trim().slice(0, 180)
|
||||
throw new Error(snippet ? `HTTP ${response.status}: ${snippet}` : `HTTP ${response.status}`)
|
||||
}
|
||||
return response.json()
|
||||
}
|
||||
|
||||
/** Übung: bestehendes Archiv-Medium verknüpfen (`POST /api/exercises/{id}/media/from-asset`). */
|
||||
export async function attachExerciseMediaFromAsset(exerciseId, body) {
|
||||
return request(`/api/exercises/${exerciseId}/media/from-asset`, {
|
||||
@@ -1313,6 +1356,7 @@ export const api = {
|
||||
patchMediaAsset,
|
||||
bulkMediaLifecycle,
|
||||
bulkPatchMediaAssets,
|
||||
bulkUploadMediaAssets,
|
||||
attachExerciseMediaFromAsset,
|
||||
listExerciseProgressionGraphs,
|
||||
getExerciseProgressionGraph,
|
||||
|
||||
Reference in new issue
Block a user