/
memoryspeak
/
MemorySpeakWebClient
Обзор
Документация
Войти
/
memoryspeak
/
MemorySpeakWebClient
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
static/js/Utils.js
194 строки
5 KB
memoryspeak
initial commit: project structure
20 ноя 2025, 21:54
20 ноя 2025, 21:54
9ddd280
Код
Авторство
О чём код?
class Utils { static translator = { 'en': { zeroBytes: '0 Bytes', bytes: 'Bytes', kb: 'KB', mb: 'MB' }, 'ru': { zeroBytes: '0 Байт', bytes: 'Байт', kb: 'КБ', mb: 'МБ' } }; static hasChildNode(parent, child) { if (parent.children) { for (let i = 0; i < parent.children.length; i++) { if (parent.children[i] === child) { return true; }; }; }; return false; }; static getUniqueArray(array) { let seen = {}; let out = []; let j = 0; for (let i = 0; i < array.length; i++) { let item = array[i]; if (seen[item] !== 1 && item != '') { seen[item] = 1; out[j++] = item; }; }; return out; }; static getBase64FromFile(file) { return new Promise(resolve => { let reader = new FileReader(); reader.onload = function(event) { let data = event.target.result; resolve(data); }; reader.readAsDataURL(file); }); }; static getFileFromBase64(base64, filename) { let mime = base64.split(',')[0].match(/:(.*?);/)[1]; let bstr = atob(base64.split(',')[1]); let n = bstr.length; let u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); }; return new File([u8arr], filename, { type: mime }); }; static getExtention(filename) { return filename.slice((Math.max(0, filename.lastIndexOf(".")) || Infinity) + 1); }; static formatBytes(bytes, decimals = 2) { if (bytes == 0) return this.translator[SettingsPage.language].zeroBytes; const k = 1024; const dm = decimals < 0 ? 0 : decimals; const sizes = [this.translator[SettingsPage.language].bytes, this.translator[SettingsPage.language].kb, this.translator[SettingsPage.language].mb]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; }; static getTagsArray(inputElement) { let tags = inputElement.value.split("#"); for (let i = 0; i < tags.length; i++) { tags[i] = tags[i].trim(); }; tags = this.getUniqueArray(tags); return tags; }; static async getFilesArray(inputElement) { let files = []; for (let i = 0; i < inputElement.files.length; i++) { const inputFile = inputElement.files[i]; let file = {}; file.base64 = await this.getBase64FromFile(inputFile); file.filename = inputFile.name; file.size = inputFile.size; file.type = inputFile.type; file.extention = this.getExtention(inputFile.name); files.push(file); }; return files; }; static deleteFile(fileId, username, md5password, onSuccess) { fetch(Strings.apiUrl, { method: 'POST', headers: {'Content-Type': 'application/json;charset=utf-8'}, body: JSON.stringify({ method: 'deletefile', username: username, md5password: md5password, file_id: fileId }) }) .then(response => response.json()) .then(onSuccess); }; static getJsonFileSize(fileId, notesId, username, md5password, onSuccess) { fetch(Strings.apiUrl, { method: 'POST', headers: {'Content-Type': 'application/json;charset=utf-8'}, body: JSON.stringify({ method: 'getjsonsizefile', username: username, md5password: md5password, file_id: fileId, notes_id: notesId }) }) .then(response => response.json()) .then(onSuccess); }; static getFile(fileId, notesId, username, md5password, onSuccess) { fetch(Strings.apiUrl, { method: 'POST', headers: {'Content-Type': 'application/json;charset=utf-8'}, body: JSON.stringify({ method: 'getfile', username: username, md5password: md5password, file_id: fileId, notes_id: notesId }) }) .then(onSuccess); }; static async showFileDownloadProcess(response, jsonSize, fileSize, filename, filenameLabel) { const reader = response.body.getReader(); const contentLength = jsonSize; let receivedLength = 0; let chunks = []; while (true) { const {done, value} = await reader.read(); if (done) break; chunks.push(value); receivedLength += value.length; filenameLabel.innerHTML = `${filename} <span class="text-muted">(${this.formatBytes(receivedLength*fileSize/contentLength)})</span>`; }; let chunksAll = new Uint8Array(receivedLength); let position = 0; for (let chunk of chunks) { chunksAll.set(chunk, position); position += chunk.length; }; let decodeAllChanks = new TextDecoder("utf-8").decode(chunksAll); return decodeAllChanks; }; static setOnClickListenerDropDownContent(button, content) { button.addEventListener('click', function(event) { event.preventDefault(); if (content.style.display == "block") { content.style.display = "none"; return; }; if (content.style.display == "none") { content.style.display = "block"; return; }; }.bind(this)); }; static getSystemLanguage() { const lang = navigator.language || navigator.userLanguage; return lang.split('-')[0]; // Возвращает только код языка (например "ru") }; };