fix: raw body upload (no base64), remove form parsing, bump 1.0.20
Deploy loadtest / validate (push) Waiting to run
Deploy loadtest / validate (push) Waiting to run
This commit is contained in:
+4
-28
@@ -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}
|
||||
|
||||
|
||||
+35
-43
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user