UNPKG

md-curcuma

Version:

A Typescript library for transporting and converting markdown and other data.

228 lines (227 loc) 8.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CSV_IO = void 0; const observer_1 = require("../core/observer"); const fs = require("fs"); const client_http = require("http"); const client_https = require("https"); const url_1 = require("url"); class CSV_IO { constructor(props) { this.observer_subject = new observer_1.Observer_Subject(); this.props = null; this.props = props; } add_observer(observer, id) { this.observer_subject.add_observer(observer, id); } notify_all(props) { this.observer_subject.notify_all(props); } notify(props) { this.observer_subject.notify(props); } read() { let csv_buffer = fs.readFileSync(this.props.readPath); const rows_array = csv_buffer.toString().split("\n"); let json_obj_array = []; const header_csv = rows_array[0]; let header_array = new Array(); if (header_csv.trim().length > 0) { let inSQuotes = false; let inDQuotes = false; let tmpParam = ""; for (let i = 0; i < header_csv.length; ++i) { const char = header_csv.substring(i, i + 1); if (char == "'") inSQuotes = inSQuotes ? false : !inDQuotes; else if (char == '"') inDQuotes = inDQuotes ? false : !inSQuotes; if (char == ",") { if (inSQuotes) tmpParam += char; else if (inDQuotes) tmpParam += char; else { header_array.push(CSV_IO.cleanup(tmpParam)); tmpParam = ""; } } else tmpParam += char; } header_array.push(CSV_IO.cleanup(tmpParam)); } for (let i = 1; i < rows_array.length - 1; i++) { let json_obj = {}; let str = rows_array[i]; let s = ""; let flag = 0; for (let ch of str) { if (ch === '"' && flag === 0) { flag = 1; } else if (ch === '"' && flag == 1) flag = 0; if (ch === "," && flag === 0) ch = "|"; if (ch !== '"') s += ch; } let properties = s.split("|"); for (let j in header_array) { json_obj[header_array[j]] = properties[j]; } } } write(dao) { } static transform_to_json(job_parameter) { let csv_buffer = fs.readFileSync(job_parameter.readPath); const rows_array = csv_buffer.toString().split("\n"); let json_obj_array = []; const header_csv = rows_array[0]; let header_array = new Array(); if (header_csv.trim().length > 0) { let inSQuotes = false; let inDQuotes = false; let tmpParam = ""; for (let i = 0; i < header_csv.length; ++i) { const char = header_csv.substring(i, i + 1); if (char == "'") inSQuotes = inSQuotes ? false : !inDQuotes; else if (char == '"') inDQuotes = inDQuotes ? false : !inSQuotes; if (char == ",") { if (inSQuotes) tmpParam += char; else if (inDQuotes) tmpParam += char; else { header_array.push(CSV_IO.cleanup(tmpParam)); tmpParam = ""; } } else tmpParam += char; } header_array.push(CSV_IO.cleanup(tmpParam)); } for (let i = 1; i < rows_array.length - 1; i++) { let json_obj = {}; let str = rows_array[i]; let s = ""; let flag = 0; for (let ch of str) { if (ch === '"' && flag === 0) { flag = 1; } else if (ch === '"' && flag == 1) flag = 0; if (ch === "," && flag === 0) ch = "|"; if (ch !== '"') s += ch; } let properties = s.split("|"); for (let j in header_array) { json_obj[header_array[j]] = properties[j]; } json_obj_array.push(json_obj); } let json = JSON.stringify(json_obj_array, null, 4); fs.writeFileSync(job_parameter.writePath, json); } static download_all_images_from_json(source_file, target_folder, url_property_name) { let rawdata = fs.readFileSync(source_file); let json = JSON.parse(rawdata.toString()); if (Array.isArray(json)) { json.forEach((element) => { if (url_property_name in element) { let url = element[url_property_name]; if (CSV_IO.is_valid_url(url)) { target_folder = target_folder.endsWith("/") ? target_folder : `${target_folder}/`; const name = url.substring(url.lastIndexOf("/") + 1) + ".jpg"; console.log(`Try Download from url: ${url}`); CSV_IO.download_image(url, `${target_folder}${name}`) .then(function (result) { element.Cover_Image = `${target_folder}${name}`; fs.writeFileSync(source_file, JSON.stringify(json, null, 4)); }) .catch(console.error); } else { console.log(`Download url invalid: ${url}`); } } }); } else { } } static download_image(url, filepath) { return new Promise((resolve, reject) => { let client = url.startsWith("https://") ? client_https : client_http; client.get(url, (response) => { if (response.statusCode === 200) { let write_stream = fs.createWriteStream(filepath); response .pipe(write_stream) .on("error", reject) .once("close", () => resolve(filepath)); } else { response.resume(); reject(new Error(`Download from ${url} Failed With Status Code: ${response.statusCode}`)); } }); }); } static is_valid_url_protocol(url) { try { const newUrl = new url_1.URL(url); return newUrl.protocol === "http:" || newUrl.protocol === "https:"; } catch (err) { return false; } } static is_valid_url(url) { try { const myURL = new url_1.URL(url); return true; } catch (error) { console.log(`${error.input} is not a valid url`); return false; } } static trim_char(str, ch) { var start = 0, end = str.length; while (start < end && str[start] === ch) ++start; while (end > start && str[end - 1] === ch) --end; return start > 0 || end < str.length ? str.substring(start, end) : str; } static cleanup(str) { str = str.replace(/[(),"-]/g, ''); str = str.replace(/[ ]/g, '_'); return str; } static imageToBase64(url, callback) { fetch(url) .then((response) => response.blob()) .then((blob) => { const reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = () => { const base64String = reader.result; callback(base64String); }; }); } } exports.CSV_IO = CSV_IO;