The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Win32;
  3. using MimeDetective;
  4. using MimeDetective.Extensions;
  5. using System;
  6. using System.IO;
  7. namespace Teknik.Utilities
  8. {
  9. public static class FileHelper
  10. {
  11. public static string GenerateRandomFileName(string path, string extension, int length)
  12. {
  13. if (Directory.Exists(path))
  14. {
  15. string filename = StringHelper.RandomString(length);
  16. string subDir = filename[0].ToString();
  17. path = Path.Combine(path, subDir);
  18. if (!Directory.Exists(path))
  19. {
  20. Directory.CreateDirectory(path);
  21. }
  22. while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
  23. {
  24. filename = StringHelper.RandomString(length);
  25. subDir = filename[0].ToString();
  26. path = Path.Combine(path, subDir);
  27. if (!Directory.Exists(path))
  28. {
  29. Directory.CreateDirectory(path);
  30. }
  31. }
  32. return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
  33. }
  34. return string.Empty;
  35. }
  36. /// <summary>
  37. /// Append a number to the end of a file name to make it unique, incrementing the number if needed
  38. /// </summary>
  39. /// <param name="file">the name of the file, including extension</param>
  40. /// <param name="destinationFolder">the destination folder in which to check for uniqueness</param>
  41. /// <returns>the file name, without folder, with extension, with a unique index as the suffix, if necessary</returns>
  42. public static string MakeUniqueFilename(string file, string destinationFolder)
  43. {
  44. string fileName = string.Empty;
  45. try
  46. {
  47. string fileBase = Path.GetFileNameWithoutExtension(file);
  48. string fileExt = Path.GetExtension(file);
  49. fileName = string.Format("{0}{1}", fileBase, fileExt);
  50. string filePath = Path.Combine(destinationFolder, fileName);
  51. int iteration = 1;
  52. while (File.Exists(filePath))
  53. {
  54. fileName = string.Format("{0} ({1}){2}", fileBase, iteration, fileExt);
  55. filePath = Path.Combine(destinationFolder, fileName);
  56. iteration++;
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. // Throw up exception to parent
  62. throw new Exception(string.Format("Exception occured in FileHelper.MakeUniqueFilename({0}, {1})", file, destinationFolder), ex);
  63. }
  64. return fileName;
  65. }
  66. public static string GetDefaultExtension(string mimeType)
  67. {
  68. return GetDefaultExtension(mimeType, string.Empty);
  69. }
  70. public static string GetDefaultExtension(string mimeType, string defaultExtension)
  71. {
  72. string result;
  73. RegistryKey key;
  74. object value;
  75. key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
  76. value = key != null ? key.GetValue("Extension", null) : null;
  77. result = value != null ? value.ToString() : defaultExtension;
  78. return result;
  79. }
  80. public static string GetDefaultExtension(Stream fileStream, string defaultExtension)
  81. {
  82. FileType fileType = fileStream.GetFileType();
  83. if (!string.IsNullOrEmpty(fileType.Extension))
  84. return fileType.Extension;
  85. return defaultExtension;
  86. }
  87. public static string MapPath(IHostingEnvironment env, string file)
  88. {
  89. var webRoot = env.WebRootPath;
  90. return Path.Combine(webRoot, file);
  91. }
  92. public static string GetFileExtension(string fileName)
  93. {
  94. string ext;
  95. var parts = fileName.Split('.');
  96. if (parts.Length == 1 || (parts[0] == "" && parts.Length == 2))
  97. {
  98. ext = string.Empty;
  99. }
  100. else if (parts.Length > 2)
  101. {
  102. ext = "." + parts[parts.Length - 1];
  103. // Special case for G-Zip files
  104. if (ext == ".gz")
  105. {
  106. ext = "." + parts[parts.Length - 2] + ext;
  107. }
  108. }
  109. else
  110. {
  111. ext = "." + parts[1];
  112. }
  113. // Strip out invalid parts
  114. ext = ext.Split('#')[0];
  115. ext = ext.Split('?')[0];
  116. return ext;
  117. }
  118. }
  119. }