Skip to content

Commit

Permalink
refactored storage, style tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Jul 25, 2020
1 parent 8fb770a commit 55df061
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 30 deletions.
5 changes: 4 additions & 1 deletion app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ module.exports = function(app = choo({ hash: true })) {
app.route('/oauth', function(state, emit) {
emit('authenticate', state.query.code, state.query.state);
});
app.route('/login', body(require('./ui/home')));
app.route('/login', function(state, emit) {
emit('replaceState', '/');
setTimeout(() => emit('render'));
});
app.route('/report', body(require('./ui/report')));
app.route('*', body(require('./ui/notFound')));
return app;
Expand Down
6 changes: 6 additions & 0 deletions app/ui/archiveTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ function password(state) {

return html`
<div class="mb-2 px-1">
<input
id="autocomplete-decoy"
class="hidden"
type="password"
value="lol"
/>
<div class="checkbox inline-block mr-3">
<input
id="add-password"
Expand Down
2 changes: 1 addition & 1 deletion app/ui/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Header extends Component {
alt="${this.state.translate('title')}"
src="${assets.get('icon.svg')}"
/>
<svg class="w-48 md:w-64">
<svg viewBox="66 0 340 64" class="w-48 md:w-64">
<use xlink:href="${assets.get('wordmark.svg')}#logo" />
</svg>
</a>
Expand Down
14 changes: 5 additions & 9 deletions app/ui/signupDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ module.exports = function(trigger) {
let submitting = false;
return html`
<send-signup-dialog
class="flex flex-col lg:flex-row justify-center my-16 md:my-0 px-8 md:px-24 w-full h-full"
class="flex flex-col justify-center my-16 md:my-0 px-8 md:px-24 w-full h-full"
>
<img src="${assets.get('master-logo.svg')}" class="h-16 mt-1 mb-4" />
<section
class="flex flex-col flex-shrink-0 self-center lg:mx-6 lg:max-w-xs"
>
<h1 class="text-3xl font-bold text-center lg:text-left">
<section class="flex flex-col flex-shrink-0 self-center">
<h1 class="text-3xl font-bold text-center">
${state.translate('accountBenefitTitle')}
</h1>
<ul
Expand All @@ -33,14 +31,12 @@ module.exports = function(trigger) {
<li>${state.translate('accountBenefitSync')}</li>
</ul>
</section>
<section
class="flex flex-col flex-grow m-4 md:self-center md:w-128 lg:max-w-xs"
>
<section class="flex flex-col flex-grow m-4 md:self-center md:w-128">
<form onsubmit=${submitEmail} data-no-csrf>
<input
id="email-input"
type="email"
class="hidden lg:block border rounded-lg w-full px-2 py-1 h-12 mb-3 text-lg text-grey-70 leading-loose dark:bg-grey-80 dark:text-white"
class="hidden border rounded-lg w-full px-2 py-1 h-12 mb-3 text-lg text-grey-70 leading-loose dark:bg-grey-80 dark:text-white"
placeholder=${state.translate('emailPlaceholder')}
/>
<input
Expand Down
2 changes: 1 addition & 1 deletion assets/wordmark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions server/routes/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module.exports = async function(req, res) {
const id = req.params.id;
try {
const meta = req.meta;
if (meta.dead || meta.flagged) {
return res.sendStatus(404);
}
const contentLength = await storage.length(id);
const fileStream = await storage.get(id);
let cancelled = false;

Expand All @@ -18,6 +16,10 @@ module.exports = async function(req, res) {
fileStream.destroy();
});

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': contentLength
});
fileStream.pipe(res).on('finish', async () => {
if (cancelled) {
return;
Expand Down
29 changes: 17 additions & 12 deletions server/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions test/backend/storage-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ describe('Storage', function() {

it('adds right prefix based on expire time', async function() {
await storage.set('x', null, { foo: 'bar' }, 300);
const path_x = await storage.getPrefixedId('x');
const { filePath: path_x } = await storage.getPrefixedInfo('x');
assert.equal(path_x, '1-x');
await storage.del('x');

await storage.set('y', null, { foo: 'bar' }, 86400);
const path_y = await storage.getPrefixedId('y');
const { filePath: path_y } = await storage.getPrefixedInfo('y');
assert.equal(path_y, '1-y');
await storage.del('y');

await storage.set('z', null, { foo: 'bar' }, 86400 * 7);
const path_z = await storage.getPrefixedId('z');
const { filePath: path_z } = await storage.getPrefixedInfo('z');
assert.equal(path_z, '7-z');
await storage.del('z');
});
Expand Down

0 comments on commit 55df061

Please sign in to comment.