Skip to content

Commit

Permalink
Skip IO errors in web frontend on non-relative URIs (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
lexaknyazev committed Aug 15, 2024
1 parent fa9aaa3 commit a3628b1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/cmd_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,12 @@ ResourcesLoader getFileResourceValidator(
// GLB-stored buffer
return readerResult.buffer;
}
if (isNonRelativeUri(uri)) {
if (uri.isNonRelative) {
return null;
}
return fileGuarded(uri).readAsBytes();
}, externalStreamFetch: (uri) {
if (isNonRelativeUri(uri)) {
if (uri.isNonRelative) {
return null;
}
return fileGuarded(uri).openRead();
Expand Down
14 changes: 6 additions & 8 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ String getString(Map<String, Object> map, String name, Context context,
Uri getUri(String uriString, Context context) {
try {
final uri = Uri.parse(uriString);
if (isNonRelativeUri(uri)) {
if (uri.isNonRelative) {
context
.addIssue(SemanticError.nonRelativeUri, name: URI, args: [uriString]);
}
Expand Down Expand Up @@ -727,13 +727,6 @@ bool isTrsDecomposable(Matrix4 matrix) {

bool isPot(int value) => (value != 0) && (value & (value - 1) == 0);

bool isNonRelativeUri(Uri uri) =>
uri.hasScheme ||
uri.hasAuthority ||
uri.hasAbsolutePath ||
uri.hasQuery ||
uri.hasFragment;

int padLength(int length) => length + (-length & 3);

List<int> createTypedIntList(int type, int length) {
Expand Down Expand Up @@ -766,6 +759,11 @@ extension Vector3IsOneOrZero on Vector3 {
bool get isZero => x == 0 && y == 0 && z == 0;
}

extension IsNonRelativeUri on Uri {
bool get isNonRelative =>
hasScheme || hasAuthority || hasAbsolutePath || hasQuery || hasFragment;
}

abstract class ElementChecker<T extends num> {
const ElementChecker();
String get path => '';
Expand Down
6 changes: 3 additions & 3 deletions test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'dart:io';

import 'package:gltf/gltf.dart';
import 'package:gltf/src/errors.dart';
import 'package:gltf/src/utils.dart' show isNonRelativeUri;
import 'package:gltf/src/utils.dart' show IsNonRelativeUri;
import 'package:test/test.dart';

Future compareReports(String basePath) async {
Expand Down Expand Up @@ -73,7 +73,7 @@ ResourcesLoader _getFileResourceValidator(
// GLB-stored buffer
return readerResult.buffer;
}
if (isNonRelativeUri(uri)) {
if (uri.isNonRelative) {
return null;
}

Expand All @@ -83,7 +83,7 @@ ResourcesLoader _getFileResourceValidator(
throw GltfExternalResourceNotFoundException(uri.toString());
});
}, externalStreamFetch: (uri) {
if (isNonRelativeUri(uri)) {
if (uri.isNonRelative) {
return null;
}
// TODO: refactor resource loader to remove this
Expand Down
7 changes: 7 additions & 0 deletions web/scripts/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'dart:math';

import 'dart:typed_data';
import 'package:gltf/gltf.dart';
import 'package:gltf/src/utils.dart';

const _kChunkSize = 1024 * 1024;
const _kMaxReportLength = 512 * 1024;
Expand Down Expand Up @@ -143,6 +144,9 @@ Future<ValidationResult> _doValidate(List<File> files) async {
final resourcesLoader = ResourcesLoader(context, readerResult.gltf,
externalBytesFetch: ([uri]) {
if (uri != null) {
if (uri.isNonRelative) {
return null;
}
final file = _getFileByUri(files, uri);
if (file != null) {
return _getFile(file);
Expand All @@ -154,6 +158,9 @@ Future<ValidationResult> _doValidate(List<File> files) async {
}
}, externalStreamFetch: (uri) {
if (uri != null) {
if (uri.isNonRelative) {
return null;
}
final file = _getFileByUri(files, uri);
if (file != null) {
return _getFileStream(file);
Expand Down

0 comments on commit a3628b1

Please sign in to comment.