chore: remove makeFriendlyUnit and move formatBytes to rswebui for project wide use, create separate file for file_proxy

This commit is contained in:
zelfroster 2023-08-11 00:38:09 +05:30
parent 2e646f3c33
commit 792b481a09
9 changed files with 42 additions and 63 deletions

View File

@ -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',
{

View File

@ -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,

View File

@ -166,7 +166,6 @@ const Component = () => {
direction: 'down',
transferred: Downloads.statusMap[hash].transfered.xint64,
chunksInfo: Downloads.chunksMap[hash],
rsJsonApiRequest: rs.rsJsonApiRequest,
})
),
]),

View File

@ -0,0 +1,10 @@
const m = require('mithril');
const futil = require('files/files_util');
const fileProxyObj = futil.createProxy({}, () => {
m.redraw();
});
module.exports = {
fileProxyObj,
};

View File

@ -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',

View File

@ -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,
};

View File

@ -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',

View File

@ -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) =>

View File

@ -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,
};