Skip to content

Commit ac61964

Browse files
committed
fix: new view counting method
1 parent 1924c22 commit ac61964

File tree

2 files changed

+149
-266
lines changed

2 files changed

+149
-266
lines changed

src/server/routes/files.dy.ts

Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { parseRange } from '@/lib/api/range';
2-
import { config } from '@/lib/config';
3-
import { verifyPassword } from '@/lib/crypto';
4-
import { datasource } from '@/lib/datasource';
51
import { prisma } from '@/lib/db';
6-
import { log } from '@/lib/logger';
72
import { FastifyReply, FastifyRequest } from 'fastify';
3+
import { rawFileHandler } from './raw/[id]';
84

95
type Params = {
106
id: string;
@@ -15,14 +11,11 @@ type Query = {
1511
download?: string;
1612
};
1713

18-
const logger = log('routes').c('files');
19-
2014
export async function filesRoute(
2115
req: FastifyRequest<{ Params: Params; Querystring: Query }>,
2216
res: FastifyReply,
2317
) {
2418
const { id } = req.params;
25-
const { pw, download } = req.query;
2619
const file = await prisma.file.findFirst({
2720
where: {
2821
name: decodeURIComponent(id),
@@ -33,120 +26,8 @@ export async function filesRoute(
3326
});
3427
if (!file) return res.callNotFound();
3528

36-
if (file.deletesAt && file.deletesAt <= new Date()) {
37-
try {
38-
await datasource.delete(file.name);
39-
await prisma.file.delete({
40-
where: {
41-
id: file.id,
42-
},
43-
});
44-
} catch (e) {
45-
logger
46-
.error('failed to delete file on expiration', {
47-
id: file.id,
48-
})
49-
.error(e as Error);
50-
}
51-
return res.callNotFound();
52-
}
53-
if (file.maxViews && file.views >= file.maxViews) {
54-
if (!config.features.deleteOnMaxViews) return res.callNotFound();
55-
try {
56-
await datasource.delete(file.name);
57-
await prisma.file.delete({
58-
where: {
59-
id: file.id,
60-
},
61-
});
62-
} catch (e) {
63-
logger
64-
.error('failed to delete file on max views', {
65-
id: file.id,
66-
})
67-
.error(e as Error);
68-
}
69-
return res.callNotFound();
70-
}
7129
if (file.User?.view.enabled) return res.redirect(`/view/${encodeURIComponent(file.name)}`);
7230
if (file.type.startsWith('text/')) return res.redirect(`/view/${encodeURIComponent(file.name)}`);
73-
const stream = await datasource.get(file.name);
74-
if (!stream) return res.callNotFound();
75-
if (file.password) {
76-
if (!pw) return res.redirect(`/view/${encodeURIComponent(file.name)}`);
77-
const verified = await verifyPassword(pw as string, file.password!);
78-
if (!verified) {
79-
logger.warn('password protected file accessed with an incorrect password', { id: file.id, ip: req.ip });
80-
return res.callNotFound();
81-
}
82-
}
83-
if (!req.headers.range) {
84-
await prisma.file.update({
85-
where: {
86-
id: file.id,
87-
},
88-
data: {
89-
views: {
90-
increment: 1,
91-
},
92-
},
93-
});
94-
}
95-
const size = file?.size || (await datasource.size(file?.name ?? id));
96-
if (req.headers.range) {
97-
const [start, end] = parseRange(req.headers.range, size);
98-
if (start >= size || end >= size) {
99-
const buf = await datasource.get(file?.name ?? id);
100-
if (!buf) return res.callNotFound();
101-
return res
102-
.type(file?.type || 'application/octet-stream')
103-
.headers({
104-
'Content-Length': size,
105-
...(file?.originalName
106-
? {
107-
'Content-Disposition': `${download ? 'attachment; ' : ''}filename="${encodeURIComponent(file.originalName)}"`,
108-
}
109-
: download && {
110-
'Content-Disposition': 'attachment;',
111-
}),
112-
})
113-
.status(416)
114-
.send(buf);
115-
}
116-
const buf = await datasource.range(file?.name ?? id, start || 0, end);
117-
if (!buf) return res.callNotFound();
118-
return res
119-
.type(file?.type || 'application/octet-stream')
120-
.headers({
121-
'Content-Range': `bytes ${start}-${end}/${size}`,
122-
'Accept-Ranges': 'bytes',
123-
'Content-Length': end - start + 1,
124-
...(file?.originalName
125-
? {
126-
'Content-Disposition': `${download ? 'attachment; ' : ''}filename="${encodeURIComponent(file.originalName)}"`,
127-
}
128-
: download && {
129-
'Content-Disposition': 'attachment;',
130-
}),
131-
})
132-
.status(206)
133-
.send(buf);
134-
}
135-
const buf = await datasource.get(file?.name ?? id);
136-
if (!buf) return res.callNotFound();
137-
return res
138-
.type(file?.type || 'application/octet-stream')
139-
.headers({
140-
'Content-Length': size,
141-
'Accept-Ranges': 'bytes',
142-
...(file?.originalName
143-
? {
144-
'Content-Disposition': `${download ? 'attachment; ' : ''}filename="${encodeURIComponent(file.originalName)}"`,
145-
}
146-
: download && {
147-
'Content-Disposition': 'attachment;',
148-
}),
149-
})
150-
.status(200)
151-
.send(buf);
31+
32+
return rawFileHandler(req, res);
15233
}

0 commit comments

Comments
 (0)