fix: Added KV cache, fixed from unix timestamp

This commit is contained in:
florian 2024-03-24 00:20:45 +01:00
parent 98845db598
commit ce119ff84c

View File

@ -124,7 +124,7 @@ app.get('/list/:pubkey', async (c) => {
const listOfBlobs: any[] = []; const listOfBlobs: any[] = [];
await Promise.all( await Promise.all(
keyList.keys.map(async (key) => { keyList.keys.map(async (key) => {
const descriptor = await c.env.KV_BLOSSOM.get(key.name); const descriptor = await c.env.KV_BLOSSOM.get(key.name, { cacheTtl: 3600 });
const [_, blobKey] = key.name.split(':'); const [_, blobKey] = key.name.split(':');
if (descriptor) { if (descriptor) {
@ -156,7 +156,6 @@ function computeContentRange(range: R2Range, size: number) {
} }
app.put('/upload', async (c) => { app.put('/upload', async (c) => {
const auth = getAuth(c.req.header('authorization') as string); const auth = getAuth(c.req.header('authorization') as string);
await checkAuth(auth, 'upload', c.env.ALLOWED_NPUBS, c.env.KV_BLOSSOM); await checkAuth(auth, 'upload', c.env.ALLOWED_NPUBS, c.env.KV_BLOSSOM);
@ -191,7 +190,7 @@ app.put('/upload', async (c) => {
const blobData: BlobData = { const blobData: BlobData = {
size: storedObject.size, size: storedObject.size,
type: storedObject.httpMetadata?.contentType, type: storedObject.httpMetadata?.contentType,
created: new Date(storedObject.uploaded).getTime(), created: dayjs(storedObject.uploaded).unix(),
}; };
await c.env.KV_BLOSSOM.put(pubkey + ':' + hash, JSON.stringify(blobData)); await c.env.KV_BLOSSOM.put(pubkey + ':' + hash, JSON.stringify(blobData));
@ -214,11 +213,11 @@ app.delete('*', async (c) => {
throw new HTTPException(400, { message: 'Invalid path, hash missing' }); throw new HTTPException(400, { message: 'Invalid path, hash missing' });
} }
if (!(await c.env.KV_BLOSSOM.get(pubkey + ':' + hash))) { if (!(await c.env.KV_BLOSSOM.get(pubkey + ':' + hash, { cacheTtl: 3600 }))) {
throw new HTTPException(403, { message: 'Not allowed to delete the blob.' }); throw new HTTPException(403, { message: 'Not allowed to delete the blob.' });
} }
const blobKey = await c.env.KV_BLOSSOM.get('blob:' + hash); const blobKey = await c.env.KV_BLOSSOM.get('blob:' + hash, { cacheTtl: 3600 });
if (!blobKey) { if (!blobKey) {
throw new HTTPException(404, { message: `Blob with the hash ${hash} not found.` }); throw new HTTPException(404, { message: `Blob with the hash ${hash} not found.` });
} }
@ -242,7 +241,7 @@ app.get('*', async (c) => {
if (!hash) { if (!hash) {
throw new HTTPException(400, { message: 'Invalid path, hash missing' }); throw new HTTPException(400, { message: 'Invalid path, hash missing' });
} }
const key = await c.env.KV_BLOSSOM.get('blob:' + hash); const key = await c.env.KV_BLOSSOM.get('blob:' + hash, { cacheTtl: 3600 });
if (!key) { if (!key) {
throw new HTTPException(404, { message: 'Blob hash key not found.' }); throw new HTTPException(404, { message: 'Blob hash key not found.' });
} }