Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
749 stars 438 forks source link

IOFileSystem - problem using the Customize example in the docs for 2.0.0 #238

Open sowens-csd opened 3 years ago

sowens-csd commented 3 years ago

I'm upgrading from the previous version to 2.0.0 and trying to follow the new Customize instructions to use CacheManager instead of BaseCacheManager. The reference to IOFileSystem doesn't resolve. What am I doing wrong?

CacheManager(
        Config(
          key,
          stalePeriod: const Duration(days: 7),
          maxNrOfCacheObjects: 60,
          repo: JsonCacheInfoRepository(databaseName: key),
          fileSystem: IOFileSystem(key),
          fileService: HttpFileService(),
        ),
      );
befora commented 3 years ago

@sowens-csd I had the same issue and had to check out the example for a little help. This worked for me.

import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:flutter_cache_manager/src/storage/file_system/file_system.dart' as c;
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

class IOFileSystem implements c.FileSystem {
  final Future<Directory> _fileDir;

  IOFileSystem(String key) : _fileDir = createDirectory(key);

  static Future<Directory> createDirectory(String key) async {
    var baseDir = await getTemporaryDirectory();
    var path = p.join(baseDir.path, key);

    var fs = const LocalFileSystem();
    var directory = fs.directory((path));
    await directory.create(recursive: true);
    return directory;
  }

  @override
  Future<File> createFile(String name) async {
    assert(name != null);
    return (await _fileDir).childFile(name);
  }
}
renefloor commented 3 years ago

I see that IOFileSystem is not exported by the package, so that could be it. However, IOFileSystem(key), is the default so that is not customising anything. The example of @sethchhim gives you more options to customise, for example the base path of where the images are stored.

sowens-csd commented 3 years ago

Excellent, if that's the default then I don't need it. You can probably remove that from the sample in your README but I see you have a doc label so you were likely planning to do that.

Just in case I need to customize it in the future, should it be exported by the package?

renefloor commented 3 years ago

I think so, but I'll have to try it.

jozef-pridavok commented 3 years ago

@renefloor do you plan to export IOFileSystem? @sethchhim 's workaround is okay but temporary for me (it is a duplicated code with warning "Don't import implementation files from another package")...

kengu commented 3 years ago

@renefloor Until file_system.dart is included in exported files, custom implementations are only possible by importing internal dependencies, which is bad practice. Any plans for exporting FileSystem?

idish commented 3 years ago

+1 for this, we can't properly customize the fileSystem class as of now. Also, it would be great if we could somehow affect the directory of the "cached" files (i.e ApplicationsDirectory instead of CacheDirectory, so we don't need to subclass and overwrite existing logic.

mw66 commented 3 months ago

Any update? how to fix this?