mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2025-07-23 23:11:04 -04:00
Show file name and size on download page
This commit is contained in:
parent
409af9b991
commit
d01c37c59d
42 changed files with 373 additions and 3 deletions
|
@ -591,6 +591,32 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
|||
return expirationDate;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert Bytes to KB/MB/GB
|
||||
*
|
||||
* @name Helper.formatBytes
|
||||
* @function
|
||||
*
|
||||
* @param {number} bytes
|
||||
* @return {string}
|
||||
*/
|
||||
me.formatBytes = function (bytes)
|
||||
{
|
||||
let result = '';
|
||||
const kilobyte = 1000;
|
||||
const decimalPoint = 2;
|
||||
const sizes = [I18n._('B'), I18n._('KB'), I18n._('MB'), I18n._('GB')];
|
||||
const index = Math.floor(Math.log(bytes) / Math.log(kilobyte));
|
||||
|
||||
if (bytes > 0) {
|
||||
result = parseFloat((bytes / Math.pow(kilobyte, index)).toFixed(decimalPoint)) + ' ' + sizes[index];
|
||||
} else {
|
||||
result = `0 ${I18n._('B')}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* resets state, used for unit testing
|
||||
*
|
||||
|
@ -3000,7 +3026,9 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
|||
|
||||
if (typeof fileName !== 'undefined') {
|
||||
attachmentLink.attr('download', fileName);
|
||||
template.append(fileName);
|
||||
|
||||
const fileSize = me.getAttachmentSize(attachmentData);
|
||||
template.append(`(${fileName}, ${fileSize})`);
|
||||
}
|
||||
|
||||
// sanitize SVG preview
|
||||
|
@ -3202,6 +3230,25 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
|||
return attachmentData.substring(5, mimeTypeEnd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attachment size in B/KB/MB/GB
|
||||
*
|
||||
* @name AttachmentViewer.getAttachmentSize
|
||||
* @function
|
||||
* @param {string} attachmentData - Base64 string
|
||||
*/
|
||||
me.getAttachmentSize = function(attachmentData)
|
||||
{
|
||||
const base64Start = attachmentData.indexOf(',') + 1;
|
||||
const rawData = attachmentData.substring(base64Start);
|
||||
const decodedData = rawData.length > 0 ? atob(rawData) : '';
|
||||
|
||||
const buf = new Uint8Array(decodedData.length);
|
||||
const bytes = new Blob([buf]).size;
|
||||
|
||||
return Helper.formatBytes(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* moves the attachment link to another element
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue