|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:typed_data'; |
| 7 | + |
| 8 | +import 'file_system/platform_file_system_cross_directory.dart'; |
| 9 | +import 'file_system/platform_file_system_cross_file.dart'; |
| 10 | +import 'platform_cross_directory.dart'; |
| 11 | +import 'platform_cross_entity.dart'; |
| 12 | +import 'scoped_storage/platform_scoped_storage_cross_directory.dart'; |
| 13 | +import 'scoped_storage/platform_scoped_storage_cross_file.dart'; |
| 14 | + |
| 15 | +/// Interface for a platform implementation of `cross_file`. |
| 16 | +abstract base class CrossFilePlatform { |
| 17 | + /// The instance of [CrossFilePlatform] to be used. |
| 18 | + /// |
| 19 | + /// Platform implementations packages should set this with their own |
| 20 | + /// implementation of [CrossFilePlatform] when they register themselves. |
| 21 | + static CrossFilePlatform? instance; |
| 22 | + |
| 23 | + /// Creates a new [PlatformFileSystemXFile]. |
| 24 | + PlatformFileSystemXFile createPlatformFileSystemXFile( |
| 25 | + PlatformFileSystemXFileCreationParams params, |
| 26 | + ) { |
| 27 | + return _DefaultFileSystemXFile(params); |
| 28 | + } |
| 29 | + |
| 30 | + /// Creates a new [PlatformFileSystemXDirectory]. |
| 31 | + PlatformFileSystemXDirectory createPlatformFileSystemXDirectory( |
| 32 | + PlatformFileSystemXDirectoryCreationParams params, |
| 33 | + ) { |
| 34 | + return _DefaultFileSystemXDirectory(params); |
| 35 | + } |
| 36 | + |
| 37 | + /// Creates a new [PlatformScopedStorageXFile]. |
| 38 | + PlatformScopedStorageXFile createPlatformScopedStorageXFile( |
| 39 | + PlatformScopedStorageXFileCreationParams params, |
| 40 | + ) { |
| 41 | + return _DefaultScopedStorageXFile(params); |
| 42 | + } |
| 43 | + |
| 44 | + /// Creates a new [PlatformScopedStorageXDirectory]. |
| 45 | + PlatformScopedStorageXDirectory createPlatformScopedStorageXDirectory( |
| 46 | + PlatformScopedStorageXDirectoryCreationParams params, |
| 47 | + ) { |
| 48 | + return _DefaultScopedStorageXDirectory(params); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// Implementation of [PlatformFileSystemXFile] that represents a resource that |
| 53 | +/// does not exist. |
| 54 | +final class _DefaultFileSystemXFile extends PlatformFileSystemXFile { |
| 55 | + _DefaultFileSystemXFile(super.params) : super.implementation(); |
| 56 | + |
| 57 | + @override |
| 58 | + Future<bool> exists() async => false; |
| 59 | + |
| 60 | + @override |
| 61 | + Future<DateTime?> lastModified() async => null; |
| 62 | + |
| 63 | + @override |
| 64 | + Future<int?> length() async => null; |
| 65 | + |
| 66 | + @override |
| 67 | + Future<String?> name() async => null; |
| 68 | + |
| 69 | + @override |
| 70 | + Stream<Uint8List> openRead([int? start, int? end]) async* { |
| 71 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 72 | + } |
| 73 | + |
| 74 | + @override |
| 75 | + Future<Uint8List> readAsBytes() { |
| 76 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + Future<String> readAsString({Encoding encoding = utf8}) { |
| 81 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + Future<PlatformFileSystemXFile> writeAsBytes(PlatformWriteAsBytesParams params) { |
| 86 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Implementation of [PlatformFileSystemXDirectory] that represents a directory |
| 91 | +/// that does not exist. |
| 92 | +final class _DefaultFileSystemXDirectory extends PlatformFileSystemXDirectory { |
| 93 | + _DefaultFileSystemXDirectory(super.params) : super.implementation(); |
| 94 | + |
| 95 | + @override |
| 96 | + Future<bool> exists() async => false; |
| 97 | + |
| 98 | + @override |
| 99 | + Stream<PlatformXEntity> list(PlatformListParams params) async* { |
| 100 | + throw UnsupportedError('This instance does not represent any directory.'); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// Implementation of [PlatformScopedStorageXFile] that represents a resource |
| 105 | +/// that does not exist. |
| 106 | +final class _DefaultScopedStorageXFile extends PlatformScopedStorageXFile { |
| 107 | + _DefaultScopedStorageXFile(super.params) : super.implementation(); |
| 108 | + |
| 109 | + @override |
| 110 | + Future<bool> canRead() async => false; |
| 111 | + |
| 112 | + @override |
| 113 | + Future<bool> exists() async => false; |
| 114 | + |
| 115 | + @override |
| 116 | + Future<DateTime?> lastModified() async => null; |
| 117 | + |
| 118 | + @override |
| 119 | + Future<int?> length() async => null; |
| 120 | + |
| 121 | + @override |
| 122 | + Future<String?> name() async => null; |
| 123 | + |
| 124 | + @override |
| 125 | + Stream<Uint8List> openRead([int? start, int? end]) async* { |
| 126 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + Future<Uint8List> readAsBytes() { |
| 131 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 132 | + } |
| 133 | + |
| 134 | + @override |
| 135 | + Future<String> readAsString({Encoding encoding = utf8}) { |
| 136 | + throw UnsupportedError('This instance does not represent any resource.'); |
| 137 | + } |
| 138 | + |
| 139 | + @override |
| 140 | + Future<void> dispose() async {} |
| 141 | +} |
| 142 | + |
| 143 | +/// Implementation of [PlatformScopedStorageXDirectory] that represents a |
| 144 | +/// directory that does not exist. |
| 145 | +final class _DefaultScopedStorageXDirectory extends PlatformScopedStorageXDirectory { |
| 146 | + _DefaultScopedStorageXDirectory(super.params) : super.implementation(); |
| 147 | + |
| 148 | + @override |
| 149 | + Future<bool> exists() async => false; |
| 150 | + |
| 151 | + @override |
| 152 | + Future<bool> canRead() async => false; |
| 153 | + |
| 154 | + @override |
| 155 | + Stream<PlatformXEntity> list(PlatformListParams params) async* { |
| 156 | + throw UnsupportedError('This instance does not represent any directory.'); |
| 157 | + } |
| 158 | + |
| 159 | + @override |
| 160 | + Future<void> dispose() async {} |
| 161 | +} |
0 commit comments