Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve file extensions #150

Merged
merged 2 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ analyzer:
unused_local_variable: warning
dead_code: warning
invalid_override_of_non_virtual_member: error
enable-experiment:
- extension-methods

linter:
rules:
Expand Down
4 changes: 2 additions & 2 deletions lib/flutter_cache_manager.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export 'src/cache_manager.dart';
export 'src/file_fetcher.dart';
export 'src/file_info.dart';
export 'src/web_helper.dart' show HttpExceptionWithStatus;
export 'src/web/file_fetcher.dart';
export 'src/web/web_helper.dart' show HttpExceptionWithStatus;
5 changes: 3 additions & 2 deletions lib/src/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import 'dart:typed_data';

import 'package:file/file.dart' as f;
import 'package:file/local.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/src/storage/cache_object.dart';
import 'package:flutter_cache_manager/src/cache_store.dart';
import 'package:flutter_cache_manager/src/file_fetcher.dart';
import 'package:flutter_cache_manager/src/web/file_fetcher.dart';
import 'package:flutter_cache_manager/src/file_info.dart';
import 'package:flutter_cache_manager/src/web_helper.dart';
import 'package:flutter_cache_manager/src/web/web_helper.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:pedantic/pedantic.dart';
Expand Down
17 changes: 10 additions & 7 deletions lib/src/file_fetcher.dart → lib/src/web/file_fetcher.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:async';
import 'dart:io';
import 'package:clock/clock.dart';
import 'package:flutter_cache_manager/src/web_helper.dart';
import 'package:http/http.dart' as http;
import 'mime_converter.dart';

///Flutter Cache Manager
///Copyright (c) 2019 Rene Floor
Expand All @@ -19,7 +20,7 @@ abstract class FileService {
/// [WebHelper]. One can easily adapt it to use dio or any other http client.
class HttpFileFetcher implements FileService {
http.Client _httpClient;
HttpFileFetcher({http.Client httpClient}){
HttpFileFetcher({http.Client httpClient}) {
_httpClient = httpClient ?? http.Client();
}

Expand All @@ -37,13 +38,17 @@ class HttpFileFetcher implements FileService {
/// Defines the interface for a get result of a [FileService].
abstract class FileFetcherResponse {
/// [content] is a stream of bytes
Stream<List<int>> get content => null;
Stream<List<int>> get content;

/// [statusCode] is expected to conform to an http status code.
int get statusCode;

/// Defines till when the cache should be assumed to be valid.
DateTime get validTill;

/// [eTag] is used when asking to update the cache
String get eTag;

/// Used to save the file on the storage, includes a dot. For example '.jpeg'
String get fileExtension;
}
Expand Down Expand Up @@ -100,10 +105,8 @@ class HttpFileFetcherResponse implements FileFetcherResponse {
String get fileExtension {
var fileExtension = '';
if (_hasHeader('content-type')) {
final type = _header('content-type').split('/');
if (type.length == 2) {
fileExtension = '.${type[1]}';
}
var contentType = ContentType.parse(_header('content-type'));
fileExtension = contentType.fileExtension ?? '';
}
return fileExtension;
}
Expand Down
90 changes: 90 additions & 0 deletions lib/src/web/mime_converter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'dart:io';

///Converts the most common MIME types to the most expected file extension.
extension ContentTypeConverter on ContentType {
String get fileExtension {
if (this == null) return null;
if (mimeTypes.containsKey(toString())) return mimeTypes[toString()];
return '.$subType';
}
}

/// Source of MIME Types:
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
/// Updated on 20th of March in 2020 while being quarantined
const mimeTypes = {
'application/epub+zip': '.epub',
'application/gzip': '.gz',
'application/java-archive': '.jar',
'application/json': '.json',
'application/ld+json': '.jsonld',
'application/msword': '.doc',
'application/octet-stream': '.bin',
'application/ogg': '.ogx',
'application/pdf': '.pdf',
'application/php': '.php',
'application/rtf': '.rtf',
'application/vnd.amazon.ebook': '.azw',
'application/vnd.apple.installer+xml': '.mpkg',
'application/vnd.mozilla.xul+xml': '.xul',
'application/vnd.ms-excel': '.xls',
'application/vnd.ms-fontobject': '.eot',
'application/vnd.ms-powerpoint': '.ppt',
'application/vnd.oasis.opendocument.presentation': '.odp',
'application/vnd.oasis.opendocument.spreadsheet': '.ods',
'application/vnd.oasis.opendocument.text': '.odt',
'application/vnd.openxmlformats-officedocument.presentationml.presentation':
'.pptx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
'.docx',
'application/vnd.rar': '.rar',
'application/vnd.visio': '.vsd',
'application/x-7z-compressed': '.7z',
'application/x-abiword': '.abw',
'application/x-bzip': '.bz',
'application/x-bzip2': '.bz2',
'application/x-csh': '.csh',
'application/x-freearc': '.arc',
'application/x-sh': '.sh',
'application/x-shockwave-flash': '.swf',
'application/x-tar': '.tar',
'application/xhtml+xml': '.xhtml',
'application/xml': '.xml',
'application/zip': '.zip',
'audio/3gpp': '.3gp',
'audio/3gpp2': '.3g2',
'audio/aac': '.aac',
'audio/midi audio/x-midi': '.midi',
'audio/mpeg': '.mp3',
'audio/ogg': '.oga',
'audio/opus': '.opus',
'audio/wav': '.wav',
'audio/webm': '.weba',
'font/otf': '.otf',
'font/ttf': '.ttf',
'font/woff': '.woff',
'font/woff2': '.woff2',
'image/bmp': '.bmp',
'image/gif': '.gif',
'image/jpeg': '.jpg',
'image/png': '.png',
'image/svg+xml': '.svg',
'image/tiff': '.tiff',
'image/vnd.microsoft.icon': '.ico',
'image/webp': '.webp',
'text/calendar': '.ics',
'text/css': '.css',
'text/csv': '.csv',
'text/html': '.html',
'text/javascript': '.js',
'text/plain': '.txt',
'text/xml': '.xml',
'video/3gpp': '.3gp',
'video/3gpp2': '.3g2',
'video/mp2t': '.ts',
'video/mpeg': '.mpeg',
'video/ogg': '.ogv',
'video/webm': '.webm',
'video/x-msvideo': '.avi'
};
2 changes: 1 addition & 1 deletion lib/src/web_helper.dart → lib/src/web/web_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:io';

import 'package:flutter_cache_manager/src/storage/cache_object.dart';
import 'package:flutter_cache_manager/src/cache_store.dart';
import 'package:flutter_cache_manager/src/file_fetcher.dart';
import 'package:flutter_cache_manager/src/web/file_fetcher.dart';
import 'package:flutter_cache_manager/src/file_info.dart';
import 'package:pedantic/pedantic.dart';
import 'package:uuid/uuid.dart';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ author: Rene Floor <pub@renefloor.nl>
homepage: https://github.com/renefloor/flutter_cache_manager

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.6.0 <3.0.0"

dependencies:
flutter:
Expand Down
4 changes: 2 additions & 2 deletions test/http_file_fetcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ void main() {
group('Check header values', () {
test('Valid headers should be parsed normally', () async {
var eTag = 'test';
var fileExtension = 'jpeg';
var contentType = 'image/$fileExtension';
var fileExtension = 'jpg';
var contentType = 'image/jpeg';
var maxAge = const Duration(hours: 2);

var client = MockClient((request) async {
Expand Down