Skip to content
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
4 changes: 4 additions & 0 deletions flutter_cache_manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [3.4.8] - 2026-11-02

* Uses the URL's file extension when a download's content type is missing or generic, such as `application/octet-stream` or `binary/octet-stream` ([#489](https://github.com/Baseflow/flutter_cache_manager/issues/489)).

## [3.4.7] - 2026-10-31

* Keeps cache cleanup going when a cached file cannot be deleted: the cache info of the other removed files is still deleted, and only the failed file's cache info is kept so the next cleanup retries it.
Expand Down
18 changes: 7 additions & 11 deletions flutter_cache_manager/lib/src/compat/file_service_compat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ class FileServiceCompat extends FileService {
Map<String, String>? headers,
}) async {
final legacyResponse = await fileFetcher(url, headers: headers);
return CompatFileServiceGetResponse(legacyResponse);
return CompatFileServiceGetResponse(legacyResponse, Uri.tryParse(url));
}
}

class CompatFileServiceGetResponse implements FileServiceResponse {
final FileFetcherResponse legacyResponse;
final Uri? _uri;
final DateTime _receivedTime = clock.now();

CompatFileServiceGetResponse(this.legacyResponse);
CompatFileServiceGetResponse(this.legacyResponse, [this._uri]);

String? _header(String name) {
return legacyResponse.header(name);
Expand Down Expand Up @@ -64,15 +65,10 @@ class CompatFileServiceGetResponse implements FileServiceResponse {
String? get eTag => _header(HttpHeaders.etagHeader);

@override
String get fileExtension {
var fileExtension = '';
final contentTypeHeader = _header(HttpHeaders.contentTypeHeader);
if (contentTypeHeader != null) {
final contentType = ContentType.parse(contentTypeHeader);
fileExtension = contentType.fileExtension;
}
return fileExtension;
}
String get fileExtension => fileExtensionFromContentTypeOrUri(
_header(HttpHeaders.contentTypeHeader),
_uri,
);

@override
int get statusCode => legacyResponse.statusCode as int;
Expand Down
19 changes: 8 additions & 11 deletions flutter_cache_manager/lib/src/web/file_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class HttpFileService extends FileService {
}
final httpResponse = await _httpClient.send(req);

return HttpGetResponse(httpResponse);
return HttpGetResponse(httpResponse, req.url);
}
}

Expand Down Expand Up @@ -66,11 +66,13 @@ abstract class FileServiceResponse {

/// Basic implementation of a [FileServiceResponse] for http requests.
class HttpGetResponse implements FileServiceResponse {
HttpGetResponse(this._response);
HttpGetResponse(this._response, [Uri? uri])
: _uri = uri ?? _response.request?.url;

final DateTime _receivedTime = clock.now();

final http.StreamedResponse _response;
final Uri? _uri;

@override
int get statusCode => _response.statusCode;
Expand Down Expand Up @@ -114,13 +116,8 @@ class HttpGetResponse implements FileServiceResponse {
String? get eTag => _header(HttpHeaders.etagHeader);

@override
String get fileExtension {
var fileExtension = '';
final contentTypeHeader = _header(HttpHeaders.contentTypeHeader);
if (contentTypeHeader != null) {
final contentType = ContentType.parse(contentTypeHeader);
fileExtension = contentType.fileExtension;
}
return fileExtension;
}
String get fileExtension => fileExtensionFromContentTypeOrUri(
_header(HttpHeaders.contentTypeHeader),
_uri,
);
}
46 changes: 46 additions & 0 deletions flutter_cache_manager/lib/src/web/mime_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,52 @@ extension ContentTypeConverter on ContentType {
String get fileExtension => mimeTypes[mimeType] ?? '.$subType';
}

final RegExp _safeFileExtension = RegExp(
r'^\.(?=[A-Za-z0-9]*[A-Za-z])[A-Za-z0-9]{1,10}$',
);

const _genericContentTypes = {
'application/octet-stream',
'application/x-octet-stream',
'application/force-download',
'application/download',
'binary/octet-stream',
};

const _dynamicEndpointExtensions = {'.php', '.asp', '.aspx', '.cgi', '.jsp'};

/// Gets an extension from a specific content type or a safe URL path suffix.
///
/// Specific content types take precedence. Missing or generic content types
/// use a short alphanumeric URL suffix containing a letter when available.
/// Known server-side endpoint suffixes are ignored. The result falls back to
/// `.bin` for generic types or an empty string when the content type is absent.
String fileExtensionFromContentTypeOrUri(String? contentTypeHeader, Uri? uri) {
Comment thread
rahulshahDEV marked this conversation as resolved.
Resolved
final contentType = contentTypeHeader == null
? null
: ContentType.parse(contentTypeHeader);

if (contentType != null &&
!_genericContentTypes.contains(contentType.mimeType)) {
return contentType.fileExtension;
}

final pathSegments = uri?.pathSegments;
final fileName = pathSegments == null || pathSegments.isEmpty
? ''
: pathSegments.last;
final extensionIndex = fileName.lastIndexOf('.');
Comment thread
rahulshahDEV marked this conversation as resolved.
Resolved
if (extensionIndex > 0 && extensionIndex < fileName.length - 1) {
final extension = fileName.substring(extensionIndex);
Comment thread
rahulshahDEV marked this conversation as resolved.
Resolved
if (_safeFileExtension.hasMatch(extension) &&
!_dynamicEndpointExtensions.contains(extension.toLowerCase())) {
return extension;
}
}

return contentType == null ? '' : '.bin';
}

/// 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
Expand Down
2 changes: 1 addition & 1 deletion flutter_cache_manager/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_cache_manager
description: Generic cache manager for flutter. Saves web files on the storages of the device and saves the cache info using sqflite.
version: 3.4.7
version: 3.4.8
homepage: https://github.com/Baseflow/flutter_cache_manager
topics:
- cache
Expand Down
Loading
Loading