File size: 2,328 Bytes
469a4d4 | 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 | // ================================================
// SGE β B2 Client
// Proxy: /b2proxy (Flask pe HuggingFace)
// ================================================
const PROXY = '/b2proxy';
async function proxyCall(action, path, extraHeaders = {}, body = null) {
const opts = {
method: 'POST',
headers: {
'X-Action': action,
'X-Path': path,
...extraHeaders
}
};
if (body) opts.body = body;
const res = await fetch(PROXY, opts);
if (!res.ok) {
const txt = await res.text();
throw new Error(txt || `err-027 β HTTP ${res.status}`);
}
return res.json();
}
// ββ UPLOAD (prin proxy Flask β fara CORS issues) ββ
async function b2Upload(file, path, onProgress) {
// Citeste fisierul
const buf = await file.arrayBuffer();
// SHA1 pentru integritate
const hash = await crypto.subtle.digest('SHA-1', buf);
const sha1 = Array.from(new Uint8Array(hash))
.map(b => b.toString(16).padStart(2, '0')).join('');
// Upload prin proxy (Flask il trimite mai departe la B2)
// Folosim XMLHttpRequest pentru progress
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', PROXY);
xhr.setRequestHeader('X-Action', 'upload');
xhr.setRequestHeader('X-Path', path);
xhr.setRequestHeader('X-Content-Type', file.type || 'application/octet-stream');
xhr.setRequestHeader('X-Sha1', sha1);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.upload.onprogress = e => {
if (e.lengthComputable && onProgress)
onProgress(Math.round(e.loaded / e.total * 100));
};
xhr.onload = () => {
if (xhr.status === 200) {
resolve(path);
} else {
reject(new Error(`err-009 β Upload esuat: ${xhr.status}`));
}
};
xhr.onerror = () => reject(new Error('err-027 β Eroare retea la upload'));
xhr.send(buf);
});
}
// ββ LIST ββ
async function b2List(prefix) {
try {
const data = await proxyCall('list', prefix);
return data.files || [];
} catch(e) {
console.error('b2List:', e.message);
return [];
}
}
// ββ DELETE ββ
async function b2Delete(key) {
try {
await proxyCall('delete', key);
} catch(e) {
console.error('b2Delete:', e.message);
}
}
|