18 lines
2.4 KiB
JavaScript
18 lines
2.4 KiB
JavaScript
'use strict';
|
|
const fs=require('node:fs/promises');
|
|
const path=require('node:path');
|
|
const JSZip=require('jszip');
|
|
async function makePreviewFiles(root){
|
|
await fs.writeFile(path.join(root,'说明.txt'),'<script>window.previewExecuted=true</script>\n第二行原文');
|
|
await fs.writeFile(path.join(root,'gbk.txt'),Buffer.from([0xd6,0xd0,0xce,0xc4]));
|
|
await fs.writeFile(path.join(root,'sample.png'),Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+cZ/0AAAAASUVORK5CYII=','base64'));
|
|
await fs.writeFile(path.join(root,'bad.png'),'<html>this is not an image</html>');
|
|
await fs.writeFile(path.join(root,'old.doc'),'legacy office document');
|
|
await fs.writeFile(path.join(root,'unknown.bin'),Buffer.from([0,1,2,3]));
|
|
const doc=new JSZip();doc.file('[Content_Types].xml','<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>');doc.file('_rels/.rels','<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>');doc.file('word/document.xml','<?xml version="1.0" encoding="UTF-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>DOCX preview original text</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:bottom="1440" w:left="1440" w:right="1440"/></w:sectPr></w:body></w:document>');
|
|
await fs.writeFile(path.join(root,'sample.docx'),await doc.generateAsync({type:'nodebuffer',compression:'DEFLATE'}));
|
|
const ExcelJS=require('exceljs'),book=new ExcelJS.Workbook(),sheet=book.addWorksheet('原始数据');sheet.getCell('A1').value='Item';sheet.getCell('B1').value='Quantity';sheet.getCell('A2').value='<img src=x onerror=alert(1)>';sheet.getCell('B2').value=42;book.addWorksheet('第二页').getCell('A1').value='Second sheet';await book.xlsx.writeFile(path.join(root,'sample.xlsx'));
|
|
return {doc};
|
|
}
|
|
module.exports={makePreviewFiles};
|