| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Linq; |
| | 4 | | using MediaBrowser.Model.IO; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace MediaBrowser.Controller.IO; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Helper methods for file system management. |
| | 11 | | /// </summary> |
| | 12 | | public static class FileSystemHelper |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Deletes the file. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="fileSystem">The fileSystem.</param> |
| | 18 | | /// <param name="path">The path.</param> |
| | 19 | | /// <param name="logger">The logger.</param> |
| | 20 | | public static void DeleteFile(IFileSystem fileSystem, string path, ILogger logger) |
| | 21 | | { |
| | 22 | | try |
| | 23 | | { |
| 0 | 24 | | fileSystem.DeleteFile(path); |
| 0 | 25 | | } |
| 0 | 26 | | catch (UnauthorizedAccessException ex) |
| | 27 | | { |
| 0 | 28 | | logger.LogError(ex, "Error deleting file {Path}", path); |
| 0 | 29 | | } |
| 0 | 30 | | catch (IOException ex) |
| | 31 | | { |
| 0 | 32 | | logger.LogError(ex, "Error deleting file {Path}", path); |
| 0 | 33 | | } |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Recursively delete empty folders. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="fileSystem">The fileSystem.</param> |
| | 40 | | /// <param name="path">The path.</param> |
| | 41 | | /// <param name="logger">The logger.</param> |
| | 42 | | public static void DeleteEmptyFolders(IFileSystem fileSystem, string path, ILogger logger) |
| | 43 | | { |
| 4 | 44 | | foreach (var directory in fileSystem.GetDirectoryPaths(path)) |
| | 45 | | { |
| 0 | 46 | | DeleteEmptyFolders(fileSystem, directory, logger); |
| 0 | 47 | | if (!fileSystem.GetFileSystemEntryPaths(directory).Any()) |
| | 48 | | { |
| | 49 | | try |
| | 50 | | { |
| 0 | 51 | | Directory.Delete(directory, false); |
| 0 | 52 | | } |
| 0 | 53 | | catch (UnauthorizedAccessException ex) |
| | 54 | | { |
| 0 | 55 | | logger.LogError(ex, "Error deleting directory {Path}", directory); |
| 0 | 56 | | } |
| 0 | 57 | | catch (IOException ex) |
| | 58 | | { |
| 0 | 59 | | logger.LogError(ex, "Error deleting directory {Path}", directory); |
| 0 | 60 | | } |
| | 61 | | } |
| | 62 | | } |
| 2 | 63 | | } |
| | 64 | | } |