Skip to content

Commit

Permalink
fixed Baseflow#435
Browse files Browse the repository at this point in the history
  • Loading branch information
iulian0512 committed Nov 8, 2023
1 parent 32b71c1 commit 474d41d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:clock/clock.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_cache_manager/src/storage/file_system/file_name_validator.dart';
import 'package:flutter_cache_manager/src/web/mime_converter.dart';

class FileServiceCompat extends FileService {
Expand Down Expand Up @@ -67,7 +68,10 @@ class CompatFileServiceGetResponse implements FileServiceResponse {
final contentTypeHeader = _header(HttpHeaders.contentTypeHeader);
if (contentTypeHeader != null) {
final contentType = ContentType.parse(contentTypeHeader);
fileExtension = contentType.fileExtension;
fileExtension =
!FileNameValidator.isValidFileName(contentType.fileExtension)
? '.file'
: contentType.fileExtension;
}
return fileExtension;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
abstract class FileNameValidator {
static final List<String> forbiddenWindowsCharacters = [
'<',
'>',
':',
'"',
'/',
'\\',
'|',
'?',
'*'
];
static final List<String> forbiddenWindowsFileNames = [
'CON',
'PRN',
'AUX',
'NUL',
'COM1',
'COM2',
'COM3',
'COM4',
'COM5',
'COM6',
'COM7',
'COM8',
'COM9',
'LPT1',
'LPT2',
'LPT3',
'LPT4',
'LPT5',
'LPT6',
'LPT7',
'LPT8',
'LPT9'
];

static bool isValidFileName(String fileName) {
// Check for forbidden characters in Windows
for (final char in forbiddenWindowsCharacters) {
if (fileName.contains(char)) return false;
}

// Check for forbidden file names in Windows
if (forbiddenWindowsFileNames.contains(fileName.toUpperCase()))
return false;

// Check for Linux forbidden character (forward slash)
if (fileName.contains('/')) return false;

return true;
}
}
6 changes: 5 additions & 1 deletion flutter_cache_manager/lib/src/web/file_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:clock/clock.dart';
import 'package:flutter_cache_manager/src/storage/file_system/file_name_validator.dart';
import 'package:flutter_cache_manager/src/web/mime_converter.dart';
import 'package:http/http.dart' as http;

Expand Down Expand Up @@ -117,7 +118,10 @@ class HttpGetResponse implements FileServiceResponse {
final contentTypeHeader = _header(HttpHeaders.contentTypeHeader);
if (contentTypeHeader != null) {
final contentType = ContentType.parse(contentTypeHeader);
fileExtension = contentType.fileExtension;
fileExtension =
!FileNameValidator.isValidFileName(contentType.fileExtension)
? '.file'
: contentType.fileExtension;
}
return fileExtension;
}
Expand Down

0 comments on commit 474d41d

Please sign in to comment.