Skip to content

Commit 32603e2

Browse files
committed
get the first data byte
1 parent fb54c0a commit 32603e2

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/jsonUtils/sketchImpl/makeImageDataFromUrl.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ export default function makeImageDataFromUrl(url?: string) {
22
let fetchedData = url ? NSData.dataWithContentsOfURL(NSURL.URLWithString(url)) : undefined;
33

44
if (fetchedData) {
5-
const firstByte = fetchedData.subdataWithRange(NSMakeRange(0, 1)).description();
5+
const firstByte = String(
6+
NSString.alloc().initWithData_encoding(fetchedData, NSISOLatin1StringEncoding),
7+
).charCodeAt(0);
68

7-
// Check for first byte. Must use non-type-exact matching (!=).
9+
// Check for first byte to see if we have an image.
810
// 0xFF = JPEG, 0x89 = PNG, 0x47 = GIF, 0x49 = TIFF, 0x4D = TIFF
911
if (
10-
/* eslint-disable eqeqeq */
11-
firstByte != '<ff>' &&
12-
firstByte != '<89>' &&
13-
firstByte != '<47>' &&
14-
firstByte != '<49>' &&
15-
firstByte != '<4d>'
16-
/* eslint-enable eqeqeq */
12+
firstByte !== 0xff &&
13+
firstByte !== 0x89 &&
14+
firstByte !== 0x47 &&
15+
firstByte !== 0x49 &&
16+
firstByte !== 0x4d
1717
) {
1818
fetchedData = null;
1919
}
2020
}
2121

22-
let image;
22+
let image: any;
2323

2424
if (!fetchedData) {
2525
const errorUrl =

src/types/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ declare const MSJSONDictionaryUnarchiver: any;
3131
declare const MSJSONDataArchiver: any;
3232
declare const NSMakeRect: any;
3333
declare const MSLayerGroup: any;
34+
declare const NSISOLatin1StringEncoding: any;

0 commit comments

Comments
 (0)