135 lines
4.2 KiB
C#
135 lines
4.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using xCommons.Constants;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xStorageService.Constants;
|
|
|
|
namespace xStorageService.Helpers {
|
|
public partial class XStorageHelper {
|
|
|
|
private int[] DocumentTypeThreshold = new int[] { 99, 307 };
|
|
private int[] ImageTypeThreshold = new int[] { 599, 606 };
|
|
private int[] AudioTypeThreshold = new int[] { 499, 504 };
|
|
private int[] VideoTypeThreshold = new int[] { 399, 405 };
|
|
|
|
public XStorageHelper () { }
|
|
|
|
/// <summary>
|
|
/// Retrieve FileType
|
|
/// </summary>
|
|
/// <param name="fileExtension"></param>
|
|
/// <returns></returns>
|
|
public XFileType GetType (int fileExtension) {
|
|
//
|
|
// Compare File Extension Integer Value and
|
|
// Find Propper Type for it ...
|
|
if (fileExtension > DocumentTypeThreshold[0] && fileExtension < DocumentTypeThreshold[1]) {
|
|
return XFileType.Document;
|
|
} else if (fileExtension > VideoTypeThreshold[0] && fileExtension < VideoTypeThreshold[1]) {
|
|
return XFileType.Video;
|
|
} else if (fileExtension > AudioTypeThreshold[0] && fileExtension < AudioTypeThreshold[1]) {
|
|
return XFileType.Audio;
|
|
} else if (fileExtension > ImageTypeThreshold[0] && fileExtension < ImageTypeThreshold[1]) {
|
|
return XFileType.Image;
|
|
} else {
|
|
//
|
|
// if not in case return Document Type
|
|
return XFileType.Document;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a File Type
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public XFileType GetType (string fileName) {
|
|
//
|
|
if (fileName.IsNullOrEmpty ()) {
|
|
XException.InavlidFile.Throw ();
|
|
}
|
|
|
|
//
|
|
var ext = GetFileExtension (fileName);
|
|
var extType = GetExtensionType (ext);
|
|
return GetType (extType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// used for validating Extension in Widget Upload ...
|
|
/// </summary>
|
|
/// <returns>an string array</returns>
|
|
public string[] GetAllowedExtensionsForWidgetUpload () {
|
|
return new [] {
|
|
FileExtensions.SAHER_DASHBOARD_WIDGET.GetStringValue ()
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a Files Extension string
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public string GetFileExtension (string fileName) {
|
|
//
|
|
// Validate Args ...
|
|
if (fileName.IsNullOrEmpty ()) {
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var fileExt = "";
|
|
try {
|
|
fileExt = Path.GetExtension (fileName);
|
|
} catch { }
|
|
|
|
//
|
|
return fileExt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a File Name Without it's Extension
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public string GetFileNameWithoutExtension (string fileName) {
|
|
//
|
|
// Validate Args ...
|
|
if (fileName.IsNullOrEmpty ()) {
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var fileNameWithoutExt = "";
|
|
try {
|
|
fileNameWithoutExt = Path
|
|
.GetFileNameWithoutExtension (fileName);
|
|
} catch { }
|
|
|
|
//
|
|
return fileNameWithoutExt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get File Extension Type
|
|
/// </summary>
|
|
/// <param name="fileExtension"></param>
|
|
/// <returns></returns>
|
|
public int GetExtensionType (string fileExtension) {
|
|
//
|
|
foreach (var extItem in Enum
|
|
.GetValues (typeof (FileExtensions))
|
|
.Cast<FileExtensions> ()) {
|
|
if (extItem.GetStringValue ()
|
|
.Contains (fileExtension.ToLower ())) {
|
|
return (int) extItem;
|
|
}
|
|
}
|
|
|
|
//
|
|
return -1;
|
|
}
|
|
}
|
|
} |