Created
April 24, 2017 17:32
-
-
Save MichaelObi/2d38617a879ca408b1730c35b3399a35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FileUtils | |
{ | |
/// <summary> | |
/// Saves file into a local directory and returns the path + file name as string | |
/// </summary> | |
/// <param name="file">File</param> | |
/// <param name="filePath"></param> | |
/// <param name="fileName"></param> | |
/// <param name="saveLocal"></param> | |
/// <returns></returns> | |
public static FileSaveResponse SaveFile(HttpPostedFileBase file, string filePath, bool saveLocal = true, int? resizeWidth = null, int? resizeHieght = null) | |
{ | |
if (file == null) return null; | |
var response = new FileSaveResponse(); | |
try | |
{ | |
response.MimeType = file.ContentType; | |
response.FileSize = file.ContentLength; | |
response.OriginalFileName = file.FileName; | |
//fileName = Path.GetFileName(fileName); | |
//get file extension | |
//var fileExtension = Path.GetExtension(fileName); | |
var path = (saveLocal) ? HttpContext.Current.Server.MapPath(AppConstants.General.RootFolder + filePath) : filePath; | |
var exists = Directory.Exists(path); | |
if (!exists) Directory.CreateDirectory(path); | |
var fileName = GenerateRandomFileName(Path.GetExtension(file.FileName)); | |
response.FileName = fileName; | |
var fullPath = Path.Combine(path, fileName); | |
if (!File.Exists(fullPath)) | |
{ | |
// | |
file.SaveAs(fullPath); | |
//} | |
} | |
response.UploadStatus = true; | |
} | |
catch (IOException ex) | |
{ | |
//TO DO: Log the exception | |
response.UploadStatus = false; | |
} | |
return response; | |
} | |
public static void SaveResizedImage(HttpPostedFileBase file, int newWidth, int newHiegth, string fullPath) | |
{ | |
var image = Image.FromStream(file.InputStream, true, true); | |
var thumb = image.GetThumbnailImage(newWidth, newHiegth, () => false, IntPtr.Zero); | |
thumb.Save(fullPath); | |
} | |
public static byte[] GetFileBytes(string filePath, string fileName) | |
{ | |
var fullFileName =string.Format("{0}{1}", HttpContext.Current.Server.MapPath(AppConstants.General.RootFolder + filePath), | |
fileName); | |
return File.ReadAllBytes(fullFileName); | |
} | |
public static SmartBookResponse CreateSmartBook(string fileName) | |
{ | |
try | |
{ | |
//var fileName = viewModel.CourseContentFile.FileName; | |
var converter = HttpContext.Current.Server.MapPath(AppConstants.General.RootFolder + Config.PdfToHtmlCommand); | |
//var converter = @"C:\Users\philemon\Downloads\pdf2htmlEX-win32-0.14.6-upx-with-poppler-data\pdf2htmlEX.exe"; | |
var uploadPath = AppConstants.General.RootFolder + Config.CourseFilesUploadPath; | |
string[] fileNameHolder = fileName.Split('.'); | |
var newUploadPath = HttpContext.Current.Server.MapPath(uploadPath) + fileNameHolder[0]; | |
var fullPath = HttpContext.Current.Server.MapPath(uploadPath) + fileName; | |
var argument = string.Format(Config.PdfToHtmlArgument, newUploadPath, fileNameHolder[0], fullPath, newUploadPath); | |
Process p = new Process(); | |
p.StartInfo.FileName = converter; | |
p.StartInfo.UseShellExecute = false; | |
p.StartInfo.RedirectStandardInput = true; | |
p.StartInfo.Arguments = argument; | |
p.StartInfo.RedirectStandardError = true; | |
p.Start(); | |
var error = p.StandardError.ReadToEnd(); | |
p.WaitForExit(); | |
p.Close(); | |
var currentDirectoryInfo = new DirectoryInfo(newUploadPath); | |
var jsonString = ""; | |
var dirName = currentDirectoryInfo.Name; | |
var pdfName = dirName + ".pdf"; | |
var cssfileName = dirName + ".css"; | |
jsonString += "\"File Name\":\"" + pdfName + "\","; | |
jsonString += "\"CSS Name\":\"" + cssfileName + "\",\"Pages\":"; | |
short total = 0; | |
var pageString = ""; | |
foreach (var filesInfo in currentDirectoryInfo.GetFileSystemInfos()) | |
{ | |
if (filesInfo.Extension != ".page") continue; | |
var pageNumber = filesInfo.Name.Replace(dirName, "").Replace(".page", "").Replace("-", ""); | |
pageString += "\"" + pageNumber + "\":\"" + filesInfo.Name + "\","; | |
total++; | |
} | |
pageString += "\"Total\":\"" + total + "\""; | |
pageString = "{" + pageString + "}"; | |
jsonString += pageString; | |
jsonString = "{" + jsonString + "}"; | |
var jsonFile = newUploadPath + @"\" + dirName + ".json"; | |
File.WriteAllText(jsonFile, jsonString); | |
var res = new SmartBookResponse | |
{ | |
TotalPages = total, | |
Error = error | |
}; | |
return res; | |
} | |
catch(Exception ex) | |
{ | |
var res = new SmartBookResponse | |
{ | |
TotalPages = 0, | |
Error = "Could not create smartbook due to error" + ex.Message | |
}; | |
return res; | |
} | |
} | |
/// <summary> | |
/// Converts File Image into Base64 string | |
/// </summary> | |
/// <param name="fileName"></param> | |
/// <returns></returns> | |
public static string ConvertImageToBase64(string fileName) | |
{ | |
using (var image = Image.FromFile(fileName)) | |
{ | |
using (var m = new MemoryStream()) | |
{ | |
image.Save(m, image.RawFormat); | |
var imageBytes = m.ToArray(); | |
// Convert byte[] to Base64 String | |
var base64String = Convert.ToBase64String(imageBytes); | |
return base64String; | |
} | |
} | |
} | |
/// <summary> | |
/// Converts base 64 string into an image file | |
/// </summary> | |
/// <param name="base64String">base 64 string</param> | |
/// <returns></returns> | |
public static Image ConvertBase64ToImageAndSaveImage(string base64String) | |
{ | |
var bytes = Convert.FromBase64String(base64String); | |
Image image; | |
using (var ms = new MemoryStream(bytes)) | |
{ | |
image = Image.FromStream(ms); | |
} | |
return image; | |
} | |
public static string GenerateRandomFileName(string fileExtension) | |
{ | |
var shortGuid = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), @"[^0-9a-zA-Z]+", ""); ; | |
return string.Format(@"{0}_{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), shortGuid, fileExtension); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment