Skip to content

Commit

Permalink
refactor(): Refactor 'Asset' class and more clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Jan 13, 2020
1 parent 6986e90 commit 974412d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 68 deletions.
147 changes: 91 additions & 56 deletions asset.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,113 @@
const extensions = require('./extensions')
const Resource = require('nanoresource')
const through = require('through2')
const getUri = require('get-uri')
const debug = require('debug')('little-media-box:asset')
const ready = require('nanoresource-ready')
const path = require('path')
const pump = require('pump')
const uuid = require('uuid/v4')
const url = require('url')
const fs = require('fs')

/**
* An array of known image ext
* @private
*/
const IMAGE_EXTNAMES = [
'.jpeg',
'.jpg',
'.png',
'.tif',
'.tiff'
]

/**
* @private
*/
const METADATA_EXTNAME = [
'.csv',
'.txt',
'.xml'
]

/**
* The `Asset` class represents a container for an external asset
* that has semantic associations with other objects.
* @public
* @class
* @extends nanoresource
*/
class Asset {
constructor(opts) {
this.id = uuid()
this.associations = []
class Asset extends Resource {

const opts = options //copy
if (opts.assetType) {
this.assetType = opts.assetType
}
/**
* `Asset` class constructor.
* @param {String} uri
* @param {?(Object)} opts
* @param {?(String)} opts.id
* @param {?(String)} opts.cwd
* @param {?(String)} opts.type
* @param {?(Array<String>)} opts.associations
*/
constructor(uri, opts) {
super()

if (opts.association) {
if (Array.isArray(opts.association) &&
opts.association.every(a => {
verifyAssociation(a)
})
) {
this.associations = opts.association
}
else if (this.verifyAssociation(opts.association)) {
this.associations = [opts.association]
} else {
console.error('Could not verify association', opts.association)
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}

if (opts.uri) {
this.uri = path.resolve(opts.uri)
this.assetType = this.getLikelyType()
this.id = opts.id || uuid()
this.uri = uri
this.cwd = opts.cwd || process.cwd()
this.type = opts.type || extensions.typeof(path.extname(this.uri))
this.byteLength = 0
this.associations = Array.from(opts.associations || [])
}

/**
* Implements the abstract `_open()` method for `nanoresource`
* Opens the asset source stream and initializes internal state.
* @protected
* @param {Function} callback
*/
_open(callback) {
const uri = url.parse(this.uri)
if (/https?:/.test(uri.protocol)) {
head(this.uri, (err, res) => {
if (err) { return callback(err) }
this.byteLength = parseInt(res.headers['content-length'])
callback(null)
})
} else {
const pathname = path.resolve(this.cwd, uri.path)
fs.stat(pathname, (err, stats) => {
if (err) { return callback(err) }
this.uri = `file://${pathname}`
this.byteLength = stats.size
callback(null)
})
}
}

get assetType() {
/**
* Wait for asset to be ready (opened) calling `callback()`
* when it is.
* @param {Function}
*/
ready(callback) {
ready(this, callback)
}

if (!this.uri) {
return null
/**
* Creates a read stream for the asset URI.
* @param {?(Object)} opts
* @return {Stream}
*/
createReadStream(opts) {
if (!opts || 'object' !== typeof opts) {
opts = {}
}

const extname = path.extname(this.uri)
const readStream = through()
const { uri } = this

getUri(uri, onstream)

return readStream

function onstream(err, sourceStream) {
if (err) { return readStream.emit('error', err) }
pump(sourceStream, readStream, onpump)
}

if ([
].includes(path.extname(this.uri).toLowerCase())) {
return 'image'
} else if ([
].includes(path.extname(this.uri).toLowerCase())) {
return 'metadata'
function onpump(err) {
if (err) { return readStream.emit('error', err) }
}
}
}

module.exports = Asset
/**
* Module exports.
*/
module.exports = {
Asset
}
25 changes: 14 additions & 11 deletions extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ text.add('mpd') // MPEG-DASH manifest file
text.add('srt') // SubRip Subtitle File
text.add('vtt') // Web Video Text Tracks File

video.add('264')
video.add('3gp')
video.add('3gp2')
video.add('3gpp')
video.add('3gpp2')
video.add('264') // Ripped Video Data File
video.add('3gp') // 3GPP Multimedia File
video.add('3gp2') // 3GPP Multimedia File
video.add('3gpp') // 3GPP Media File
video.add('3gpp2') // 3GPP2 Multimedia File

video.add('h264')
video.add('h265')
video.add('hevc')
video.add('h264') // H.264 Encoded Video File
video.add('hevc') // High Efficiency Video Coding File

video.add('m1v')
video.add('ts') // (also `.mpeg`) by adding this we overload `.ts` over TypeScript files
video.add('m1v') // MPEG-1 Video File
video.add('ts') // Video Transport Stream File (also `.mpeg`) - by adding this we overload `.ts` over TypeScript files

/**
* The `Extensions` class represents an extended `Array` that contains a
Expand Down Expand Up @@ -146,8 +145,12 @@ class Extensions extends Array {
return 1
} else if (-1 === left && -1 === right) {
return 0
} else if (left === right) {
return 0
} else if (left < right) {
return -1
} else {
return left === right ? 0 : left < right ? -1 : 1
return 1
}
})

Expand Down
1 change: 0 additions & 1 deletion source.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { Transform } = require('streamx')
const { ffmpeg } = require('./ffmpeg')
const { head } = require('simple-get')
const Resource = require('nanoresource')
Expand Down

0 comments on commit 974412d

Please sign in to comment.