Line data Source code
1 : import 'package:file_type_plus/file_type_plus.dart';
2 :
3 : /// Extension on [FileTypeImpl] providing pattern matching functionality.
4 : ///
5 : /// This extension enables type-safe pattern matching over different media types
6 : /// using a fold-like pattern, allowing you to handle each media type specifically.
7 : extension MediaTypeExtension on FileTypeImpl {
8 : /// Performs pattern matching on media types using a fold-like pattern.
9 : ///
10 : /// This method allows you to handle different media types with type-specific
11 : /// callbacks. Each media type has an optional callback that will be executed
12 : /// if the current instance is of that type.
13 : ///
14 : /// Parameters:
15 : /// - [image]: Called if this is an [ImageType]
16 : /// - [audio]: Called if this is an [AudioType]
17 : /// - [video]: Called if this is an [VideoType]
18 : /// - [document]: Called if this is a [DocumentType]
19 : /// - [url]: Called if this is a [UrlType]
20 : /// - [orElse]: Called if no matching callback is provided
21 : ///
22 : /// Returns: The result of the matching callback or [orElse]
23 0 : T fold<T>({
24 : required T Function() orElse,
25 : T Function(ImageType image)? image,
26 : T Function(AudioType audio)? audio,
27 : T Function(VideoType video)? video,
28 : T Function(DocumentType doc)? document,
29 : T Function(UrlType url)? url,
30 : }) {
31 0 : if (this is ImageType && image != null) {
32 0 : return image(this as ImageType);
33 0 : } else if (this is AudioType && audio != null) {
34 0 : return audio(this as AudioType);
35 0 : } else if (this is VideoType && video != null) {
36 0 : return video(this as VideoType);
37 0 : } else if (this is DocumentType && document != null) {
38 0 : return document(this as DocumentType);
39 0 : } else if (this is UrlType && url != null) {
40 0 : return url(this as UrlType);
41 : }
42 0 : return orElse();
43 : }
44 : }
45 :
46 : /// Base class for defining custom media type variants.
47 : ///
48 : /// The built-in types ([ImageType], [AudioType], [VideoType], [DocumentType],
49 : /// [UrlType], [OtherType]) are implemented by extending this class. You can
50 : /// introduce your own application-specific types by subclassing [FileTypeImpl]
51 : /// and forwarding an appropriate base [FileType] value to [super.copy].
52 : ///
53 : /// Example:
54 : /// ```dart
55 : /// class SubtitleType extends FileTypeImpl {
56 : /// SubtitleType() : super.copy(FileType.other);
57 : /// @override
58 : /// List<Object?> get props => const [];
59 : /// }
60 : /// ```
61 : abstract class FileTypeImpl extends FileType {
62 2 : FileTypeImpl.copy(super.value) : super.copy();
63 : }
64 :
65 : /// Media type for video files.
66 : ///
67 : /// Extends [FileTypeImpl] to provide video-specific classification.
68 : /// Optionally stores duration information if available.
69 : class VideoType extends FileTypeImpl implements DurationMedia {
70 : /// The duration of the video, if available.
71 : @override
72 : final Duration? duration;
73 :
74 : /// Creates a [VideoType] with an optional duration.
75 1 : @override
76 2 : VideoType([this.duration]) : super.copy(FileType.video);
77 :
78 1 : @override
79 2 : List<Object?> get props => [duration];
80 : }
81 :
82 : /// Media type for audio files.
83 : ///
84 : /// Extends [FileTypeImpl] to provide audio-specific classification.
85 : /// Optionally stores duration information if available.
86 : class AudioType extends FileTypeImpl implements DurationMedia {
87 : /// The duration of the audio, if available.
88 : @override
89 : final Duration? duration;
90 1 : @override
91 2 : List<Object?> get props => [duration];
92 :
93 : /// Creates an [AudioType] with an optional duration.
94 3 : AudioType([this.duration]) : super.copy(FileType.audio);
95 : }
96 :
97 : /// Media type for image files.
98 : ///
99 : /// Extends [FileTypeImpl] to provide image-specific classification.
100 : class ImageType extends FileTypeImpl {
101 : /// Creates an [ImageType].
102 3 : ImageType() : super.copy(FileType.image);
103 :
104 1 : @override
105 1 : List<Object?> get props => [];
106 : }
107 :
108 : /// Media type for document files (primarily PDF).
109 : ///
110 : /// Extends [FileTypeImpl] to provide document-specific classification.
111 : class DocumentType extends FileTypeImpl {
112 : /// Creates a [DocumentType].
113 3 : DocumentType() : super.copy(FileType.document);
114 1 : @override
115 1 : List<Object?> get props => [];
116 : }
117 :
118 : /// Media type for URL references.
119 : ///
120 : /// Extends [FileTypeImpl] to provide URL reference classification.
121 : class UrlType extends FileTypeImpl {
122 : /// Creates a [UrlType].
123 0 : UrlType() : super.copy(FileType.html);
124 0 : @override
125 0 : List<Object?> get props => [];
126 : }
127 :
128 : /// Media type for other/unclassified file types.
129 : ///
130 : /// Extends [FileTypeImpl] to provide a fallback classification for files
131 : /// that don't match any other specific media type.
132 : class OtherType extends FileTypeImpl {
133 : /// Creates an [OtherType].
134 3 : OtherType() : super.copy(FileType.other);
135 :
136 1 : @override
137 1 : List<Object?> get props => [];
138 : }
139 :
140 : /// Interface for media types that have duration information.
141 : ///
142 : /// This interface is implemented by media types that can have duration metadata,
143 : /// such as audio and video files.
144 : abstract class DurationMedia {
145 : /// The duration of the media, if available.
146 : Duration? get duration;
147 : }
|