/// <summary> /// 初始化类<see cref="MediaDirectory"/>。 /// </summary> /// <param name="directory">存储文件夹。</param> /// <param name="mfdb">数据库操作接口。</param> /// <param name="sfdb">数据库操作接口。</param> public MediaDirectory(IStorageDirectory directory, IDbContext <MediaFile> mfdb, IDbContext <StoredFile> sfdb) { _directory = directory; _mfdb = mfdb; _sfdb = sfdb; //媒体文件夹。 _media = directory.GetPhysicalPath("media"); //缩略图文件夹 _thumbs = directory.GetPhysicalPath("media/thumbs"); }
/// <summary> /// 清除已经删除的物理文件。 /// </summary> /// <returns>返回删除任务。</returns> public async Task ClearDeletedPhysicalFilesAsync() { var files = await _sfdb.AsQueryable() .WithNolock() .LeftJoin <MediaFile>((s, m) => s.FileId == m.FileId) .Where <MediaFile>(x => x.Id == null) .Select() .AsEnumerableAsync(); foreach (var file in files) { var info = new FileInfo(_directory.GetPhysicalPath(file.Path)); if (info.Exists) {//删除物理文件 if (info.Extension.IsPicture()) { foreach (var current in info.Directory.GetFiles()) { if (current.Name.StartsWith(file.FileId + ".", StringComparison.OrdinalIgnoreCase)) { current.Delete(); } } } else { info.Delete(); } } //删除数据库 await _sfdb.DeleteAsync(file.FileId); } }
/// <summary> /// 初始化类<see cref="FileStorageCache"/>。 /// </summary> /// <param name="cache">缓存接口。</param> /// <param name="root">存储文件夹。</param> /// <param name="db">缓存数据库操作接口。</param> public FileStorageCache(IMemoryCache cache, IStorageDirectory root, IDbContext <StorageCache> db) { _cache = cache; _db = db; _cacheRoot = root.GetPhysicalPath("caches"); if (!Directory.Exists(_cacheRoot)) { Directory.CreateDirectory(_cacheRoot); } }
/// <summary> /// 上传用户头像。 /// </summary> /// <param name="userId">用户Id。</param> /// <param name="file">当前用户实例。</param> /// <returns>返回上传任务。</returns> public async Task <string> UploadAsync(int userId, IFormFile file) { var tempFile = await _storageDirectory.SaveToTempAsync(file); tempFile = tempFile.Resize(Size, Size); //上传头像新文件,把老文件删除 var path = _storageDirectory.GetPhysicalPath($"avatars/{userId}/"); if (Directory.Exists(path)) { Directory.Delete(path, true); } path = Path.Combine(path, "default.png").MakeDirectory(); tempFile.MoveTo(path); return($"/s-avatars/{userId}.png"); }
public EpubManager(IStorageDirectory storageDirectory) { _storageDirectory = storageDirectory; _epubDirectory = storageDirectory.GetPhysicalPath("epub").MakeDirectory(); }