mirror of
https://github.com/timvisee/send.git
synced 2025-10-05 18:34:18 +02:00
refactored storage, style tweaks
This commit is contained in:
parent
8fb770a4ea
commit
55df061567
8 changed files with 42 additions and 30 deletions
|
@ -32,27 +32,31 @@ class DB {
|
|||
return Math.ceil(result) * 1000;
|
||||
}
|
||||
|
||||
async getPrefixedId(id) {
|
||||
async getPrefixedInfo(id) {
|
||||
const [prefix, dead, flagged] = await this.redis.hmgetAsync(
|
||||
id,
|
||||
'prefix',
|
||||
'dead',
|
||||
'flagged'
|
||||
);
|
||||
if (dead || flagged) {
|
||||
throw new Error('id not available');
|
||||
}
|
||||
return `${prefix}-${id}`;
|
||||
return {
|
||||
filePath: `${prefix}-${id}`,
|
||||
flagged,
|
||||
dead
|
||||
};
|
||||
}
|
||||
|
||||
async length(id) {
|
||||
const filePath = await this.getPrefixedId(id);
|
||||
const { filePath } = await this.getPrefixedInfo(id);
|
||||
return this.storage.length(filePath);
|
||||
}
|
||||
|
||||
async get(id) {
|
||||
const filePath = await this.getPrefixedId(id);
|
||||
return this.storage.getStream(filePath);
|
||||
const info = await this.getPrefixedInfo(id);
|
||||
if (info.dead || info.flagged) {
|
||||
throw new Error(info.flagged ? 'flagged' : 'dead');
|
||||
}
|
||||
return this.storage.getStream(info.filePath);
|
||||
}
|
||||
|
||||
async set(id, file, meta, expireSeconds = config.default_expire_seconds) {
|
||||
|
@ -75,18 +79,19 @@ class DB {
|
|||
this.redis.hincrby(id, key, increment);
|
||||
}
|
||||
|
||||
kill(id) {
|
||||
async kill(id) {
|
||||
const { filePath } = await this.getPrefixedInfo(id);
|
||||
this.storage.del(filePath);
|
||||
this.redis.hset(id, 'dead', 1);
|
||||
}
|
||||
|
||||
async flag(id, key) {
|
||||
// this.redis.persist(id);
|
||||
await this.kill(id);
|
||||
this.redis.hmset(id, { flagged: 1, key });
|
||||
this.redis.sadd('flagged', id);
|
||||
}
|
||||
|
||||
async del(id) {
|
||||
const filePath = await this.getPrefixedId(id);
|
||||
const { filePath } = await this.getPrefixedInfo(id);
|
||||
this.storage.del(filePath);
|
||||
this.redis.del(id);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue