2018-06-15 02:57:03 +02:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Win32;
|
2017-07-27 09:52:23 +02:00
|
|
|
|
using MimeDetective;
|
|
|
|
|
using MimeDetective.Extensions;
|
2016-01-18 07:05:20 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2015-12-22 08:54:22 +01:00
|
|
|
|
|
2017-01-18 09:12:22 +01:00
|
|
|
|
namespace Teknik.Utilities
|
2015-12-22 08:54:22 +01:00
|
|
|
|
{
|
2017-01-18 09:12:22 +01:00
|
|
|
|
public static class FileHelper
|
2015-12-22 08:54:22 +01:00
|
|
|
|
{
|
2017-01-18 23:36:44 +01:00
|
|
|
|
public static string GenerateRandomFileName(string path, string extension, int length)
|
2016-01-18 07:05:20 +01:00
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
2017-01-10 04:56:26 +01:00
|
|
|
|
string filename = StringHelper.RandomString(length);
|
2016-01-21 19:52:04 +01:00
|
|
|
|
string subDir = filename[0].ToString();
|
|
|
|
|
path = Path.Combine(path, subDir);
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
}
|
2016-01-18 07:05:20 +01:00
|
|
|
|
while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
|
|
|
|
|
{
|
2017-01-10 04:56:26 +01:00
|
|
|
|
filename = StringHelper.RandomString(length);
|
2016-01-21 19:52:04 +01:00
|
|
|
|
subDir = filename[0].ToString();
|
|
|
|
|
path = Path.Combine(path, subDir);
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
}
|
2016-01-18 07:05:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 23:36:44 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Append a number to the end of a file name to make it unique, incrementing the number if needed
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">the name of the file, including extension</param>
|
|
|
|
|
/// <param name="destinationFolder">the destination folder in which to check for uniqueness</param>
|
|
|
|
|
/// <returns>the file name, without folder, with extension, with a unique index as the suffix, if necessary</returns>
|
|
|
|
|
public static string MakeUniqueFilename(string file, string destinationFolder)
|
|
|
|
|
{
|
|
|
|
|
string fileName = string.Empty;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string fileBase = Path.GetFileNameWithoutExtension(file);
|
|
|
|
|
string fileExt = Path.GetExtension(file);
|
|
|
|
|
fileName = string.Format("{0}{1}", fileBase, fileExt);
|
|
|
|
|
string filePath = Path.Combine(destinationFolder, fileName);
|
|
|
|
|
|
|
|
|
|
int iteration = 1;
|
|
|
|
|
while (File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
fileName = string.Format("{0} ({1}){2}", fileBase, iteration, fileExt);
|
|
|
|
|
filePath = Path.Combine(destinationFolder, fileName);
|
|
|
|
|
iteration++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Throw up exception to parent
|
|
|
|
|
throw new Exception(string.Format("Exception occured in FileHelper.MakeUniqueFilename({0}, {1})", file, destinationFolder), ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileName;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 09:12:22 +01:00
|
|
|
|
public static string GetDefaultExtension(string mimeType)
|
|
|
|
|
{
|
|
|
|
|
return GetDefaultExtension(mimeType, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetDefaultExtension(string mimeType, string defaultExtension)
|
2016-01-18 07:05:20 +01:00
|
|
|
|
{
|
|
|
|
|
string result;
|
|
|
|
|
RegistryKey key;
|
|
|
|
|
object value;
|
|
|
|
|
|
|
|
|
|
key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
|
|
|
|
|
value = key != null ? key.GetValue("Extension", null) : null;
|
2016-01-27 23:18:31 +01:00
|
|
|
|
result = value != null ? value.ToString() : defaultExtension;
|
2016-01-18 07:05:20 +01:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-07-27 09:52:23 +02:00
|
|
|
|
|
|
|
|
|
public static string GetDefaultExtension(Stream fileStream, string defaultExtension)
|
|
|
|
|
{
|
|
|
|
|
FileType fileType = fileStream.GetFileType();
|
|
|
|
|
if (!string.IsNullOrEmpty(fileType.Extension))
|
|
|
|
|
return fileType.Extension;
|
|
|
|
|
return defaultExtension;
|
|
|
|
|
}
|
2018-06-15 02:57:03 +02:00
|
|
|
|
|
|
|
|
|
public static string MapPath(IHostingEnvironment env, string file)
|
|
|
|
|
{
|
|
|
|
|
var webRoot = env.WebRootPath;
|
|
|
|
|
return Path.Combine(webRoot, file);
|
|
|
|
|
}
|
2015-12-22 08:54:22 +01:00
|
|
|
|
}
|
2017-01-18 09:12:22 +01:00
|
|
|
|
}
|