Skip to content

Commit

Permalink
Fixed rendering b64 total pieces decoding (#8181)
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink authored Sep 27, 2024
2 parents e78b900 + 9ed5619 commit c04b328
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/tribler/ui/src/pages/Downloads/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function DownloadDetails({ selectedDownloads }: { selectedDownloa
<div className="flex flex-col h-full p-4 text-sm">
<div className="flex flex-row">
<div className="basis-1/4">{t('Progress')}</div>
<div className="basis-3/4 m-auto"><Pieces pieces64={download.pieces || ''} /></div>
<div className="basis-3/4 m-auto"><Pieces numpieces={download.total_pieces} pieces64={download.pieces || ''} /></div>
</div>
<div className="flex flex-row">
<div className="basis-1/4">{t('Name')}</div>
Expand Down
14 changes: 7 additions & 7 deletions src/tribler/ui/src/pages/Downloads/Pieces.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useEffect, useRef } from 'react'


export default function Pieces({ pieces64 }: { pieces64: string }) {
export default function Pieces({ pieces64, numpieces }: { pieces64: string, numpieces: number }) {
const ref = useRef<HTMLCanvasElement>(null)

const convertPieces = (pieces64: string) => {
const convertPieces = (pieces64: string, numpieces: number) => {
if (pieces64 === undefined) { return [] }

// Remove trailing '='
pieces64 = pieces64.replace(/\=+$/, '');

const pieces = [];
const pieceString = atob(pieces64);
for (let i = 0; i < pieceString.length; ++i) {
for (let i = 0; i < Math.min(numpieces, pieceString.length); ++i) {
const pieceNumber = pieceString[i].charCodeAt(0);
for (let j = 8 - 1; j >= 0; --j) {
pieces.push(pieceNumber & 1 << j ? 1 : 0);
Expand All @@ -23,14 +23,14 @@ export default function Pieces({ pieces64 }: { pieces64: string }) {

useEffect(() => {
if (ref.current) {
const canvas = ref.current.getContext('2d')
const pieces = convertPieces(pieces64)
const canvas = ref.current.getContext('2d');
const pieces = convertPieces(pieces64, numpieces);
if (!canvas || !pieces || pieces.length === 0) { return; }

// Get size from the HTML canvas element
const width = canvas.canvas.width;
const height = canvas.canvas.height;
const numPieces = pieces.length;
const numPieces = numpieces;

if (numPieces <= width) {
const pieceWidth = width / numPieces;
Expand All @@ -55,7 +55,7 @@ export default function Pieces({ pieces64 }: { pieces64: string }) {
}
}
}
}, [pieces64])
}, [pieces64, numpieces])

return <canvas ref={ref} style={{ height: '20px', width: '97%', background: 'white', border: '1px solid #2f2f2f' }} />
}
Expand Down

0 comments on commit c04b328

Please sign in to comment.