-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1914 from ardriveapp/PE-7106-upload-custom-manifests
PE-7106: feat(custom manifest)
- Loading branch information
Showing
7 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
lib/services/arweave/graphql/queries/LicenseDataBundled.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:ardrive/utils/logger.dart'; | ||
import 'package:ardrive_io/ardrive_io.dart'; | ||
|
||
/// Checks if a file is an Arweave manifest file by examining its content type and contents. | ||
/// | ||
/// Returns true if the file has JSON content type and contains the string "arweave/paths", | ||
/// which indicates it follows the Arweave path manifest specification. | ||
Future<bool> isCustomManifest(IOFile file) async { | ||
try { | ||
if (file.contentType == 'application/json') { | ||
final fileLength = await file.length; | ||
|
||
int bytesToRead = 100; | ||
|
||
if (fileLength < bytesToRead) { | ||
bytesToRead = fileLength; | ||
} | ||
|
||
/// Read the first 100 bytes of the file | ||
final first100Bytes = file.openReadStream(0, bytesToRead); | ||
|
||
String content = ''; | ||
|
||
await for (var bytes in first100Bytes) { | ||
content += utf8.decode(bytes); | ||
} | ||
|
||
/// verify if file contains "arweave/paths" | ||
if (content.contains('arweave/paths')) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} catch (e) { | ||
logger.e('Error checking if file is a custom manifest', e); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:ardrive/utils/is_custom_manifest.dart'; | ||
import 'package:ardrive_io/ardrive_io.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class MockIOFile extends Mock implements IOFile {} | ||
|
||
void main() { | ||
late MockIOFile mockFile; | ||
|
||
setUp(() { | ||
mockFile = MockIOFile(); | ||
registerFallbackValue(0); | ||
registerFallbackValue(100); | ||
}); | ||
|
||
group('isCustomManifest', () { | ||
test('returns true when file is JSON and contains arweave/paths', () async { | ||
// Arrange | ||
const jsonContent = | ||
'{"manifest":"arweave/paths","version":"0.1.0","index":{"path":"hello_world.html"},"paths":{"hello_world.html":{"id":"KlwrMWFW9ckVKa8pCGk9a8EjwzYZ7jNVUVHdcE2YkHo"}}}'; | ||
final bytes = utf8.encode(jsonContent); | ||
|
||
when(() => mockFile.contentType).thenReturn('application/json'); | ||
when(() => mockFile.openReadStream(any(), any())) | ||
.thenAnswer((_) => Stream.value(Uint8List.fromList(bytes))); | ||
when(() => mockFile.length).thenReturn(bytes.length); | ||
// Act | ||
final result = await isCustomManifest(mockFile); | ||
|
||
// Assert | ||
expect(result, true); | ||
verify(() => mockFile.openReadStream(0, 100)).called(1); | ||
}); | ||
|
||
test('returns false when file is JSON but does not contain arweave/paths', | ||
() async { | ||
// Arrange | ||
const jsonContent = '{"version": 1, "type": "regular"}'; | ||
final bytes = utf8.encode(jsonContent); | ||
|
||
when(() => mockFile.contentType).thenReturn('application/json'); | ||
when(() => mockFile.openReadStream(any(), any())) | ||
.thenAnswer((_) => Stream.value(Uint8List.fromList(bytes))); | ||
when(() => mockFile.length).thenReturn(bytes.length); | ||
|
||
// Act | ||
final result = await isCustomManifest(mockFile); | ||
|
||
// Assert | ||
expect(result, false); | ||
verify(() => mockFile.openReadStream(0, 33)).called(1); | ||
}); | ||
|
||
test('returns false when file is not JSON', () async { | ||
// Arrange | ||
when(() => mockFile.contentType).thenReturn('text/plain'); | ||
when(() => mockFile.length).thenReturn(0); | ||
// Act | ||
final result = await isCustomManifest(mockFile); | ||
|
||
// Assert | ||
expect(result, false); | ||
verifyNever(() => mockFile.openReadStream(any(), any())); | ||
}); | ||
|
||
test('returns false when stream is empty', () async { | ||
// Arrange | ||
when(() => mockFile.contentType).thenReturn('application/json'); | ||
when(() => mockFile.openReadStream(any(), any())) | ||
.thenAnswer((_) => Stream.value(Uint8List(0))); | ||
when(() => mockFile.length).thenReturn(0); | ||
// Act | ||
final result = await isCustomManifest(mockFile); | ||
|
||
// Assert | ||
expect(result, false); | ||
verify(() => mockFile.openReadStream(0, 0)).called(1); | ||
}); | ||
}); | ||
} |