FileService
FileService
is a utility class that provides common file operations and metadata extraction used throughout the Supa Flutter application. It encapsulates logic for detecting file types, generating viewer URLs, launching external links, and resolving appropriate icons for various file extensions.
๐ฆ Featuresโ
- File type detection (Office, PDF, Image, etc.)
- Viewer URL generation for Microsoft Office and Google Docs
- Launch external file URLs in browser
- Icon mapping for file extensions using Carbon Icons
- Supported file type validation
๐ง Use Casesโ
- Detecting file type from filename or extension.
- Displaying appropriate icon in the UI.
- Opening files in web viewers like Office Online or Google Docs.
- Validating whether a file can be processed.
๐ File Type Detectionโ
Example:โ
FileService.isPDFFile("invoice.pdf"); // true
FileService.isImageFile("profile.jpg"); // true
FileService.isOfficeFile("report.docx"); // true
Available Detectorsโ
Method | File Types |
---|---|
isOfficeFile | .doc , .docx , .xls , .xlsx , .ppt , .pptx |
isDocFile | .doc , .docx |
isSpreadSheetFile | .xls , .xlsx |
isPresentationFile | .ppt , .pptx |
isImageFile | .jpg , .jpeg , .png |
isPDFFile | .pdf |
isSupportedFile | All supported file types listed below |
๐ Viewer URL Utilitiesโ
final officeUrl = FileService.createOfficeViewerUrl(fileUrl);
final googleUrl = FileService.createGoogleDocsViewerUrl(fileUrl);
Formatโ
-
Office Viewer:
https://view.officeapps.live.com/op/view.aspx?src=<encoded_url>
-
Google Docs Viewer:
https://docs.google.com/gview?embedded=true&url=<encoded_url>
๐ผ Icon Mappingโ
final icon = FileService.iconData("document.pdf");
Icon Mapping Tableโ
Extension Types | Icon |
---|---|
.pdf | CarbonIcons.pdf |
.doc , .docx | CarbonIcons.doc |
.ppt , .pptx | CarbonIcons.ppt |
.xls , .xlsx | CarbonIcons.xls |
.zip | CarbonIcons.zip |
.txt | CarbonIcons.txt |
.jpg , .jpeg , .png , .gif , .bmp , .webp | CarbonIcons.image |
Default | Icons.attachment |
๐ Launching URLsโ
await FileService.launchUrl("https://example.com/document.pdf");
- Checks URL validity before launching via browser.
- Uses
url_launcher_string
internally.
๐งช Supported File Typesโ
Validated via isSupportedFile()
:
const supportedExtensions = [
'jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', // Images
'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', // Office files
'pdf', 'zip', 'txt'
];
๐ Locationโ
lib/services/file_service.dart