196 lines
7.1 KiB
TypeScript
196 lines
7.1 KiB
TypeScript
import * as stream from 'stream';
|
|
export declare type ErrCallback = (err) => void;
|
|
export declare type DataCallback = (err?: any, data?: any) => void;
|
|
export declare type BlobCallback = (err?: any, blob?: Blob) => void;
|
|
export declare type FilenameCallback = (err?: any, filename?: string) => void;
|
|
export declare type FilenameOrUrlCallback = (err?: any, filenameOrUrl?: string) => void;
|
|
/** Asset storage object. Assets are binary files attached to a node.
|
|
* Use `node.createAsset()` to create an asset object. Asset objects have
|
|
* minimal meta information about the asset file: just subtype and
|
|
* file extension. All other metadata should be stored in the node it
|
|
* is attached to.
|
|
*
|
|
* @class Asset
|
|
* @memberof jibo.kb
|
|
* @param {string} [filenameOrUrl] Filename to asset on disk, or url
|
|
* to asset via KB service.
|
|
* @param {string} [subtype] Subtype name of asset. Defaults to `asset`.
|
|
* @param {string} [ext] Optional filename extension.
|
|
*/
|
|
export default class Asset {
|
|
_id: string;
|
|
subtype: string;
|
|
ext: string;
|
|
rootDir: string;
|
|
constructor(filenameOrURL?: string, subtype?: string, ext?: string);
|
|
/** Set root directory for this asset.
|
|
*
|
|
* @method jibo.kb.Asset#setRootDir
|
|
* @param {string} rootDir The full directory name or base URL
|
|
* that points to the storage area for this asset in its KB slice
|
|
* (which must be the same as the node it is attached to).
|
|
* @internal
|
|
*/
|
|
setRootDir(rootDir: string): void;
|
|
/** The filename for this asset, without any directory
|
|
* information. Format of the filename is:
|
|
*
|
|
* `{$_id}.{$subtype}.{$ext}`.
|
|
*
|
|
* If the asset doesn't have an extension, `{$ext}` (and its dot
|
|
* seperator) will be absent.
|
|
*
|
|
* @method jibo.kb.Asset#filename
|
|
* @returns {string} The filename.
|
|
*/
|
|
filename(): string;
|
|
/** Full path to the asset file on disk or a URL to fetch it
|
|
* via the KB service.
|
|
*
|
|
* @method jibo.kb.Asset#fullFilenameOrURL
|
|
* @returns {string} Full path filename or URL.
|
|
*/
|
|
fullFilenameOrURL(): string;
|
|
/** Print useful console logs from asset objects.
|
|
*
|
|
* @method jibo.kb.Asset#toString
|
|
* @returns {string} Full path filename or URL.
|
|
*/
|
|
toString(): string;
|
|
/** Write out the data for this asset file. Data can be provided
|
|
* as a readable stream, buffer, or blob.
|
|
*
|
|
* @method jibo.kb.Asset#save
|
|
* @param {Stream|Buffer|Blob} data Data to be saved in asset file.
|
|
* @param {Function} [callback] Called when done. If callback is
|
|
* omitted a promise is returned instead.
|
|
* @returns {Promise} A promise if the callback is omitted.
|
|
*/
|
|
save(data: any, callback: FilenameOrUrlCallback): any;
|
|
save(data: any): Promise<string>;
|
|
/** Set up an asset object by parsing an asset filename or
|
|
* URL. Sets the `_id`, `subtype`, and `ext`. Set rootDir if full
|
|
* path filename or URL.
|
|
*
|
|
* @method jibo.kb.Asset#setSelfFromFilenameOrURL
|
|
* @param {string} filenameOrUrl Filename, full path filename, or URL.
|
|
*/
|
|
setSelfFromFilenameOrURL(filenameOrURL: string): void;
|
|
/** Load the binary file pointed to by this asset object.
|
|
* Returns a buffer (via callback) with the contents (data) of the file.
|
|
*
|
|
* @method jibo.kb.Asset#load
|
|
* @param {Function} [callback] Called with (err, data) when
|
|
* done. If callback is omitted a promise that resolves to `data`
|
|
* is returned instead.
|
|
* @returns {Promise} A promise that resolves with the value of
|
|
* `data` if the callback is omitted.
|
|
*/
|
|
load(callback: DataCallback): any;
|
|
load(): Promise<any>;
|
|
/** Create a stream of the binary file this asset object points to.
|
|
* Return a stream (via callback) of the contents (data)
|
|
* of the file.
|
|
*
|
|
* @method jibo.kb.Asset#loadStream
|
|
* @returns {Stream} Stream of binary asset data.
|
|
*/
|
|
loadStream(): stream.Stream;
|
|
/** Load the binary file pointed to by this asset object.
|
|
* Returns a blob (via callback) with the contents (data) of the file.
|
|
*
|
|
* @method jibo.kb.Asset#loadBlob
|
|
* @param {Function} [callback] Called with (err, data) when
|
|
* done. If callback is omitted a promise that resolves to `data`
|
|
* is returned instead.
|
|
* @returns {Promise} A promise that resolves with the value of
|
|
* `data` if the callback is omitted.
|
|
*/
|
|
loadBlob(callback: BlobCallback): any;
|
|
loadBlob(): Promise<Blob>;
|
|
/** Remove the asset file from disk.
|
|
*
|
|
* @method jibo.kb.Asset#remove
|
|
* @param {Function} [callback] Called when done. If callback is
|
|
* omitted a promise is returned instead.
|
|
* @returns {Promise} A promise if the callback is omitted.
|
|
*/
|
|
remove(callback: ErrCallback): any;
|
|
remove(): Promise<any>;
|
|
/** Assemble the URL for this asset.
|
|
*
|
|
* @method jibo.kb.Asset#_url
|
|
* @returns {string} URL
|
|
* @private
|
|
*/
|
|
private _url();
|
|
/** Saves the asset data via the KB service web interface.
|
|
*
|
|
* @method jibo.kb.Asset#_saveViaWeb
|
|
* @private
|
|
*/
|
|
private _saveViaWeb(data, callback);
|
|
/** Set up an asset by parsing a filename
|
|
*
|
|
* @method jibo.kb.Asset#_setSelfFromFilename
|
|
* @param {string} filename Filename or full path filename
|
|
* @private
|
|
*/
|
|
private _setSelfFromFilename(filename);
|
|
/** Set up an asset from a URL
|
|
*
|
|
* @method jibo.kb.Asset#_setSelfFromURL
|
|
* @param {string} URL
|
|
* @private
|
|
*/
|
|
private _setSelfFromURL(httpurl);
|
|
/** Remove the asset file from disk via KB service.
|
|
*
|
|
* @method jibo.kb.Asset#_removeViaWeb
|
|
* @param {Function} callback Called when done
|
|
* @private
|
|
*/
|
|
private _removeViaWeb(callback);
|
|
/** Save asset file from buffer.
|
|
*
|
|
* @method jibo.kb.Asset#_saveBuffer
|
|
* @param {Buffer} buffer Data to be saved in asset file
|
|
* @param {Function} callback Called when done
|
|
* @private
|
|
*/
|
|
private _saveBuffer(buffer, callback);
|
|
/** Save asset file from stream.
|
|
*
|
|
* @method jibo.kb.Asset#_saveStream
|
|
* @param {Stream} stream Data to be saved in asset file
|
|
* @param {Function} callback Called when done
|
|
* @private
|
|
*/
|
|
private _saveStream(stream, callback);
|
|
/** Loads the asset via the KB service.
|
|
*
|
|
* @method jibo.kb.Asset#_loadBufferViaWeb
|
|
* @param {Function} callback Called with (err, data) when done
|
|
* @private
|
|
*/
|
|
private _loadBufferViaWeb(callback);
|
|
/** Loads a stream of the asset via the KB service.
|
|
*
|
|
* @method jibo.kb.Asset#_loadStreamViaWeb
|
|
* @returns {Stream} Stream of asset
|
|
* @private
|
|
*/
|
|
private _loadStreamViaWeb();
|
|
/** Check http request result for null error object and 2xx result code and
|
|
* generate an error object if not (or pass through non null error object).
|
|
*
|
|
* @method jibo.kb.Asset#_checkStatusCode
|
|
* @param {Error} err Error object returned from request
|
|
* @param {Object} res Http request result object
|
|
* @param {string} [message] Message to add to error object
|
|
* @returns {Error} Error object or null
|
|
* @private
|
|
*/
|
|
private _checkStatusCode(err, res, message?);
|
|
}
|