Skip to content

Commit b93b7cb

Browse files
committed
Fix up deletions
1 parent 3c8cea9 commit b93b7cb

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

server/db.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export const DbLinks = {
7171
db.query(`UPDATE shlink set active=false where id=?`, [shl.id]);
7272
return true;
7373
},
74+
linkExists(linkId: string): boolean {
75+
return Boolean(db.query(`SELECT * from shlink where id=?`, [linkId]));
76+
},
7477
getManagedShl(linkId: string, managementToken: string): types.HealthLink {
7578
const linkRow = db
7679
.prepareQuery(`SELECT * from shlink where id=? and management_token=?`)
@@ -119,15 +122,14 @@ export const DbLinks = {
119122

120123
return hashEncoded;
121124
},
122-
async deleteFile(linkId: string, file: types.HealthLinkFile): Promise<string> {
123-
const hash = await crypto.subtle.digest('SHA-256', file.content);
125+
async deleteFile(linkId: string, content: Uint8Array) {
126+
const hash = await crypto.subtle.digest('SHA-256', content);
124127
const hashEncoded = base64url.encode(hash);
125128

126129
db.query(
127-
`delete from shlink_file where shlink = :linkId and content_type = :content and content_hash = :hashEncoded`,
130+
`delete from shlink_file where shlink = :linkId and content_hash = :hashEncoded`,
128131
{
129132
linkId,
130-
content: file.content,
131133
hashEncoded,
132134
}
133135
);
@@ -137,6 +139,8 @@ export const DbLinks = {
137139
// hashEncoded,
138140
// content: file.content,
139141
// });
142+
143+
return true;
140144
},
141145
async addEndpoint(linkId: string, endpoint: types.HealthLinkEndpoint): Promise<string> {
142146
const id = randomStringWithEntropy(32);

server/routers/api.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,7 @@ export const shlApiRouter = new oak.Router()
161161
throw new Error(`Can't manage SHLink ` + context.params.shlId);
162162
}
163163

164-
const fileToRemove = {
165-
contentType: context.request.headers.get('content-type')!,
166-
content: await currentFileBody.value,
167-
}
168-
169-
const deleted = db.DbLinks.deleteFile(shl.id, fileToRemove);
164+
const deleted = db.DbLinks.deleteFile(shl.id, await currentFileBody.value);
170165
context.response.body = {
171166
...shl,
172167
deleted,
@@ -190,12 +185,16 @@ export const shlApiRouter = new oak.Router()
190185
})
191186
.delete('/shl/:shlId', async (context) => {
192187
const managementToken = await context.request.headers.get('authorization')?.split(/bearer /i)[1]!;
193-
const shl = db.DbLinks.getManagedShl(context.params.shlId, managementToken)!;
194-
if (!shl) {
195-
throw new Error(`Can't manage SHLink ` + context.params.shlId);
188+
if (db.DbLinks.linkExists(context.params.shlId)) {
189+
const shl = db.DbLinks.getManagedShl(context.params.shlId, managementToken)!;
190+
if (!shl) {
191+
return (context.response.status = 401);
192+
}
193+
const deactivated = db.DbLinks.deactivate(shl);
194+
context.response.body = deactivated;
195+
} else {
196+
return (context.response.status = 404);
196197
}
197-
const deactivated = db.DbLinks.deactivate(shl);
198-
context.response.body = deactivated;
199198
})
200199
.post('/subscribe', async (context) => {
201200
const shlSet: { shlId: string; managementToken: string }[] = await context.request.body({ type: 'json' }).value;

0 commit comments

Comments
 (0)