Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed rendering b64 total pieces decoding #8181

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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