-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from Longridge-High-School/feat-attachments-to…
…-docs feat: attach files to documents
- Loading branch information
Showing
5 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { | ||
type LoaderFunctionArgs, | ||
type MetaFunction, | ||
json, | ||
type ActionFunctionArgs, | ||
redirect, | ||
unstable_parseMultipartFormData | ||
} from '@remix-run/node' | ||
import {useLoaderData} from '@remix-run/react' | ||
import {basename} from 'path' | ||
import {invariant} from '@arcath/utils' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {pageTitle} from '~/lib/utils/page-title' | ||
import {Input} from '~/lib/components/input' | ||
import {Button} from '~/lib/components/button' | ||
import {getUploadMetaData} from '~/lib/utils/upload-handler.server' | ||
import {getUploadHandler} from '~/lib/utils/upload-handler.server' | ||
import {AButton} from '~/lib/components/button' | ||
|
||
export type Attachment = { | ||
uri: string | ||
originalFileName: string | ||
type: string | ||
} | ||
|
||
export const loader = async ({request, params}: LoaderFunctionArgs) => { | ||
const user = await ensureUser(request, 'document:write', { | ||
documentId: params.document | ||
}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const document = await prisma.document.findFirstOrThrow({ | ||
where: {id: params.document} | ||
}) | ||
|
||
const attachments = ( | ||
JSON.parse(document.attachments) as Array<Attachment> | ||
).filter(v => v !== null) | ||
|
||
const {searchParams} = new URL(request.url) | ||
const del = searchParams.get('delete') | ||
|
||
if (del) { | ||
delete attachments[parseInt(del)] | ||
|
||
await prisma.document.update({ | ||
where: {id: params.document}, | ||
data: {attachments: JSON.stringify(attachments)} | ||
}) | ||
} | ||
|
||
return json({ | ||
user, | ||
document, | ||
attachments: attachments.filter(v => v !== null) | ||
}) | ||
} | ||
|
||
export const meta: MetaFunction<typeof loader> = ({data}) => { | ||
return [{title: pageTitle('Document', data!.document.title, 'Attachments')}] | ||
} | ||
|
||
export const action = async ({request, params}: ActionFunctionArgs) => { | ||
await ensureUser(request, 'document:write', { | ||
documentId: params.document | ||
}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const document = await prisma.document.findFirstOrThrow({ | ||
where: {id: params.document} | ||
}) | ||
|
||
const uploadHandler = getUploadHandler() | ||
|
||
const formData = await unstable_parseMultipartFormData(request, uploadHandler) | ||
|
||
const file = formData.get('file') as unknown as | ||
| {filepath: string; type: string} | ||
| undefined | ||
|
||
invariant(file) | ||
|
||
const fileName = basename(file.filepath) | ||
|
||
const metaData = getUploadMetaData(fileName) | ||
|
||
const newAttachment: Attachment = { | ||
uri: `/uploads/${fileName}`, | ||
originalFileName: metaData ? metaData.originalFileName : fileName, | ||
type: file.type | ||
} | ||
|
||
const attachments = JSON.parse(document.attachments) as Array<Attachment> | ||
|
||
attachments.push(newAttachment) | ||
|
||
await prisma.document.update({ | ||
where: {id: params.document}, | ||
data: {attachments: JSON.stringify(attachments)} | ||
}) | ||
|
||
return redirect(`/app/documents/${document.id}`) | ||
} | ||
|
||
const AttachToDocumentPage = () => { | ||
const {attachments} = useLoaderData<typeof loader>() | ||
|
||
return ( | ||
<div className="grid grid-cols-4 gap-4"> | ||
<div className="entry col-span-3"> | ||
<h2>Attachments</h2> | ||
<form method="POST" encType="multipart/form-data"> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>URI</th> | ||
<th>File Name</th> | ||
<th>File Type</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{attachments.map(({uri, originalFileName, type}, i) => { | ||
return ( | ||
<tr key={uri}> | ||
<td>{uri}</td> | ||
<td>{originalFileName}</td> | ||
<td>{type}</td> | ||
<td> | ||
<AButton href={`?delete=${i}`}>🗑️</AButton> | ||
</td> | ||
</tr> | ||
) | ||
})} | ||
</tbody> | ||
<tfoot> | ||
<td colSpan={2}> | ||
<Input type="file" name="file" /> | ||
</td> | ||
<td colSpan={2}> | ||
<Button className="bg-success">Upload</Button> | ||
</td> | ||
</tfoot> | ||
</table> | ||
</form> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default AttachToDocumentPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
prisma/migrations/20241013175935_add_attachments_to_document/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- RedefineTables | ||
PRAGMA defer_foreign_keys=ON; | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Document" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"body" TEXT NOT NULL, | ||
"title" TEXT NOT NULL, | ||
"attachments" TEXT NOT NULL DEFAULT '[]', | ||
"aclId" TEXT NOT NULL DEFAULT '', | ||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" DATETIME NOT NULL, | ||
CONSTRAINT "Document_aclId_fkey" FOREIGN KEY ("aclId") REFERENCES "ACL" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
); | ||
INSERT INTO "new_Document" ("aclId", "body", "createdAt", "id", "title", "updatedAt") SELECT "aclId", "body", "createdAt", "id", "title", "updatedAt" FROM "Document"; | ||
DROP TABLE "Document"; | ||
ALTER TABLE "new_Document" RENAME TO "Document"; | ||
PRAGMA foreign_keys=ON; | ||
PRAGMA defer_foreign_keys=OFF; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters