From 792b481a09add36ee3f383d0ff4d6570075f4b92 Mon Sep 17 00:00:00 2001 From: zelfroster Date: Fri, 11 Aug 2023 00:38:09 +0530 Subject: [PATCH] chore: remove makeFriendlyUnit and move formatBytes to rswebui for project wide use, create separate file for file_proxy --- webui-src/app/channels/channel_view.js | 2 +- webui-src/app/channels/channels_util.js | 12 ------- webui-src/app/files/files_downloads.js | 1 - webui-src/app/files/files_proxy.js | 10 ++++++ webui-src/app/files/files_search.js | 11 +++--- webui-src/app/files/files_util.js | 47 +++++-------------------- webui-src/app/files/friends_files.js | 2 +- webui-src/app/files/my_files.js | 2 +- webui-src/app/rswebui.js | 18 +++++++--- 9 files changed, 42 insertions(+), 63 deletions(-) create mode 100644 webui-src/app/files/files_proxy.js diff --git a/webui-src/app/channels/channel_view.js b/webui-src/app/channels/channel_view.js index 2b050d2..81adda2 100644 --- a/webui-src/app/channels/channel_view.js +++ b/webui-src/app/channels/channel_view.js @@ -723,7 +723,7 @@ const PostView = () => { post.mFiles.map((file) => m('tr', [ m('td', file.mName), - m('td', util.formatbytes(file.mSize.xint64)), + m('td', rs.formatBytes(file.mSize.xint64)), m( 'button', { diff --git a/webui-src/app/channels/channels_util.js b/webui-src/app/channels/channels_util.js index 0d74adf..9e272d8 100644 --- a/webui-src/app/channels/channels_util.js +++ b/webui-src/app/channels/channels_util.js @@ -180,19 +180,8 @@ const FilesTable = () => { }; }; -function formatbytes(bytes, decimals = 2) { - // takes in size returns a string (size)(kb/gb/tb) - if (bytes === 0) return '0 Bytes'; - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; -} - const ChannelTable = () => { return { - oninit: (v) => {}, view: (v) => m('table.channels', [m('tr', [m('th', 'Channel Name')]), v.children]), }; }; @@ -239,7 +228,6 @@ module.exports = { Data, SearchBar, ChannelSummary, - formatbytes, DisplayChannelsFromList, updatedisplaychannels, ChannelTable, diff --git a/webui-src/app/files/files_downloads.js b/webui-src/app/files/files_downloads.js index e64343b..31a9bf3 100644 --- a/webui-src/app/files/files_downloads.js +++ b/webui-src/app/files/files_downloads.js @@ -166,7 +166,6 @@ const Component = () => { direction: 'down', transferred: Downloads.statusMap[hash].transfered.xint64, chunksInfo: Downloads.chunksMap[hash], - rsJsonApiRequest: rs.rsJsonApiRequest, }) ), ]), diff --git a/webui-src/app/files/files_proxy.js b/webui-src/app/files/files_proxy.js new file mode 100644 index 0000000..3528085 --- /dev/null +++ b/webui-src/app/files/files_proxy.js @@ -0,0 +1,10 @@ +const m = require('mithril'); +const futil = require('files/files_util'); + +const fileProxyObj = futil.createProxy({}, () => { + m.redraw(); +}); + +module.exports = { + fileProxyObj, +}; diff --git a/webui-src/app/files/files_search.js b/webui-src/app/files/files_search.js index 9d9fd64..d774d2d 100644 --- a/webui-src/app/files/files_search.js +++ b/webui-src/app/files/files_search.js @@ -1,6 +1,7 @@ const m = require('mithril'); const rs = require('rswebui'); const futil = require('files/files_util'); +const fproxy = require('files/files_proxy'); const widget = require('widgets'); let matchString = ''; @@ -66,7 +67,7 @@ const Layout = () => { ), ]), m('div.file-search-container__results', [ - Object.keys(futil.proxyObj).length === 0 + Object.keys(fproxy.fileProxyObj).length === 0 ? m('h5.bold', 'Results') : m('table.results-container', [ m( @@ -80,13 +81,13 @@ const Layout = () => { ), m( 'tbody.results', - futil.proxyObj[currentItem.slice(1)] === undefined && - futil.proxyObj[currentItem.slice(1)].length === 0 + fproxy.fileProxyObj[currentItem.slice(1)] === undefined && + fproxy.fileProxyObj[currentItem.slice(1)].length === 0 ? 'Fetching Results...' - : futil.proxyObj[currentItem.slice(1)].map((item) => + : fproxy.fileProxyObj[currentItem.slice(1)].map((item) => m('tr', [ m('td.results__name', [m('i.fas.fa-file'), m('span', item.fName)]), - m('td.results__size', futil.makeFriendlyUnit(item.fSize.xint64)), + m('td.results__size', rs.formatBytes(item.fSize.xint64)), m('td.results__hash', item.fHash), m( 'td.results__download', diff --git a/webui-src/app/files/files_util.js b/webui-src/app/files/files_util.js index 3b64f43..5e8b7c8 100644 --- a/webui-src/app/files/files_util.js +++ b/webui-src/app/files/files_util.js @@ -51,22 +51,6 @@ const createProxy = (obj, onChange) => { }); }; -const proxyObj = createProxy({}, () => { - m.redraw(); -}); - -function makeFriendlyUnit(bytes) { - let cnt = bytes; - for (const s of ['', 'k', 'M', 'G']) { - if (cnt < 1000) { - return cnt.toFixed(1) + ' ' + s + 'B'; - } else { - cnt = cnt / 1024; - } - } - return cnt.toFixed(1) + 'TB'; -} - function calcRemainingTime(bytes, rate) { if (rate <= 0 || bytes < 1) { return '--'; @@ -91,7 +75,7 @@ function calcRemainingTime(bytes, rate) { } } -function fileAction(rsJsonApiRequest, hash, action) { +function fileAction(hash, action) { const jsonParams = { hash, flags: 0, @@ -113,7 +97,7 @@ function fileAction(rsJsonApiRequest, hash, action) { console.error('Unknown action in Downloads.control()'); return; } - rsJsonApiRequest('/rsFiles/FileControl', jsonParams); + rs.rsJsonApiRequest('/rsFiles/FileControl', jsonParams); } const ProgressBar = () => { @@ -128,7 +112,6 @@ const ProgressBar = () => { const File = () => { let chunkStrat; - let rsJsonApiRequest = () => {}; const chunkStrats = { // rstypes.h :: 366 0: 'Streaming', // CHUNK_STRATEGY_STREAMING @@ -136,7 +119,7 @@ const File = () => { 2: 'Progressive', // CHUNK_STRATEGY_PROGRESSIVE }; function fileCancel(hash) { - rsJsonApiRequest('/rsFiles/FileCancel', { hash }).then((res) => + rs.rsJsonApiRequest('/rsFiles/FileCancel', { hash }).then((res) => widget.popupMessage(m('p', `Download Cancel ${res ? 'Successful' : 'Failed'}`)) ); } @@ -149,7 +132,7 @@ const File = () => { function actionButton(file, action) { return m( 'button', - { title: action, onclick: () => fileAction(rsJsonApiRequest, file.hash, action) }, + { title: action, onclick: () => fileAction(file.hash, action) }, m(`i.fas.fa-${action === 'resume' ? 'play' : action}`) ); } @@ -157,14 +140,12 @@ const File = () => { return { oninit: async (v) => { chunkStrat = await v.attrs.strategy; - // TODO: rs from rswebui is undefined for some reason, need to be checked - rsJsonApiRequest = await v.attrs.rsJsonApiRequest; }, view: (v) => { const { info, direction, transferred, chunksInfo } = v.attrs; function changeChunkStrategy(e) { chunkStrat = e.target.selectedIndex; - rsJsonApiRequest('/rsFiles/setChunkStrategy', { + rs.rsJsonApiRequest('/rsFiles/setChunkStrategy', { hash: info.hash, newStrategy: chunkStrat, }); @@ -194,15 +175,15 @@ const File = () => { m('.file-view__body-details-stat', [ m('span', { title: 'downloaded size' }, [ m('i.fas.fa-download'), - makeFriendlyUnit(transferred), + rs.formatBytes(transferred), ]), m('span', { title: 'total size' }, [ m('i.fas.fa-file'), - makeFriendlyUnit(info.size.xint64), + rs.formatBytes(info.size.xint64), ]), m('span', { title: 'speed' }, [ m(`i.fas.fa-arrow-circle-${direction}`), - `${makeFriendlyUnit(info.tfRate * 1024)}/s`, + `${rs.formatBytes(info.tfRate * 1024)}/s`, ]), direction === 'down' && m('span', { title: 'time remaining' }, [ @@ -277,14 +258,6 @@ const FriendsFilesTable = () => { ]), }; }; -function formatbytes(bytes, decimals = 2) { - if (bytes === 0) return '0 Bytes'; - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; -} module.exports = { RS_FILE_CTRL_PAUSE, @@ -301,12 +274,10 @@ module.exports = { RS_FILE_REQ_ANONYMOUS_ROUTING, RS_FILE_HINTS_REMOTE, RS_FILE_HINTS_LOCAL, - makeFriendlyUnit, File, SearchBar, compareArrays, MyFilesTable, FriendsFilesTable, - formatbytes, - proxyObj, + createProxy, }; diff --git a/webui-src/app/files/friends_files.js b/webui-src/app/files/friends_files.js index d4dbbae..3eb9af2 100644 --- a/webui-src/app/files/friends_files.js +++ b/webui-src/app/files/friends_files.js @@ -73,7 +73,7 @@ function displayfiles() { ? nameOfId + ' (' + parStruct.details.name.slice(0, 8) + '...)' : parStruct.details.name ), - m('td', util.formatbytes(parStruct.details.size.xint64)), + m('td', rs.formatBytes(parStruct.details.size.xint64)), isFile && m( 'td', diff --git a/webui-src/app/files/my_files.js b/webui-src/app/files/my_files.js index fef66a1..201d563 100644 --- a/webui-src/app/files/my_files.js +++ b/webui-src/app/files/my_files.js @@ -48,7 +48,7 @@ function displayfiles() { }, parStruct.details.name ), - m('td', util.formatbytes(parStruct.details.size.xint64)), + m('td', rs.formatBytes(parStruct.details.size.xint64)), ]), parStruct.showChild && childrenList.map((child) => diff --git a/webui-src/app/rswebui.js b/webui-src/app/rswebui.js index 89f4528..1aeb7a8 100644 --- a/webui-src/app/rswebui.js +++ b/webui-src/app/rswebui.js @@ -1,5 +1,5 @@ const m = require('mithril'); -const futil = require('files/files_util'); +const fproxy = require('files/files_proxy'); const RsEventsType = { NONE: 0, // Used internally to detect invalid event type passed @@ -175,11 +175,11 @@ const eventQueue = { console.log('search results : ', event); // if request item doesn't already exists in Object then create new item - if (!Object.prototype.hasOwnProperty.call(futil.proxyObj, event.mRequestId)) { - futil.proxyObj[event.mRequestId] = []; + if (!Object.prototype.hasOwnProperty.call(fproxy.fileProxyObj, event.mRequestId)) { + fproxy.fileProxyObj[event.mRequestId] = []; } - futil.proxyObj[event.mRequestId].push(...event.mResults); + fproxy.fileProxyObj[event.mRequestId].push(...event.mResults); }, }, [RsEventsType.CHAT_MESSAGE]: { @@ -336,6 +336,15 @@ function logon(loginHeader, displayAuthError, displayErrorMessage, successful) { }); } +function formatBytes(bytes, decimals = 2) { + if (bytes === 0) return '0 Bytes'; + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; +} + module.exports = { rsJsonApiRequest, setKeys, @@ -344,4 +353,5 @@ module.exports = { events: eventQueue.events, userList, loginKey, + formatBytes, };