tav-media
Version:
Cross platform media editing framework
70 lines (69 loc) • 2.64 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { tav } from '../tav';
import { Asset } from './tav-asset';
import { FileSystem } from '../io/file-system';
import { Path } from '../io/path';
/**
* ImageAsset is an asset that can be used to display image file.
* @hideconstructor
* @category Assets
*/
export class ImageAsset extends Asset {
constructor() {
super(...arguments);
/**
* Returns `'ImageAsset'`
*/
this.type = 'ImageAsset';
}
/**
* Creates a new ImageAsset from the specified file path. Returns null if the file does not
* exist or it's not a valid image file.
*/
static MakeFromPath(path) {
return __awaiter(this, void 0, void 0, function* () {
const imageAsset = new ImageAsset(null, path);
yield imageAsset.prepare();
const asset = yield imageAsset.build();
if (!asset) {
imageAsset.release();
return null;
}
return imageAsset;
});
}
doPrepare() {
const _super = Object.create(null, {
doPrepare: { get: () => super.doPrepare }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.doPrepare.call(this);
this.localPath = this.path;
if (tav.PLATFORM === 'wx' || tav.PLATFORM === 'browser') {
try {
this.localPath = Path.getLocalFileName(this.path);
yield FileSystem.download(this.localPath, this.path);
}
catch (e) {
console.error('image load failed', this.path);
return null;
}
}
});
}
createAsset() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.localPath)
return undefined;
return yield tav.ImageAsset.MakeFromPath(this.localPath);
});
}
}