@sudoo/exif-node
Version:
:camera: Exif for Node
86 lines (85 loc) • 3.09 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExifNode = void 0;
const exif_1 = require("@sudoo/exif");
const io_1 = require("@sudoo/io");
class ExifNode extends exif_1.Exif {
static attemptFromBinaryString(binaryString) {
try {
return this.fromBinaryString(binaryString);
}
catch (err) {
return null;
}
}
static fromBinaryString(binaryString) {
try {
return new ExifNode(binaryString);
}
catch (err) {
throw new Error('[Sudoo-Exif-Node] Invalid Image Data');
}
}
static attemptFromBase64(base64) {
const splited = base64.split(',');
if (splited.length === 2) {
const splitedData = Buffer.from(splited[1], 'base64');
return this.attemptFromBuffer(splitedData);
}
const data = Buffer.from(base64, 'base64');
return this.attemptFromBuffer(data);
}
static fromBase64(base64) {
const splited = base64.split(',');
if (splited.length === 2) {
const splitedData = Buffer.from(splited[1], 'base64');
return this.fromBuffer(splitedData);
}
const data = Buffer.from(base64, 'base64');
return this.fromBuffer(data);
}
static attemptFromBuffer(image) {
const data = image.toString('binary');
return this.attemptFromBinaryString(data);
}
static fromBuffer(image) {
const data = image.toString('binary');
return this.fromBinaryString(data);
}
static loadFromFile(path) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield io_1.readBufferFile(path);
return this.fromBuffer(result);
});
}
constructor(data) {
super(data);
}
toBase64() {
return this.toBuffer().toString('base64');
}
toBase64WithType(type) {
const actualType = type === 'jpg' ? 'jpeg' : type;
const header = `image/${actualType};base64,`;
return header + this.toBase64();
}
toBuffer() {
return Buffer.from(this._imageData, 'binary');
}
saveAsFile(path) {
return __awaiter(this, void 0, void 0, function* () {
yield io_1.writeBufferFile(path, this.toBuffer());
return;
});
}
}
exports.ExifNode = ExifNode;