Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
e0ec2b0e
Commit
e0ec2b0e
authored
Mar 14, 2024
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix blob image parsing
parent
9278adb7
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
44 additions
and
11 deletions
+44
-11
guessDataType.ts
lib/blob/guessDataType.ts
+4
-1
removeNonSignificantZeroBytes.ts
lib/blob/removeNonSignificantZeroBytes.ts
+20
-0
bytesToBase64.ts
lib/bytesToBase64.ts
+10
-0
hexToBase64.ts
lib/hexToBase64.ts
+2
-8
BlobData.tsx
ui/blob/BlobData.tsx
+8
-2
No files found.
lib/blob/guessDataType.ts
View file @
e0ec2b0e
...
...
@@ -2,8 +2,11 @@ import filetype from 'magic-bytes.js';
import
hexToBytes
from
'
lib/hexToBytes
'
;
import
removeNonSignificantZeroBytes
from
'
./removeNonSignificantZeroBytes
'
;
export
default
function
guessDataType
(
data
:
string
)
{
const
bytes
=
new
Uint8Array
(
hexToBytes
(
data
));
const
filteredBytes
=
removeNonSignificantZeroBytes
(
bytes
);
return
filetype
(
b
ytes
)[
0
];
return
filetype
(
filteredB
ytes
)[
0
];
}
lib/blob/removeNonSignificantZeroBytes.ts
0 → 100644
View file @
e0ec2b0e
export
default
function
removeNonSignificantZeroBytes
(
bytes
:
Uint8Array
)
{
return
shouldRemoveBytes
(
bytes
)
?
bytes
.
filter
((
item
,
index
)
=>
index
%
32
)
:
bytes
;
}
// check if every 0, 32, 64, etc byte is 0 in the provided array
function
shouldRemoveBytes
(
bytes
:
Uint8Array
)
{
let
result
=
true
;
for
(
let
index
=
0
;
index
<
bytes
.
length
;
index
+=
32
)
{
const
element
=
bytes
[
index
];
if
(
element
===
0
)
{
continue
;
}
else
{
result
=
false
;
break
;
}
}
return
result
;
}
lib/bytesToBase64.ts
0 → 100644
View file @
e0ec2b0e
export
default
function
bytesToBase64
(
bytes
:
Uint8Array
)
{
let
binary
=
''
;
for
(
const
byte
of
bytes
)
{
binary
+=
String
.
fromCharCode
(
byte
);
}
const
base64String
=
btoa
(
binary
);
return
base64String
;
}
lib/hexToBase64.ts
View file @
e0ec2b0e
import
bytesToBase64
from
'
./bytesToBase64
'
;
import
hexToBytes
from
'
./hexToBytes
'
;
export
default
function
hexToBase64
(
hex
:
string
)
{
const
bytes
=
new
Uint8Array
(
hexToBytes
(
hex
));
let
binary
=
''
;
for
(
const
byte
of
bytes
)
{
binary
+=
String
.
fromCharCode
(
byte
);
}
const
base64String
=
btoa
(
binary
);
return
base64String
;
return
bytesToBase64
(
bytes
);
}
ui/blob/BlobData.tsx
View file @
e0ec2b0e
...
...
@@ -2,6 +2,8 @@ import { Flex, GridItem, Select, Skeleton, Button } from '@chakra-ui/react';
import
React
from
'
react
'
;
import
*
as
blobUtils
from
'
lib/blob
'
;
import
removeNonSignificantZeroBytes
from
'
lib/blob/removeNonSignificantZeroBytes
'
;
import
bytesToBase64
from
'
lib/bytesToBase64
'
;
import
downloadBlob
from
'
lib/downloadBlob
'
;
import
hexToBase64
from
'
lib/hexToBase64
'
;
import
hexToBytes
from
'
lib/hexToBytes
'
;
...
...
@@ -49,7 +51,8 @@ const BlobData = ({ data, isLoading, hash }: Props) => {
switch
(
format
)
{
case
'
Image
'
:
{
const
bytes
=
new
Uint8Array
(
hexToBytes
(
data
));
return
new
Blob
([
bytes
],
{
type
:
guessedType
?.
mime
});
const
filteredBytes
=
removeNonSignificantZeroBytes
(
bytes
);
return
new
Blob
([
filteredBytes
],
{
type
:
guessedType
?.
mime
});
}
case
'
UTF-8
'
:
{
return
new
Blob
([
hexToUtf8
(
data
)
],
{
type
:
guessedType
?.
mime
??
'
text/plain
'
});
...
...
@@ -74,7 +77,10 @@ const BlobData = ({ data, isLoading, hash }: Props) => {
return
<
RawDataSnippet
data=
"Not an image"
showCopy=
{
false
}
isLoading=
{
isLoading
}
/>;
}
const
base64
=
hexToBase64
(
data
);
const
bytes
=
new
Uint8Array
(
hexToBytes
(
data
));
const
filteredBytes
=
removeNonSignificantZeroBytes
(
bytes
);
const
base64
=
bytesToBase64
(
filteredBytes
);
const
imgSrc
=
`data:
${
guessedType
.
mime
}
;base64,
${
base64
}
`
;
return
<
BlobDataImage
src=
{
imgSrc
}
/>;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment