From 0a76457efdb2893ff1733a2d87cb25ffdce4b935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Sat, 11 Jul 2026 09:53:21 +0400 Subject: [PATCH] fix: raw body upload (no base64), remove form parsing, bump 1.0.20 --- site/app.py | 32 +++---------------- site/static/app.js | 78 +++++++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 71 deletions(-) diff --git a/site/app.py b/site/app.py index 4fa6ba2..c3874b4 100644 --- a/site/app.py +++ b/site/app.py @@ -1,13 +1,12 @@ import time import json -import base64 from flask import Flask, render_template, request from flask_sock import Sock app = Flask(__name__) sock = Sock(app) -VERSION = '1.0.19' +VERSION = '1.0.20' @app.route('/') @@ -39,32 +38,9 @@ def ws_upload(ws): @app.route('/upload', methods=['POST']) def upload(): t0 = time.time() - total = 0 - # base64-режим: клиент отправляет поле "data" - data = request.form.get('data', '') - if data: - # убрать префикс data:...;base64, - if ',' in data: - data = data.split(',', 1)[1] - # form-encode превращает + в пробелы — восстановить - data = data.replace(' ', '+') - # добавить padding если потерялся - missing = len(data) % 4 - if missing: - data += '=' * (4 - missing) - raw = base64.b64decode(data) - total = len(raw) - # файловый режим - elif request.files: - for f in request.files.values(): - while True: - chunk = f.read(8192) - if not chunk: - break - total += len(chunk) - # raw-режим: тело как есть - else: - total = len(request.get_data()) + # raw-режим: тело как есть (клиент шлёт файл напрямую через xhr.send(file)) + raw = request.get_data() + total = len(raw) elapsed = round((time.time() - t0) * 1000) return {'ok': True, 'bytes': total, 'elapsed_ms': elapsed} diff --git a/site/static/app.js b/site/static/app.js index 00f8e00..5d0afd9 100644 --- a/site/static/app.js +++ b/site/static/app.js @@ -193,58 +193,50 @@ async function uploadAll() { fileSummary.textContent = summary; } -/** uploadFileHTTP(f, idx) — загрузить файл через HTTP POST (base64, с retry) */ +/** uploadFileHTTP(f, idx) — загрузить файл как raw body (без base64) */ function uploadFileHTTP(f, idx) { return new Promise(function (resolve, reject) { - var reader = new FileReader(); - reader.onload = function () { - var dataUrl = reader.result; - var startTime = Date.now(); - var RETRY = 3; + var startTime = Date.now(); + var RETRY = 3; - function doUpload(attempt) { - var xhr = new XMLHttpRequest(); - xhr.open('POST', '/upload'); - xhr.timeout = 300000; + function doUpload(attempt) { + var xhr = new XMLHttpRequest(); + xhr.open('POST', '/upload'); + xhr.timeout = 300000; + xhr.setRequestHeader('X-File-Name', encodeURIComponent(f.name)); + xhr.setRequestHeader('X-File-Size', f.size); - xhr.upload.onprogress = function (e) { - if (e.lengthComputable) { - f.progress = Math.round(e.loaded / e.total * 100); - renderTable(); - } - }; + xhr.upload.onprogress = function (e) { + if (e.lengthComputable) { + f.progress = Math.round(e.loaded / e.total * 100); + renderTable(); + } + }; - xhr.onload = function () { - try { - var r = JSON.parse(xhr.responseText); - resolve({ bytes: r.bytes, elapsed_ms: Date.now() - startTime }); - } catch (e) { - if (attempt < RETRY) { doUpload(attempt + 1); return; } - reject(new Error('Bad response')); - } - }; - - xhr.onerror = function () { + xhr.onload = function () { + try { + var r = JSON.parse(xhr.responseText); + resolve({ bytes: r.bytes, elapsed_ms: Date.now() - startTime }); + } catch (e) { if (attempt < RETRY) { doUpload(attempt + 1); return; } - reject(new Error('Network error')); - }; + reject(new Error('Bad response')); + } + }; - xhr.ontimeout = function () { - if (attempt < RETRY) { doUpload(attempt + 1); return; } - reject(new Error('Timeout')); - }; + xhr.onerror = function () { + if (attempt < RETRY) { doUpload(attempt + 1); return; } + reject(new Error('Network error')); + }; - var form = new FormData(); - form.append('data', dataUrl); - form.append('name', f.name); - form.append('orig_size', f.size); - xhr.send(form); - } + xhr.ontimeout = function () { + if (attempt < RETRY) { doUpload(attempt + 1); return; } + reject(new Error('Timeout')); + }; - doUpload(1); - }; - reader.onerror = function () { reject(new Error('Read error')); }; - reader.readAsDataURL(f.file); + xhr.send(f.file); + } + + doUpload(1); }); }