1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| const http = require("http"); const fs = require("fs"); const url = require("url");
const tempDir = "./imgs";
Buffer.prototype.split = function (sep) { let sepLength = sep.length, arr = [], offset = 0, currentIndex = 0; while ((currentIndex = this.indexOf(sep, offset)) !== -1) { arr.push(this.slice(offset, currentIndex)); offset = currentIndex + sepLength; } arr.push(this.slice(offset)); return arr; };
function handlePart(part) { const [head, body] = part.split("\r\n\r\n"); const headStr = head.toString(); const key = headStr.match(/name="(.+?)"/)[1]; const match = headStr.match(/filename="(.+?)"/); if (!match) { const value = body.toString().slice(0, -2); return { key, value }; } const filename = match[1]; return { key, value: filename === "blob" ? body : filename }; }
const pipeStream = (path, writeStream) => new Promise((resolve) => { const readStream = fs.createReadStream(path); readStream.on("end", () => { fs.unlinkSync(path); resolve(); }); readStream.pipe(writeStream); });
const server = http.createServer((req, res) => { const uri = url.parse(req.url, true);
if (uri.pathname === "/" && req.method === "GET") { res.writeHead(200, { "Content-Type": "text-plain" }); res.end(fs.readFileSync("./index.html")); return; } if (uri.pathname === "/check_uploaded" && req.method === "GET") { if (!uri.query.md5code) { res.end("没有md5code"); return; } const hasSplitFile = fs .readdirSync(tempDir) .some((f) => f.includes(uri.query.md5code)); res.end( JSON.stringify( hasSplitFile ? { code: 200, data: "还未上传完成" } : { code: 404, data: "该文件已上传完成" } ) ); return; } if (uri.pathname === "/big_files" && req.method === "POST") { let currentBuffer;
req.on("data", (data) => { currentBuffer = data; }); req.on("end", () => { const contentType = req.headers["content-type"]; const headBoundary = contentType.slice(contentType.lastIndexOf("=") + 1); const bodyBoundary = `--${headBoundary}`;
const obj = {}; const parts = Buffer.concat([currentBuffer]) .split(bodyBoundary) .slice(1, -1); for (let i = 0; i < parts.length; i++) { const { key, value } = handlePart(parts[i]); obj[key] = value; }
const exist = fs.existsSync(tempDir); if (!exist) { fs.mkdirSync(tempDir); } fs.writeFileSync(`${tempDir}/${obj.hash}`, obj.file);
res.end("ok"); }); return; } if (uri.pathname === "/big_files_merge" && req.method === "POST") { const size = 10 * 1024; const postData = {}; req.on("data", (data) => { Object.assign(postData, JSON.parse(data.toString())); }); req.on("end", () => { const files = fs.readdirSync(tempDir); const fileGroup = files .filter((filename) => filename.includes(postData.hashname)) .sort((a, b) => a.split(`_`)[1] - b.split(`_`)[1]); const streams = fileGroup.map((filename, index) => pipeStream( `${tempDir}/${filename}`, fs.createWriteStream(`./${postData.filename}`, { start: index * size, }) ) ); Promise.all(streams).then(() => { fs.rmdirSync(tempDir); fs.readFile(postData.filename, (err, buffer) => { if (err) return; res.end(buffer); }); }); }); return; } });
server.listen(5500);
|