Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from blockstack/hotfix/disk-content-type
Hotfix/disk content type
- Loading branch information
Showing
14 changed files
with
356 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env", "flow"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"plugins": ["flowtype"], | ||
"env": { | ||
"browser": false, | ||
"es6": true, | ||
"node": true, | ||
}, | ||
"rules": { | ||
"comma-dangle": ["error", "never"], | ||
"quotes": [2, "single"], | ||
"eol-last": 2, | ||
"no-debugger": 1, | ||
"no-mixed-requires": 0, | ||
"no-underscore-dangle": 0, | ||
"no-multi-spaces": 0, | ||
"no-trailing-spaces": 0, | ||
"no-extra-boolean-cast": 0, | ||
"no-undef": 2, | ||
"no-unused-vars": 2, | ||
"no-var": 2, | ||
"no-param-reassign": 0, | ||
"no-else-return": 0, | ||
"no-console": 0, | ||
"prefer-const": 2, | ||
"new-cap": 0, | ||
"camelcase": 2, | ||
"brace-style": 2, | ||
"semi": [2, "never"], | ||
"flowtype/define-flow-type": 1, | ||
"valid-jsdoc": ["error"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ignore] | ||
.*/node_modules/documentation/* | ||
.*/node_modules/.staging/* | ||
.*/node_modules/npm/* | ||
|
||
[include] | ||
|
||
[libs] | ||
|
||
[options] | ||
|
||
[lints] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "gaia-reader", | ||
"version": "2.2.2", | ||
"description": "", | ||
"main": "index.js", | ||
"engines": { | ||
"node": "^8" | ||
}, | ||
"dependencies": { | ||
"body-parser": "^1.18.1", | ||
"cors": "^2.8.4", | ||
"express": "^4.15.4", | ||
"express-winston": "^2.4.0", | ||
"node-fetch": "^2.0.0", | ||
"winston": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-eslint": "^8.2.2", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-flow": "^6.23.0", | ||
"eslint": "^4.18.1", | ||
"eslint-plugin-flowtype": "^2.46.3", | ||
"fetch-mock": "^6.4.2", | ||
"flow-bin": "^0.71.0", | ||
"nock": "^9.1.9", | ||
"nyc": "^13.0.0", | ||
"proxyquire": "^2.0.1", | ||
"supertest": "^3.0.0", | ||
"tape": "^4.9.0" | ||
}, | ||
"bin": { | ||
"blockstack-gaia-reader": "./lib/index.js" | ||
}, | ||
"scripts": { | ||
"start": "npm run build && node lib/index.js", | ||
"build": "npm run lint && babel src -d lib && chmod +x lib/index.js", | ||
"flow": "flow", | ||
"lint": "eslint src", | ||
"test-inner": "npm run build && npm run flow && node ./test/test.js", | ||
"test": "nyc --reporter=text npm run test-inner" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/blockstack/gaia.git" | ||
}, | ||
"authors": [ | ||
"Aaron Blankstein (aaron@blockstack.com)", | ||
"Jude Nelson (jude@blockstack.com)" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import winston from 'winston' | ||
import fs from 'fs' | ||
import process from 'process' | ||
|
||
const configDefaults = { | ||
argsTransport: { | ||
level: 'debug', | ||
handleExceptions: true, | ||
timestamp: true, | ||
stringify: true, | ||
colorize: true, | ||
json: true | ||
}, | ||
regtest: false, | ||
testnet: false, | ||
port: 8008, | ||
diskSettings: { | ||
storageRootDirectory: '/tmp/gaia-disk' | ||
} | ||
} | ||
|
||
export function getConfig() { | ||
const configPath = process.env.CONFIG_PATH || process.argv[2] || './config.json' | ||
let config | ||
try { | ||
config = Object.assign( | ||
{}, configDefaults, JSON.parse(fs.readFileSync(configPath).toString())) | ||
} catch (e) { | ||
config = Object.assign({}, configDefaults) | ||
} | ||
|
||
winston.configure({ transports: [ | ||
new winston.transports.Console(config.argsTransport) ] }) | ||
|
||
return config | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* @flow */ | ||
|
||
import express from 'express' | ||
import expressWinston from 'express-winston' | ||
import logger from 'winston' | ||
import cors from 'cors' | ||
import Path from 'path' | ||
|
||
import { | ||
GaiaDiskReader | ||
} from './server' | ||
|
||
export function makeHttpServer(config: Object) { | ||
const app = express() | ||
const server = new GaiaDiskReader(config) | ||
|
||
app.config = config | ||
|
||
app.use(expressWinston.logger({ | ||
transports: logger.loggers.default.transports })) | ||
|
||
app.use(cors()) | ||
|
||
app.get(/\/([a-zA-Z0-9]+)\/(.+)/, (req: express.request, res: express.response) => { | ||
let filename = req.params[1] | ||
if (filename.endsWith('/')) { | ||
filename = filename.substring(0, filename.length - 1) | ||
} | ||
const address = req.params[0] | ||
|
||
return server.handleGet(address, filename) | ||
.then((fileInfo) => { | ||
const exists = fileInfo.exists | ||
const contentType = fileInfo.contentType | ||
|
||
if (!exists) { | ||
return res.status(404).send('File not found') | ||
} | ||
|
||
const opts = { | ||
root: config.storageRootDirectory, | ||
headers: { | ||
'content-type': contentType | ||
} | ||
} | ||
const path = Path.join(address, filename) | ||
|
||
return res.sendFile(path, opts) | ||
}) | ||
.catch((err) => { | ||
logger.error(err) | ||
return res.status(400).send('Could not return file') | ||
}) | ||
}) | ||
|
||
return app | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env node | ||
import winston from 'winston' | ||
import { makeHttpServer } from './http.js' | ||
import { getConfig } from './config.js' | ||
|
||
const conf = getConfig() | ||
const app = makeHttpServer(conf) | ||
|
||
app.listen( | ||
app.config.port, | ||
() => winston.warn(`server starting on port ${app.config.port} in ${app.settings.env} mode`)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* @flow */ | ||
|
||
import Path from 'path' | ||
import fs from 'fs' | ||
|
||
const METADATA_DIRNAME = '.gaia-metadata' | ||
|
||
export class GaiaDiskReader { | ||
|
||
config: Object | ||
|
||
constructor(config: Object) { | ||
this.config = config | ||
} | ||
|
||
handleGet(topLevelDir: string, filename: string) | ||
: Promise<{ exists: boolean, contentType: ?string } > { | ||
const storageRoot = this.config.diskSettings.storageRootDirectory | ||
if (!storageRoot) { | ||
throw new Error('Misconfiguration: no storage root set') | ||
} | ||
|
||
const filePath = Path.join(storageRoot, topLevelDir, filename) | ||
try { | ||
fs.statSync(filePath) | ||
} catch (e) { | ||
const ret = { exists: false, contentType: undefined } | ||
return Promise.resolve().then(() => ret) | ||
} | ||
|
||
const metadataPath = Path.join(storageRoot, METADATA_DIRNAME, topLevelDir, filename) | ||
try { | ||
const metadataJSON = fs.readFileSync(metadataPath).toString() | ||
const metadata = JSON.parse(metadataJSON) | ||
const ret = { exists: true, contentType: metadata['content-type'] } | ||
return Promise.resolve().then(() => ret) | ||
} catch (e) { | ||
const ret = { exists: true, contentType: 'application/octet-stream' } | ||
return Promise.resolve().then(() => ret) | ||
} | ||
} | ||
} |
Oops, something went wrong.