made download count and expiry options server configurable

This commit is contained in:
Danny Coates 2018-09-24 15:08:39 -07:00
parent b61bf3c867
commit e2259ae737
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
6 changed files with 22 additions and 28 deletions

View File

@ -1,3 +1,4 @@
/* globals DEFAULTS */
const html = require('choo/html'); const html = require('choo/html');
const raw = require('choo/html/raw'); const raw = require('choo/html/raw');
const selectbox = require('../selectbox'); const selectbox = require('../selectbox');
@ -17,7 +18,7 @@ module.exports = function(state, emit) {
return el; return el;
} }
const counts = [1, 2, 3, 4, 5, 20, 50, 100, 200].filter( const counts = DEFAULTS.DOWNLOAD_COUNTS.filter(
i => state.capabilities.account || i <= state.user.maxDownloads i => state.capabilities.account || i <= state.user.maxDownloads
); );
@ -40,7 +41,7 @@ module.exports = function(state, emit) {
dlCountSelect dlCountSelect
); );
const expires = [300, 3600, 86400, 604800].filter( const expires = DEFAULTS.EXPIRE_TIMES_SECONDS.filter(
i => state.capabilities.account || i <= state.user.maxExpireSeconds i => state.capabilities.account || i <= state.user.maxExpireSeconds
); );

View File

@ -29,6 +29,7 @@
"test-integration": "docker-compose up --abort-on-container-exit --exit-code-from integration-tests --build --remove-orphans --quiet-pull && docker-compose down", "test-integration": "docker-compose up --abort-on-container-exit --exit-code-from integration-tests --build --remove-orphans --quiet-pull && docker-compose down",
"test-integration-stage": "cross-env BASE_URL=https://send.stage.mozaws.net npm run test-integration", "test-integration-stage": "cross-env BASE_URL=https://send.stage.mozaws.net npm run test-integration",
"start": "npm run clean && cross-env NODE_ENV=development FXA_CLIENT_ID=fced6b5e3f4c66b9 BASE_URL=http://localhost:8080 webpack-dev-server --mode=development", "start": "npm run clean && cross-env NODE_ENV=development FXA_CLIENT_ID=fced6b5e3f4c66b9 BASE_URL=http://localhost:8080 webpack-dev-server --mode=development",
"android": "cross-env ANDROID=1 npm start",
"prod": "node server/bin/prod.js" "prod": "node server/bin/prod.js"
}, },
"lint-staged": { "lint-staged": {

View File

@ -9,16 +9,6 @@ const conf = convict({
default: '', default: '',
env: 'S3_BUCKET' env: 'S3_BUCKET'
}, },
num_of_prefixes: {
format: Number,
default: 5,
env: 'NUM_OF_PREFIXES'
},
expire_prefixes: {
format: Array,
default: ['5minutes', '1hour', '1day', '1week', '2weeks'],
env: 'EXPIRE_PREFIXES'
},
expire_times_seconds: { expire_times_seconds: {
format: Array, format: Array,
default: [300, 3600, 86400, 604800], default: [300, 3600, 86400, 604800],
@ -39,6 +29,11 @@ const conf = convict({
default: 86400, default: 86400,
env: 'ANON_MAX_EXPIRE_SECONDS' env: 'ANON_MAX_EXPIRE_SECONDS'
}, },
download_counts: {
format: Array,
default: [1, 2, 3, 4, 5, 20, 50, 100, 200],
env: 'DOWNLOAD_COUNTS'
},
max_downloads: { max_downloads: {
format: Number, format: Number,
default: 200, default: 200,

View File

@ -55,6 +55,8 @@ module.exports = async function(req, res) {
MAX_ARCHIVES_PER_USER: ${config.max_archives_per_user} MAX_ARCHIVES_PER_USER: ${config.max_archives_per_user}
}; };
var DEFAULTS = { var DEFAULTS = {
DOWNLOAD_COUNTS: ${JSON.stringify(config.download_counts)},
EXPIRE_TIMES_SECONDS: ${JSON.stringify(config.expire_times_seconds)},
EXPIRE_SECONDS: ${config.default_expire_seconds} EXPIRE_SECONDS: ${config.default_expire_seconds}
}; };
${authConfig}; ${authConfig};

View File

@ -3,6 +3,10 @@ const Metadata = require('../metadata');
const mozlog = require('../log'); const mozlog = require('../log');
const createRedisClient = require('./redis'); const createRedisClient = require('./redis');
function getPrefix(seconds) {
return Math.max(Math.floor(seconds / 86400), 1);
}
class DB { class DB {
constructor(config) { constructor(config) {
const Storage = config.s3_bucket ? require('./s3') : require('./fs'); const Storage = config.s3_bucket ? require('./s3') : require('./fs');
@ -37,14 +41,7 @@ class DB {
} }
async set(id, file, meta, expireSeconds = config.default_expire_seconds) { async set(id, file, meta, expireSeconds = config.default_expire_seconds) {
const expireTimes = config.expire_times_seconds; const prefix = getPrefix(expireSeconds);
let i;
for (i = 0; i < expireTimes.length - 1; i++) {
if (expireSeconds <= expireTimes[i]) {
break;
}
}
const prefix = config.expire_prefixes[i];
const filePath = `${prefix}-${id}`; const filePath = `${prefix}-${id}`;
await this.storage.set(filePath, file); await this.storage.set(filePath, file);
this.redis.hset(id, 'prefix', prefix); this.redis.hset(id, 'prefix', prefix);

View File

@ -23,8 +23,6 @@ class MockStorage {
const config = { const config = {
s3_bucket: 'foo', s3_bucket: 'foo',
default_expire_seconds: 20, default_expire_seconds: 20,
num_of_prefixes: 3,
expire_prefixes: ['ten', 'twenty', 'thirty'],
expire_times_seconds: [10, 20, 30], expire_times_seconds: [10, 20, 30],
env: 'development', env: 'development',
redis_host: 'localhost' redis_host: 'localhost'
@ -71,19 +69,19 @@ describe('Storage', function() {
}); });
it('adds right prefix based on expire time', async function() { it('adds right prefix based on expire time', async function() {
await storage.set('x', null, { foo: 'bar' }, 10); await storage.set('x', null, { foo: 'bar' }, 300);
const path_x = await storage.getPrefixedId('x'); const path_x = await storage.getPrefixedId('x');
assert.equal(path_x, 'ten-x'); assert.equal(path_x, '1-x');
await storage.del('x'); await storage.del('x');
await storage.set('y', null, { foo: 'bar' }, 11); await storage.set('y', null, { foo: 'bar' }, 86400);
const path_y = await storage.getPrefixedId('y'); const path_y = await storage.getPrefixedId('y');
assert.equal(path_y, 'twenty-y'); assert.equal(path_y, '1-y');
await storage.del('y'); await storage.del('y');
await storage.set('z', null, { foo: 'bar' }, 33); await storage.set('z', null, { foo: 'bar' }, 86400 * 7);
const path_z = await storage.getPrefixedId('z'); const path_z = await storage.getPrefixedId('z');
assert.equal(path_z, 'thirty-z'); assert.equal(path_z, '7-z');
await storage.del('z'); await storage.del('z');
}); });