CommonFileSystem.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using MediaBrowser.Controller.IO;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. namespace MediaBrowser.ServerApplication.IO
  7. {
  8. /// <summary>
  9. /// Class CommonFileSystem
  10. /// </summary>
  11. public class CommonFileSystem : IFileSystem
  12. {
  13. protected ILogger Logger;
  14. public CommonFileSystem(ILogger logger)
  15. {
  16. Logger = logger;
  17. }
  18. /// <summary>
  19. /// Determines whether the specified filename is shortcut.
  20. /// </summary>
  21. /// <param name="filename">The filename.</param>
  22. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  23. /// <exception cref="System.ArgumentNullException">filename</exception>
  24. public virtual bool IsShortcut(string filename)
  25. {
  26. if (string.IsNullOrEmpty(filename))
  27. {
  28. throw new ArgumentNullException("filename");
  29. }
  30. var extension = Path.GetExtension(filename);
  31. return string.Equals(extension, ".mblink", StringComparison.OrdinalIgnoreCase);
  32. }
  33. /// <summary>
  34. /// Resolves the shortcut.
  35. /// </summary>
  36. /// <param name="filename">The filename.</param>
  37. /// <returns>System.String.</returns>
  38. /// <exception cref="System.ArgumentNullException">filename</exception>
  39. public virtual string ResolveShortcut(string filename)
  40. {
  41. if (string.IsNullOrEmpty(filename))
  42. {
  43. throw new ArgumentNullException("filename");
  44. }
  45. if (string.Equals(Path.GetExtension(filename), ".mblink", StringComparison.OrdinalIgnoreCase))
  46. {
  47. return File.ReadAllText(filename);
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// Creates the shortcut.
  53. /// </summary>
  54. /// <param name="shortcutPath">The shortcut path.</param>
  55. /// <param name="target">The target.</param>
  56. /// <exception cref="System.ArgumentNullException">
  57. /// shortcutPath
  58. /// or
  59. /// target
  60. /// </exception>
  61. public void CreateShortcut(string shortcutPath, string target)
  62. {
  63. if (string.IsNullOrEmpty(shortcutPath))
  64. {
  65. throw new ArgumentNullException("shortcutPath");
  66. }
  67. if (string.IsNullOrEmpty(target))
  68. {
  69. throw new ArgumentNullException("target");
  70. }
  71. File.WriteAllText(shortcutPath, target);
  72. }
  73. /// <summary>
  74. /// Gets the file system info.
  75. /// </summary>
  76. /// <param name="path">The path.</param>
  77. /// <returns>FileSystemInfo.</returns>
  78. public FileSystemInfo GetFileSystemInfo(string path)
  79. {
  80. // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists
  81. if (Path.HasExtension(path))
  82. {
  83. var fileInfo = new FileInfo(path);
  84. if (fileInfo.Exists)
  85. {
  86. return fileInfo;
  87. }
  88. return new DirectoryInfo(path);
  89. }
  90. else
  91. {
  92. var fileInfo = new DirectoryInfo(path);
  93. if (fileInfo.Exists)
  94. {
  95. return fileInfo;
  96. }
  97. return new FileInfo(path);
  98. }
  99. }
  100. /// <summary>
  101. /// The space char
  102. /// </summary>
  103. private const char SpaceChar = ' ';
  104. /// <summary>
  105. /// The invalid file name chars
  106. /// </summary>
  107. private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
  108. /// <summary>
  109. /// Takes a filename and removes invalid characters
  110. /// </summary>
  111. /// <param name="filename">The filename.</param>
  112. /// <returns>System.String.</returns>
  113. /// <exception cref="System.ArgumentNullException">filename</exception>
  114. public string GetValidFilename(string filename)
  115. {
  116. if (string.IsNullOrEmpty(filename))
  117. {
  118. throw new ArgumentNullException("filename");
  119. }
  120. var builder = new StringBuilder(filename);
  121. foreach (var c in InvalidFileNameChars)
  122. {
  123. builder = builder.Replace(c, SpaceChar);
  124. }
  125. return builder.ToString();
  126. }
  127. /// <summary>
  128. /// Gets the creation time UTC.
  129. /// </summary>
  130. /// <param name="info">The info.</param>
  131. /// <returns>DateTime.</returns>
  132. public DateTime GetCreationTimeUtc(FileSystemInfo info)
  133. {
  134. // This could throw an error on some file systems that have dates out of range
  135. try
  136. {
  137. return info.CreationTimeUtc;
  138. }
  139. catch (Exception ex)
  140. {
  141. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  142. return DateTime.MinValue;
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// Adapted from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java
  148. /// </summary>
  149. internal class WindowsShortcut
  150. {
  151. public bool IsDirectory { get; private set; }
  152. public bool IsLocal { get; private set; }
  153. public string ResolvedPath { get; private set; }
  154. public WindowsShortcut(string file)
  155. {
  156. ParseLink(File.ReadAllBytes(file), Encoding.UTF8);
  157. }
  158. private static bool isMagicPresent(byte[] link)
  159. {
  160. const int magic = 0x0000004C;
  161. const int magic_offset = 0x00;
  162. return link.Length >= 32 && bytesToDword(link, magic_offset) == magic;
  163. }
  164. /**
  165. * Gobbles up link data by parsing it and storing info in member fields
  166. * @param link all the bytes from the .lnk file
  167. */
  168. private void ParseLink(byte[] link, Encoding encoding)
  169. {
  170. if (!isMagicPresent(link))
  171. throw new IOException("Invalid shortcut; magic is missing", 0);
  172. // get the flags byte
  173. byte flags = link[0x14];
  174. // get the file attributes byte
  175. const int file_atts_offset = 0x18;
  176. byte file_atts = link[file_atts_offset];
  177. byte is_dir_mask = (byte)0x10;
  178. if ((file_atts & is_dir_mask) > 0)
  179. {
  180. IsDirectory = true;
  181. }
  182. else
  183. {
  184. IsDirectory = false;
  185. }
  186. // if the shell settings are present, skip them
  187. const int shell_offset = 0x4c;
  188. const byte has_shell_mask = (byte)0x01;
  189. int shell_len = 0;
  190. if ((flags & has_shell_mask) > 0)
  191. {
  192. // the plus 2 accounts for the length marker itself
  193. shell_len = bytesToWord(link, shell_offset) + 2;
  194. }
  195. // get to the file settings
  196. int file_start = 0x4c + shell_len;
  197. const int file_location_info_flag_offset_offset = 0x08;
  198. int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
  199. IsLocal = (file_location_info_flag & 2) == 0;
  200. // get the local volume and local system values
  201. //final int localVolumeTable_offset_offset = 0x0C;
  202. const int basename_offset_offset = 0x10;
  203. const int networkVolumeTable_offset_offset = 0x14;
  204. const int finalname_offset_offset = 0x18;
  205. int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
  206. String finalname = getNullDelimitedString(link, finalname_offset, encoding);
  207. if (IsLocal)
  208. {
  209. int basename_offset = link[file_start + basename_offset_offset] + file_start;
  210. String basename = getNullDelimitedString(link, basename_offset, encoding);
  211. ResolvedPath = basename + finalname;
  212. }
  213. else
  214. {
  215. int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
  216. int shareName_offset_offset = 0x08;
  217. int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
  218. + networkVolumeTable_offset;
  219. String shareName = getNullDelimitedString(link, shareName_offset, encoding);
  220. ResolvedPath = shareName + "\\" + finalname;
  221. }
  222. }
  223. private static string getNullDelimitedString(byte[] bytes, int off, Encoding encoding)
  224. {
  225. int len = 0;
  226. // count bytes until the null character (0)
  227. while (true)
  228. {
  229. if (bytes[off + len] == 0)
  230. {
  231. break;
  232. }
  233. len++;
  234. }
  235. return encoding.GetString(bytes, off, len);
  236. }
  237. /*
  238. * convert two bytes into a short note, this is little endian because it's
  239. * for an Intel only OS.
  240. */
  241. private static int bytesToWord(byte[] bytes, int off)
  242. {
  243. return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
  244. }
  245. private static int bytesToDword(byte[] bytes, int off)
  246. {
  247. return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
  248. }
  249. }
  250. }