-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee66061
commit 4e6b8a2
Showing
20 changed files
with
837 additions
and
230 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,35 @@ | ||
import { describe, expect, test } from "bun:test"; | ||
import makeAttachmentLoader from "./attachment"; | ||
import { encodeGlobalId } from "@/schema/system"; | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: | ||
const makeLoader = () => makeAttachmentLoader({} as any); | ||
|
||
process.env.ATTACHMENT_BUCKET = "tendrel-ruggiano-test-attachment-bucket"; | ||
|
||
describe.skipIf(!!process.env.CI)("attachment loader", () => { | ||
test("ok", async () => { | ||
const data = await makeLoader().byId.load( | ||
encodeGlobalId({ | ||
type: "workpictureinstance", | ||
id: "ace41781-58df-452b-8525-d7f1c8130586", | ||
}), | ||
); | ||
expect(data).toMatchObject({ | ||
id: expect.any(String), | ||
attachment: expect.stringMatching( | ||
/^https:\/\/tendrel-ruggiano-test-attachment-bucket/, | ||
), | ||
}); | ||
}); | ||
|
||
test("not found", async () => { | ||
const p = makeLoader().byId.load( | ||
encodeGlobalId({ | ||
type: "__test__", | ||
id: "1", | ||
}), | ||
); | ||
expect(p).rejects.toThrow("No Attachment for id '1'"); | ||
}); | ||
}); |
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,53 @@ | ||
import type { Attachment } from "@/schema"; | ||
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import Dataloader from "dataloader"; | ||
import type { Request } from "express"; | ||
import { sql } from "./postgres"; | ||
import { decodeGlobalId } from "@/schema/system"; | ||
import { GraphQLError } from "graphql"; | ||
|
||
async function createPresignedUrl(client: S3Client, uri: URL) { | ||
const command = new GetObjectCommand({ | ||
Bucket: uri.host, | ||
Key: uri.pathname.slice(1), // remove the leading '/' | ||
}); | ||
if (uri.protocol !== "s3:") { | ||
throw "invariant violated"; | ||
} | ||
return getSignedUrl(client, command, { | ||
expiresIn: Number(process.env.ATTACHMENT_EXPIRATION_TIME_SECONDS ?? 3600), | ||
}); | ||
} | ||
|
||
export default (_: Request) => ({ | ||
byId: new Dataloader<string, Attachment>(async keys => { | ||
const pks = keys.map(k => decodeGlobalId(k).id); | ||
const rows = await sql<{ _key: string; uri: string }[]>` | ||
SELECT | ||
workpictureinstanceuuid AS _key, | ||
workpictureinstancestoragelocation AS uri | ||
FROM public.workpictureinstance | ||
WHERE workpictureinstanceuuid IN ${sql(pks)}; | ||
`; | ||
const s3 = new S3Client(); | ||
return Promise.all( | ||
pks.map(async (pk, i) => { | ||
const row = rows.find(r => r._key === pk); | ||
|
||
if (!row) { | ||
throw new GraphQLError(`No Attachment for id '${pk}'`, { | ||
extensions: { | ||
code: "NOT_FOUND", | ||
}, | ||
}); | ||
} | ||
|
||
return { | ||
id: keys[i], | ||
attachment: await createPresignedUrl(s3, new URL(row.uri)), | ||
}; | ||
}), | ||
); | ||
}), | ||
}); |
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
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
Oops, something went wrong.