Skip to main content

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โ€‹

MethodFile Types
isOfficeFile.doc, .docx, .xls, .xlsx, .ppt, .pptx
isDocFile.doc, .docx
isSpreadSheetFile.xls, .xlsx
isPresentationFile.ppt, .pptx
isImageFile.jpg, .jpeg, .png
isPDFFile.pdf
isSupportedFileAll 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 TypesIcon
.pdfCarbonIcons.pdf
.doc, .docxCarbonIcons.doc
.ppt, .pptxCarbonIcons.ppt
.xls, .xlsxCarbonIcons.xls
.zipCarbonIcons.zip
.txtCarbonIcons.txt
.jpg, .jpeg, .png, .gif, .bmp, .webpCarbonIcons.image
DefaultIcons.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