チャット・掲示板(スレッド/コメント)へファイル/画像添付を追加。 - lib/db/migrations/002_attachments.sql: attachments テーブル + file_assets.source 列 - repositories/AttachmentRepository + services/AttachmentService: 添付紐付け(プロジェクト/アップロード者チェック) - FileStorageService.uploadForAttachment: 通知/SSE/アクティビティを行わない添付専用アップロード(source='attachment') - ChatService/BoardService: fileIds 受理・添付紐付け・履歴/詳細表示・削除クリーンアップ(トランザクション) - API: POST /attachments + chat/board の fileIds 受理 - UI: AttachmentList(画像サムネイル+Lightbox/ダウンロード) + AttachmentPicker(複数アップロード・送信中は送信不可) をチャット/スレッド/コメントに統合 - 添付ファイルは既存の /api/files/[fileId]/download(権限チェック済)で配信
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getCurrentUser } from '@/lib/auth/getCurrentUser';
|
|
import { createFileStorageService } from '@/lib/api/services';
|
|
import { UnauthorizedError, ValidationError } from '@/lib/errors';
|
|
import { handleApiError } from '@/lib/api/handleError';
|
|
|
|
export const runtime = 'nodejs';
|
|
|
|
/**
|
|
* チャット/掲示板添付用のファイルアップロード。
|
|
* Files一覧公開用の通知/SSE/アクティビティを行わず、source='attachment'で保存する。
|
|
*/
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ projectId: string }> }
|
|
) {
|
|
const user = await getCurrentUser();
|
|
if (!user) return handleApiError(new UnauthorizedError());
|
|
const { projectId } = await params;
|
|
|
|
const formData = await request.formData();
|
|
const file = formData.get('file');
|
|
if (!(file instanceof File)) {
|
|
return handleApiError(
|
|
new ValidationError('ファイルを指定してください', 'file')
|
|
);
|
|
}
|
|
|
|
const data = Buffer.from(await file.arrayBuffer());
|
|
const service = createFileStorageService();
|
|
try {
|
|
const fileAsset = service.uploadForAttachment(user.id, Number(projectId), {
|
|
originalName: file.name,
|
|
mimeType: file.type || 'application/octet-stream',
|
|
data,
|
|
});
|
|
return NextResponse.json({ file: fileAsset }, { status: 201 });
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|