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 () { } /// /// Retrieve FileType /// /// /// 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; } } /// /// Retrieve a File Type /// /// /// public XFileType GetType (string fileName) { // if (fileName.IsNullOrEmpty ()) { XException.InavlidFile.Throw (); } // var ext = GetFileExtension (fileName); var extType = GetExtensionType (ext); return GetType (extType); } /// /// used for validating Extension in Widget Upload ... /// /// an string array public string[] GetAllowedExtensionsForWidgetUpload () { return new [] { FileExtensions.SAHER_DASHBOARD_WIDGET.GetStringValue () }; } /// /// Get a Files Extension string /// /// /// public string GetFileExtension (string fileName) { // // Validate Args ... if (fileName.IsNullOrEmpty ()) { return null; } // var fileExt = ""; try { fileExt = Path.GetExtension (fileName); } catch { } // return fileExt; } /// /// Get a File Name Without it's Extension /// /// /// public string GetFileNameWithoutExtension (string fileName) { // // Validate Args ... if (fileName.IsNullOrEmpty ()) { return null; } // var fileNameWithoutExt = ""; try { fileNameWithoutExt = Path .GetFileNameWithoutExtension (fileName); } catch { } // return fileNameWithoutExt; } /// /// Get File Extension Type /// /// /// public int GetExtensionType (string fileExtension) { // foreach (var extItem in Enum .GetValues (typeof (FileExtensions)) .Cast ()) { if (extItem.GetStringValue () .Contains (fileExtension.ToLower ())) { return (int) extItem; } } // return -1; } } }