Permalink
Browse files

静的ファイルのサンプルを同梱して、bucketを指定した場合の正規表現がおかしかったので修正

  • Loading branch information...
1 parent b0d9372 commit 109414845f6d57ecbafc9625c82d5e776c33de39 @linyows committed Apr 24, 2012
Showing with 69 additions and 46 deletions.
  1. +13 −14 README.md
  2. +1 −1 lib/hose/handler.js
  3. +14 −14 lib/hose/parse.js
  4. +41 −17 lib/hose/render.js
View
@@ -3,7 +3,7 @@ Hose
<img src="//github.com/linyows/hose/raw/master/etc/hose.png"/></img>
-hose is real-time resizing image server for [Amazon S3](http://aws.amazon.com/s3/) on [node.js](http://nodejs.org).
+hose is real-time resizing image server for [Amazon S3](http://aws.amazon.com/s3/).
Inspired by [cookpad's slide](http://www.slideshare.net/mirakui/ss-8150494).
Features
@@ -18,7 +18,7 @@ Quick start (mac)
Install:
- $ brew install imagemagick
+ $ brew install node imagemagick
$ git clone git://github.com/linyows/hose.git
$ cd hose
$ npm install
@@ -32,7 +32,6 @@ S3:
accessKeyId: 'your access key id'
secretKeyId: 'your secret key id'
bucketName: 'your bucket name'
- staticHost: 'foobar.s3.amazonaws.com'
Sign:
secretKey: 'your secret key id'
adminKey: 'test'
@@ -42,25 +41,18 @@ Deploy file on "bucketName":
/baz.jpg
-Deploy files on "staticHost":
-
- /favicon.ico
- /403.html
- /404.html
- /500.html
-
Start server:
$ ./bin/hose --port 8000
Access:
http://localhost:8000/baz/300x300cq80/test.jpg
-
+ http://localhost:8000/baz/100x50cq90/test.jpg
Url
---
-
+
Example
http://foobar.com/myBucket/(category/subcategory/date/fileName)/100x100cq75/802a393d7247aa0caf9056223503bdf611d478ee.jpg
@@ -89,7 +81,7 @@ S3:
In the **yaml-file**
http://yourdomain.com/(filePath without extension)/resizeParams/signature.ext
-
+
`$ vim config/default.yaml`
```yaml
@@ -102,7 +94,14 @@ S3:
Running Tests
-------------
- $ make test
+<del>$ make test</del>
+
+Todo
+----
+
+- Test code
+- Register npm
+- Design of sample html
License
-------
View
@@ -20,7 +20,7 @@ Handler = module.exports = function(req, res){
var s3Req = knox.createClient({
key: CONFIG.S3.accessKeyId,
secret: CONFIG.S3.secretKeyId,
- bucket: (CONFIG.S3.bucketName || CONFIG.S3.backetPrefix + parsed.bucket + CONFIG.S3.backetSuffix)
+ bucket: (CONFIG.S3.bucketName || CONFIG.S3.bucketPrefix + parsed.bucket + CONFIG.S3.bucketSuffix)
}).get('/' + parsed.path + parsed.extension);
s3Req.end();
View
@@ -1,5 +1,5 @@
var url = require('url'),
- CONFIG = require('config').Sign;
+ CONFIG = require('config');
Parse = module.exports = function(str){
this.str = str;
@@ -21,28 +21,28 @@ Parse.prototype._url = function(){
bucket: '([A-z0-9.-]+)',
filePath: '([A-z0-9/_-]+)',
resizeParams: '([0-9]{2,3})?(x)?([0-9]{2,3})?(c)?(q([0-9]{2}|100))?',
- signature: (CONFIG.adminKey !== '' ? '([a-f0-9]{64}|' + CONFIG.adminKey + ')' : '([a-f0-9]{64})'),
+ signature: (CONFIG.Sign.adminKey !== '' ? '([a-f0-9]{64}|' + CONFIG.Sign.adminKey + ')' : '([a-f0-9]{64})'),
extension: '(.jpg|.png|.gif)'
};
- var patern = '^/(' + r.bucket + '?/' + r.filePath + '/' + r.resizeParams + ')/' + r.signature + r.extension + '$';
+ var patern = '^/((' + r.bucket + '/)?' + r.filePath + '/' + r.resizeParams + ')/' + r.signature + r.extension + '$';
var regexp = new RegExp(patern, 'i');
var matches = regexp.exec(parsed.pathname);
if (!matches) { return null; }
return {
uri: matches[0],
key: matches[1],
- bucket: matches[2],
- path: matches[3],
- width: (('undefined' === typeof matches[4]) ? '' : matches[4] - 0),
- height: (('undefined' === typeof matches[6]) ? '' : matches[6] - 0),
- max: (('undefined' === typeof matches[5]) ? (('undefined' === typeof matches[4]) ? matches[6] : matches[4]) - 0 : false),
- crop: (('undefined' === typeof matches[7]) ? false : true),
- quality: (('string' === typeof matches[9]) ? matches[9] : 100),
- qualityRate: (('string' === typeof matches[9]) ? matches[9] / 100 : 1.0),
- signature: matches[10],
- extension: (('undefined' === typeof matches[11]) ? '' : matches[11]),
- type: this._toFileType(matches[11])
+ bucket: CONFIG.S3.bucketName === '' ? matches[3] : '',
+ path: CONFIG.S3.bucketName === '' ? matches[4] : ('undefined' === typeof matches[2] ? matches[4] : matches[2] + matches[4]),
+ width: 'undefined' === typeof matches[5] ? '' : matches[5] - 0,
+ height: 'undefined' === typeof matches[7] ? '' : matches[7] - 0,
+ max: 'undefined' === typeof matches[6] ? ('undefined' === typeof matches[5] ? matches[7] : matches[5]) - 0 : false,
+ crop: 'undefined' === typeof matches[8] ? false : true,
+ quality: 'string' === typeof matches[10] ? matches[10] : 100,
+ qualityRate: 'string' === typeof matches[10] ? matches[10] / 100 : 1.0,
+ signature: matches[11],
+ extension: 'undefined' === typeof matches[12] ? '' : matches[12],
+ type: this._toFileType(matches[12])
};
};
View
@@ -1,5 +1,6 @@
-var http = require('http'),
- util = require('util'),
+var http = require('http'),
+ fs = require('fs'),
+ util = require('util'),
logger = require('./logger'),
CONFIG = require('config');
@@ -8,11 +9,11 @@ var Render = module.exports = require('./klass').create();
Render.include({
init: function(req, res) {
this.staticServer = {
- host: CONFIG.S3.staticHost,
- port: CONFIG.S3.staticPort || 80,
- path: CONFIG.S3.staticRoot || '/'
+ host: CONFIG.Static.host,
+ port: CONFIG.Static.port || 80,
+ path: CONFIG.Static.root || '/'
};
- this.encoding = CONFIG.S3.staticEncoding || 'utf-8';
+ this.encoding = CONFIG.Static.encoding || 'utf-8';
this.serverName = CONFIG.Server.name || '';
this.req = req;
this.res = res;
@@ -73,29 +74,52 @@ Render.include({
this._static('/500.html', { code: 500 });
},
- _static: function(path, options) {
- var self = this;
- var staticServer = this.staticServer;
- staticServer.path = staticServer.path.replace(/\/$/, '') + path;
-
+ _static: function(path, options){
if (options) {
- var code = (options.code) ? options.code : 500;
- var encoding = (options.encoding) ? options.encoding : this.encoding;
+ this.code = (options.code) ? options.code : 500;
+ this.encoding = (options.encoding) ? options.encoding : this.encoding;
+ }
+ if (this.staticServer.host === '') {
+ this._local(path);
+ } else {
+ this._remote(path);
}
+ },
+ _local: function(path){
+ var self = this;
+ fs.readFile(__dirname + '/../../public' + path, function (err, buf) {
+ if (err) {
+ console.log(err);
+ self.fatalError();
+ util.error(err);
+
+ } else {
+ self.res.write(buf, self.encoding);
+ self.res.end();
+ var Logger = logger.inst(self.req, self.code);
+ }
+ });
+
+ },
+ _remote: function(path) {
+ var self = this;
+ var staticServer = self.staticServer;
+ staticServer.path = staticServer.path.replace(/\/$/, '') + path;
+
http.get(staticServer, function(res) {
var buf = '';
- res.setEncoding(this.encoding);
- self.res.writeHead(code, res.headers);
+ res.setEncoding(self.encoding);
+ self.res.writeHead(self.code, res.headers);
res.on('data', function(chunk) {
buf += chunk;
});
res.on('end', function() {
- self.res.write(buf, this.encoding);
+ self.res.write(buf, self.encoding);
self.res.end();
- var Logger = logger.inst(self.req, code);
+ var Logger = logger.inst(self.req, self.code);
});
}).on('error', function(err) {

0 comments on commit 1094148

Please sign in to comment.