hlx-file-writer
Advanced tools
Comparing version 0.0.7 to 0.0.8
24
file.js
const fs = require('fs'); | ||
const path = require('path'); | ||
const {URL} = require('url'); | ||
const debug = require('debug'); | ||
const {tryCatch, mkdirP} = require('hlx-util'); | ||
function storeData({uri, data}, rootPath) { | ||
const print = debug('hlx-file-writer'); | ||
function storeData({uri, parentUri, data}, {inputDir, outputDir}) { | ||
if (!data) { | ||
@@ -11,4 +14,4 @@ return Promise.reject(new Error('No segment data')); | ||
if (!path.isAbsolute(rootPath)) { | ||
rootPath = path.join(process.cwd(), rootPath); | ||
if (!path.isAbsolute(outputDir)) { | ||
outputDir = path.join(process.cwd(), outputDir); | ||
} | ||
@@ -18,11 +21,18 @@ | ||
print(`storeData: uri=${uri}, parentUri=${parentUri}, inputDir=${inputDir}, outputDir=${outputDir}`); | ||
if (path.isAbsolute(uri)) { | ||
localPath = path.join(rootPath, uri); | ||
localPath = path.join(outputDir, uri); | ||
} else { | ||
const obj = tryCatch( | ||
() => new URL(uri), | ||
() => new URL(uri, rootPath), | ||
() => new URL(uri, parentUri), | ||
() => null | ||
); | ||
localPath = path.join(rootPath, obj ? obj.pathname : uri); | ||
if (obj) { | ||
localPath = path.join(outputDir, obj.pathname); | ||
} else { | ||
const pathname = path.relative(inputDir, path.join(parentUri, uri)); | ||
localPath = path.join(outputDir, pathname); | ||
} | ||
} | ||
@@ -29,0 +39,0 @@ |
{ | ||
"name": "hlx-file-writer", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "A writable stream to save HLS playlists/segments as local files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,4 @@ [![Build Status](https://travis-ci.org/hlxjs/hlx-file-writer.svg?branch=master)](https://travis-ci.org/hlxjs/hlx-file-writer) | ||
* It determines the local path for each files based on the `uri` described in the HLS playlist. | ||
* The hostname contained in the `uri` will be ignored (e.g. "https://foo.bar/abc/def.m3u8" is translated into "{rootPath}/abc/def.m3u8") | ||
* The hostname contained in the `uri` will be ignored (e.g. "https://foo.bar/abc/def.m3u8" is translated into "{outputDir}/abc/def.m3u8") | ||
* If the `uri` is a file url, users can specify a root directory (`inputDir`) from which the file should be read (e.g. "file://path/to/abc/def.m3u8", inputDir="/path/to", is translated into "{outputDir}/abc/def.m3u8") | ||
@@ -31,3 +32,3 @@ ## Install | ||
const save = createWriteStream({ | ||
rootPath: '/var/www/media/', | ||
outputDir: '/var/www/media/', | ||
storePlaylist: true | ||
@@ -57,3 +58,4 @@ }); | ||
| ----------- | ------ | ------- | --------------------------------- | | ||
| rootPath | string | process.CWD() | The root directory in which all the files are stored | | ||
| inputDir | string | / | The root directory from which all the files are read (This option is only used in case of file urls) | | ||
| outputDir | string | process.CWD() | The root directory to which all the files are written | | ||
| storePlaylist | boolean | false | If true, the playlist files are also stored as local files | | ||
@@ -60,0 +62,0 @@ |
@@ -12,3 +12,4 @@ const stream = require('stream'); | ||
super({objectMode: true}); | ||
this.rootPath = options.rootPath || process.cwd(); | ||
this.outputDir = options.outputDir || process.cwd(); | ||
this.outputDir = options.inputDir || '/'; | ||
this.shouldStorePlaylist = Boolean(options.storePlaylist); | ||
@@ -26,6 +27,6 @@ } | ||
if (data.type === 'playlist') { | ||
params = {uri: data.uri, data: HLS.stringify(data)}; | ||
params = {uri: data.uri, parentUri: data.parentUri, data: HLS.stringify(data)}; | ||
} | ||
storeData(params, this.rootPath) | ||
storeData(params, this) | ||
.then(path => { | ||
@@ -32,0 +33,0 @@ print(`The data is written to ${path}`); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7603
99
62