public IHasBinaryData PopulateFromFile(string filePath, IHasBinaryData dataObject) { FileStream fs = null; try { fs = System.IO.File.Open(filePath, FileMode.Open); if (fs != null) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); dataObject.Data = bytes; } } catch (Exception e) { throw; } finally { if (fs != null) { fs.Close(); } } return dataObject; }
public void DeleteTemporaryFile(IHasBinaryData hasBinaryData) { string filePath = this.ConstructTempFilePath(hasBinaryData); //If a file already exists, delete it... if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } }
public void Create(IHasBinaryData bd) { string filePath = this.ConstructTempFilePath(bd); //If a file already exists, delete it... if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } System.IO.File.WriteAllBytes(filePath, bd.Data); }
public IHasBinaryData Fetch(IHasBinaryData hasBinaryData) { string path = ConstructTempFilePath(hasBinaryData); try { hasBinaryData = FileHelpers.PopulateFromFile(path, hasBinaryData); } catch (FileNotFoundException e) { //There is no file - therefore we consume this exception } return hasBinaryData; }
public bool IsFileImage(IHasBinaryData dataObject) { return this.IsFileImage(dataObject.Data); }
public string ConstructTempFileURL(IHasBinaryData hasBinaryData) { return RouteHelpers.TemporaryFileUrl(hasBinaryData.ID); }
public string ConstructTempFilePath(IHasBinaryData hasBinaryData) { var absolutePath = System.Configuration.ConfigurationSettings.AppSettings["ApplicationPath"] + System.Configuration.ConfigurationSettings.AppSettings["ApplicationRelativeTempFileDirectoryPath"] + "/" + hasBinaryData.ID + ".temp"; return absolutePath; }
/// <summary> /// A model binder *may* have populated our model with byte data. /// If so we need to temporarily persist the data, so that if need be we can access it later, if its valid /// If the post didnt contain byte data, we may have a temporary file still associated with this model, /// so we grab the data and populate the file. /// </summary> /// <param name="model"></param> /// <param name="fileValidationHelper"></param> public IHasBinaryData HandleTemporaryFile(IHasBinaryData model, IFileValidationHelper fileValidationHelper) { bool isExistingTemporaryFile = false; if (model.Data == null || model.Data.Length == 0) { //The model doesnt have any data.... lets try to grab some model = (UploadFilePartialViewModel)TemporaryFileStrategy.Fetch(model); isExistingTemporaryFile = true; } if (model.Data == null || model.Data.Length == 0) { //There isnt any data so we can return... return model; } //is the data valid? if (!fileValidationHelper.IsValidFileType(model.Data)) { //Post 1: - uploads valid temporary file //Post 2: - uploads invalid temporary file //We dont want to keep the data associated with post 1, so we attempt a delete just in case.. TemporaryFileStrategy.DeleteTemporaryFile(model); //The data is invalid, therefore we will remove it... //model.Data = null; //Throw an exception throw new InvalidFileTypeException(); } if (isExistingTemporaryFile==false) { //Persist the file for later use if the file is the correct type TemporaryFileStrategy.Create(model); } return model; }
public static string RenderTemporaryImage(this HtmlHelper helper, IHasBinaryData binaryData, string id, string alternateText, object htmlAttributes) { ITemporaryFileStrategy temporaryFileStrategy = IOCHelper.GetTemporaryFileStrategy(); string path = temporaryFileStrategy.ConstructTempFileURL(binaryData); string output = output = RenderImage(helper, path, id, alternateText, htmlAttributes); return output; }