atomicals-js
Version:
Atomicals JavaScript SDK and CLI
127 lines (121 loc) • 5.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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.RenderPreviewsCommand = exports.isText = exports.isImage = void 0;
function isImage(contentType) {
return /^image\/(jpe?g|png|gif|bmp|webp|svg)$/.test(contentType);
}
exports.isImage = isImage;
function isText(contentType) {
return /utf\-?8|application\/json|text\/plain|markdown|xml|html/.test(contentType);
}
exports.isText = isText;
class RenderPreviewsCommand {
constructor(filesmap, body) {
this.filesmap = filesmap;
this.body = body;
}
run() {
return __awaiter(this, void 0, void 0, function* () {
for (const inputIndex in this.filesmap) {
if (!this.filesmap.hasOwnProperty(inputIndex)) {
continue;
}
console.log(`-------------------------------------------------`);
console.log(`Rendering files at inputIndex ${inputIndex}`);
for (const filename in this.filesmap[inputIndex].files) {
if (!this.filesmap[inputIndex].files.hasOwnProperty(filename)) {
continue;
}
console.log(`-------------------------------------------------`);
console.log(`Rendering file ${filename}`);
const filepath = this.filesmap[inputIndex].files[filename].fullPath;
const contentType = this.filesmap[inputIndex].files[filename].contentType;
const contentLength = this.filesmap[inputIndex].files[filename].contentLength;
const body = this.filesmap[inputIndex].files[filename].body;
console.log('File name: ', filename);
console.log('Full path: ', filepath);
console.log('Content Type: ', contentType);
console.log('Content Length: ', contentLength);
if (this.body) {
console.log('Body (hex encoded): ', this.filesmap[inputIndex].files[filename].body);
}
if (isImage(contentType)) {
const { default: terminalImage } = yield Promise.resolve().then(() => __importStar(require("terminal-image")));
console.log(yield terminalImage.file(filepath));
}
else if (isText(contentType)) {
console.log('Body decoded: ');
console.log(Buffer.from(this.filesmap[inputIndex].files[filename].body, 'hex').toString('utf8'));
}
else {
console.log(`File is not an image or text-like. View file manually at ${filepath}`);
}
}
}
/*
displayImage.fromFile("banner.png").then(image => {
console.log(image)
})
displayImage.fromFile("shadow.gif").then(image => {
console.log(image)
})
displayImage.fromFile("ape.png").then(image => {
console.log(image)
})
displayImage.fromFile("pepe.png").then(image => {
console.log(image)
}) */
return null;
});
}
}
exports.RenderPreviewsCommand = RenderPreviewsCommand;
/*
// Remove the body by default
if (!body) {
for (const inputIndex in result.data.filemap) {
if (!result.data.filemap.hasOwnProperty(inputIndex)) {
continue;
}
for (const filename in result.data.filemap[inputIndex].files) {
if (!result.data.filemap[inputIndex].files.hasOwnProperty(filename)) {
continue;
}
const fileEntry = result.data.filemap[inputIndex].files[filename];
delete fileEntry['body'];
}
}
}
*/