Skip to content

Commit

Permalink
Merge pull request #7 from hlxjs/issues/fix-dest-path
Browse files Browse the repository at this point in the history
Fix dest path issue
  • Loading branch information
kuu authored Jul 27, 2019
2 parents 22966ae + 89b642e commit c6194f9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 96 deletions.
13 changes: 9 additions & 4 deletions file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');

const {getPath} = require('./util');
const {tryCatch} = require('hlx-util');

function storeData({uri, data}, rootPath) {
if (!data) {
Expand All @@ -14,10 +14,15 @@ function storeData({uri, data}, rootPath) {

let localPath;

if (path.isAbsolute(uri) && fs.existsSync(uri)) {
localPath = path.join(rootPath, path.basename(uri));
if (path.isAbsolute(uri)) {
localPath = path.join(rootPath, path.relative(rootPath, uri));
} else {
localPath = path.join(rootPath, getPath(uri));
const obj = tryCatch(
() => new URL(uri),
() => new URL(uri, rootPath),
() => null
);
localPath = path.join(rootPath, obj ? obj.pathname : uri);
}

// Create directory
Expand Down
23 changes: 18 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
},
"dependencies": {
"debug": "^4.1.1",
"hls-parser": "^0.4.3"
"hls-parser": "^0.4.4",
"hlx-util": "0.0.2"
},
"devDependencies": {
"ava": "^2.2.0",
Expand Down
31 changes: 31 additions & 0 deletions test/spec/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ test.cb('file.storeData.Buffer', t => {
});
});

test.cb('file.storeData.Buffer.path', t => {
const mockFs = {
writeFile(path, data, cb) {
setImmediate(() => {
cb(null, path);
});
},
existsSync() {
return true;
},
lstatSync() {
return {
isDirectory() {
return true;
}
};
}
};

delete require.cache[require.resolve('fs')];
const {storeData} = proxyquire('../../file', {fs: mockFs});
const spyWriteFile = sinon.spy(mockFs, 'writeFile');

storeData({uri: '/abc/def/ghi/jkl.mp4', data: Buffer.alloc(10)}, '/abc/def/')
.then(destPath => {
t.is(destPath, '/abc/def/ghi/jkl.mp4');
t.is(spyWriteFile.callCount, 1);
t.end();
});
});

test.cb('file.storeData.Stream', t => {
const mockFs = {
createWriteStream(path) {
Expand Down
39 changes: 0 additions & 39 deletions test/spec/util.spec.js

This file was deleted.

47 changes: 0 additions & 47 deletions util.js

This file was deleted.

0 comments on commit c6194f9

Please sign in to comment.