feat(m10): file sharing with upload, lightbox, permission, tests, e2e

- FileRepository (CRUD, soft-delete, pagination, project isolation) +
  FileStorageService (local FS save data/uploads/<projectId>/<uuid>.<ext>,
  MIME allowlist, filename sanitize, unique uuid name, member permission,
  file_shared notification to other members, file_uploaded activity log,
  SSE file.uploaded, uploader/admin delete + FS unlink)
- APIs: files list/upload(multipart), download (membership), delete
- Screens: files page + Uploader(multipart) + FileList(image Lightbox,
  PDF link, delete), ProjectNav ファイル link
- Unit tests (FileRepository, FileStorageService) + e2e file-sharing
This commit is contained in:
Ken Yasue
2026-06-25 02:02:39 +02:00
parent 1425773cd4
commit 60fef5c0c9
13 changed files with 945 additions and 0 deletions

View File

@ -0,0 +1,107 @@
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import type { FileAsset } from '@/lib/types';
export function FileList({
files,
canDelete,
}: {
files: FileAsset[];
canDelete: boolean;
}) {
const router = useRouter();
const [lightbox, setLightbox] = useState<FileAsset | null>(null);
async function onDelete(fileId: number) {
const res = await fetch(`/api/files/${fileId}`, { method: 'DELETE' });
if (res.ok) router.refresh();
}
if (files.length === 0) {
return <p className="text-sm text-gray-400"></p>;
}
return (
<>
<ul
className="grid grid-cols-2 gap-3 sm:grid-cols-3"
data-testid="file-list"
>
{files.map((file) => {
const isImage = file.mimeType.startsWith('image/');
const isPdf = file.mimeType === 'application/pdf';
const url = `/api/files/${file.id}/download`;
return (
<li
key={file.id}
className="rounded-lg border bg-white p-3 shadow-sm"
data-testid={`file-item-${file.id}`}
>
{isImage ? (
<button
type="button"
onClick={() => setLightbox(file)}
className="block w-full"
>
<img
src={url}
alt={file.originalName}
className="h-24 w-full rounded object-cover"
/>
</button>
) : isPdf ? (
<a
href={url}
target="_blank"
rel="noreferrer"
className="flex h-24 items-center justify-center rounded bg-gray-100 text-sm text-blue-600 hover:underline"
>
PDF
</a>
) : (
<a
href={url}
className="flex h-24 items-center justify-center rounded bg-gray-100 text-sm text-blue-600 hover:underline"
>
</a>
)}
<p
className="mt-2 truncate text-xs text-gray-700"
title={file.originalName}
>
{file.originalName}
</p>
<p className="text-xs text-gray-400">{file.size} bytes</p>
{canDelete && (
<button
type="button"
onClick={() => onDelete(file.id)}
className="mt-1 text-xs text-red-600 hover:underline"
>
</button>
)}
</li>
);
})}
</ul>
{lightbox && (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 p-4"
onClick={() => setLightbox(null)}
data-testid="lightbox"
>
<img
src={`/api/files/${lightbox.id}/download`}
alt={lightbox.originalName}
className="max-h-full max-w-full rounded"
/>
</div>
)}
</>
);
}

View File

@ -0,0 +1,58 @@
'use client';
import { useRef, useState } from 'react';
import { useRouter } from 'next/navigation';
export function Uploader({ projectId }: { projectId: number }) {
const router = useRouter();
const inputRef = useRef<HTMLInputElement>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
async function onFile(event: React.ChangeEvent<HTMLInputElement>) {
const file = event.target.files?.[0];
if (!file) return;
setLoading(true);
setError(null);
const form = new FormData();
form.append('file', file);
const res = await fetch(`/api/projects/${projectId}/files`, {
method: 'POST',
body: form,
});
setLoading(false);
if (res.ok) {
if (inputRef.current) inputRef.current.value = '';
router.refresh();
} else {
const b = (await res.json().catch(() => null)) as {
error?: { message?: string };
} | null;
setError(b?.error?.message ?? 'アップロードに失敗しました');
}
}
return (
<div className="rounded-lg border bg-white p-4 shadow-sm">
<label className="block text-sm font-medium">
</label>
<input
ref={inputRef}
type="file"
onChange={onFile}
disabled={loading}
className="mt-2 block text-sm"
data-testid="file-input"
/>
{loading && (
<p className="mt-1 text-sm text-gray-500">...</p>
)}
{error && (
<p className="mt-1 text-sm text-red-600" role="alert">
{error}
</p>
)}
</div>
);
}

View File

@ -4,6 +4,7 @@ const NAV_ITEMS = [
{ href: '/notes', label: 'メモ' },
{ href: '/chat', label: 'チャット' },
{ href: '/todos', label: 'ToDo' },
{ href: '/files', label: 'ファイル' },
{ href: '/members', label: 'メンバー' },
{ href: '/activity', label: 'アクティビティ' },
{ href: '/settings', label: '設定' },
@ -24,6 +25,7 @@ export function ProjectNav({
| 'notes'
| 'chat'
| 'todos'
| 'files'
| 'members'
| 'activity'
| 'settings';
@ -34,6 +36,7 @@ export function ProjectNav({
notes: active === 'notes',
chat: active === 'chat',
todos: active === 'todos',
files: active === 'files',
members: active === 'members',
activity: active === 'activity',
settings: active === 'settings',