Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltwaterC committed Aug 11, 2011
1 parent 23dc573 commit b943139
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 20 deletions.
7 changes: 5 additions & 2 deletions .gitignore
@@ -1,2 +1,5 @@
/.project
/.settings
.project
.settings/
src/
bin/
share/
5 changes: 5 additions & 0 deletions .npmignore
@@ -0,0 +1,5 @@
.project
.settings/
src/
bin/
share/
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,6 @@
## v0.2
* Includes the file(1) (v5.07) source tree along with a build script which was made to work on Ubuntu / OS X. The wrapper uses this file version along with the appropriate magic database.

## v0.1.1
* Updated the error detection code. Some file 4.x/5.x versions used to send the error message via STDOUT. The recent versions fix this behavior by sending the error message via STDERR. mime-magic error reporting is compatible with both.

Expand Down
12 changes: 10 additions & 2 deletions Makefile
@@ -1,5 +1,13 @@
all:
npm:
/usr/bin/env npm install

publish: all
publish: npm
/usr/bin/env npm publish

build:
tools/build.sh

purge:
rm -rf src/
rm -rf bin/
rm -rf share/
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -24,8 +24,8 @@ mime.fileWrapper('/path/to/foo.pdf', function (err, mime) {
});
```

Passing relative paths is supported. The fileWrapper uses child_process.exec behind the scenes, therefore the err argument contains the information returned by the exec method itself plus the error message returned by file(1). The module was developed against file 5.03 which sends the error message via STDOUT. An unfortunate choice from the developer, but if your file utility behaves differently, then you may not get the exact error message.
Passing relative paths is supported. The fileWrapper uses child_process.exec behind the scenes, therefore the err argument contains the information returned by the exec method itself plus the error message returned by file(1).

## Known issues
## Notice

Not all the unices are supported by this release. The file(1) invocation is different on OS X for example. Other possible (such as BSD ports) use cases are unknown. Till a proper wrapper and proper bindings are available, consider it usable just under the above circumstances.
The module was developed under Ubuntu 10.04. It was tested under OS X Snow Leopard. Other platforms may be supported, but the behavior is untested.
Binary file added arc/file-5.07.tar.gz
Binary file not shown.
18 changes: 7 additions & 11 deletions lib/mime-magic.js
@@ -1,24 +1,20 @@
var p = require('path');
var cp = require('child_process');

var fileExec = p.resolve(__dirname + '/../bin/file') + ' --magic-file ' + p.resolve(__dirname + '/../share/magic.mgc') + ' --brief --mime-type ';

var trim = function (string) {
string = string || '';
return string.replace(/^\s*/, '').replace(/\s*$/, '');
return string.trim();
};

var fileWrapper = function (path, cb) {
path = p.resolve(path);
cp.exec('/usr/bin/env file --brief --mime-type ' + path, function (err, stdout, stderr) {
stdout = trim(stdout);
stderr = trim(stderr);
cp.exec(fileExec + p.resolve(path), function (err, stdout) {
stdout = stdout.trim();
if (err) {
var message = '';
if (stderr) {
message = stderr;
} else if (stdout) { // some version send the error over STDOUT
message = stdout;
if (stdout) {
err.message = stdout;
}
err.message = message;
cb(err);
} else {
cb(null, stdout);
Expand Down
8 changes: 6 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "mime-magic",
"version": "0.1.1",
"version": "0.2.0",
"main": "./lib/mime-magic.js",
"description": "libmagic bindings for node.js. Includes a file(1) wrapper as alternative.",
"engines": {
Expand All @@ -15,5 +15,9 @@
"type": "git",
"url": "git://github.com/SaltwaterC/mime-magic.git"
},
"keywords": ["mime", "magic", "libmagic", "file", "wrapper"]
"keywords": ["mime", "magic", "libmagic", "file", "wrapper"],
"scripts": {
"preinstall": "make build",
"preuninstall": "make purge"
}
}
26 changes: 26 additions & 0 deletions tools/build.sh
@@ -0,0 +1,26 @@
#!/bin/sh

if [ ! -d src/file-5.07 ]
then
cd arc
tar -xf file-5.07.tar.gz
mkdir ../src
mv file-5.07 ../src
cd ..
fi

if [ ! -f share/magic.mgc -o ! -f bin/file ]
then
cd src/file-5.07
./configure
make
cd ../../
mkdir -p share
cp src/file-5.07/magic/magic.mgc share
mkdir -p bin/.libs
./src/file-5.07/src/file -v
cp src/file-5.07/src/file bin/file
cp src/file-5.07/src/.libs/file bin/.libs
fi

exit 0

0 comments on commit b943139

Please sign in to comment.