private void InitializeInstance(CFDataFile src, int?id, RequestContext ctx, string controller) { UrlHelper urlHelper = new UrlHelper(ctx); int idValue = id.HasValue ? id.Value : src.Id; Id = src.Id; FileName = src.FileName; Guid = src.Guid; Path = src.Path; Thumbnail = src.Thumbnail; ContentType = src.ContentType; TopMimeType = src.TopMimeType; ThumbnailType = src.ThumbnailType; //ThumbnailUrl = "url('" + urlHelper.Action("Thumbnail", controller, new //{ // id = idValue, // name = src.Guid //}) + "')"; if (src.ContentType.Contains("image")) { ThumbnailUrl = urlHelper.Action("Image", "Items", new { area = "", id = idValue, guid = src.Guid, size = "Thumbnail" }); } else { ThumbnailUrl = "/Content/Thumbnails/" + src.Thumbnail; } Url = urlHelper.Action("File", controller, new { id = idValue, guid = src.Guid }); }
public BulletinBoardItem(CFItem dataModel, RequestContext ctx, string fields) { CFDataFile file = dataModel.Files.FirstOrDefault(); FileViewModel vm = new FileViewModel(file, dataModel.Id, ctx); Id = dataModel.Id; Thumbnail = vm.Thumbnail; Image = vm.Url; List <string> requiredFields = fields != null?fields.ToLower().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList() : new List <string>(); Metadata = new List <MetadataFieldValue>(); foreach (CFMetadataSet ms in dataModel.MetadataSets) { foreach (FormField field in ms.Fields) { if (requiredFields.Contains(field.GetName().ToLower())) { List <TextValue> vals = field.GetValues().Where(tv => !string.IsNullOrEmpty(tv.Value)).ToList(); if (vals.Count > 0) { Metadata.Add(new MetadataFieldValue() { FieldName = field.GetName(), FieldValues = vals }); } } } } }
private void InitializeInstance(CFDataFile src, int?id, RequestContext ctx, string controller) { UrlHelper urlHelper = new UrlHelper(ctx); int idValue = id.HasValue ? id.Value : src.Id; Id = src.Id; FileName = src.FileName; Guid = src.Guid; Path = src.Path; Thumbnail = src.Thumbnail; ContentType = src.ContentType; TopMimeType = src.TopMimeType; ThumbnailType = src.ThumbnailType; ThumbnailUrl = "url('" + urlHelper.Action("Thumbnail", controller, new { id = idValue, name = src.Guid }) + "')"; Url = urlHelper.Action("File", controller, new { id = idValue, guid = src.Guid }); }
//XXX Duplicating code from ItemService.cs UpdateFiles method private void MoveFileToField(CFDataFile dataFile, FormField field) { //DataFile dataFile = fileDescription.DataFile; //moving the physical files from the temporary upload folder to a folder identified by the GUID of the //item inside the uploaded data folder string dstDir = Path.Combine(ConfigHelper.DataRoot, field.MappedGuid); if (!Directory.Exists(dstDir)) { Directory.CreateDirectory(dstDir); } string srcFile = Path.Combine(dataFile.Path, dataFile.LocalFileName); string dstFile = Path.Combine(dstDir, dataFile.LocalFileName); File.Move(srcFile, dstFile); //moving the thumbnail, if it's not a shared one if (dataFile.ThumbnailType == CFDataFile.eThumbnailTypes.NonShared) { string srcThumbnail = Path.Combine(dataFile.Path, dataFile.Thumbnail); string dstThumbnail = Path.Combine(dstDir, dataFile.Thumbnail); File.Move(srcThumbnail, dstThumbnail); } //updating the file path dataFile.Path = dstDir; }
public override object GetContent(object model) { //For testing -- go to the page that use this region and add ?entity=[entityId] HttpContext context = HttpContext.Current; if (context != null) { string entityId = context.Request.QueryString[EntityImagePanel.ENTITY_PARAM]; CatfishDbContext db = new CatfishDbContext(); EntityService es = new EntityService(db); if (!string.IsNullOrEmpty(entityId)) //get it from url param { Entity = es.GetAnEntity(Convert.ToInt32(entityId)); EntityId = Entity.Id; } else { if (EntityId > 0) //the default entity Id { Entity = es.GetAnEntity(Convert.ToInt32(EntityId)); } } if (Entity != null) { foreach (var f in ((CFItem)Entity).Files) { CFDataFile img = f; FileGuid = f.Guid; } } } return(base.GetContent(model)); }
protected void UpdateFiles(Attachment srcAttachmentField, CFItem dstItem) { List <string> keepFileGuids = srcAttachmentField.FileGuids.Split(new char[] { Attachment.FileGuidSeparator }, StringSplitOptions.RemoveEmptyEntries).ToList(); //Removing attachments that are in the dbModel but not in attachments to be kept foreach (CFDataFile file in dstItem.Files.ToList()) { if (keepFileGuids.IndexOf(file.Guid) < 0) { //Deleting the file node from the XML Model dstItem.RemoveFile(file); } } //Adding new files foreach (string fileGuid in keepFileGuids) { if (dstItem.Files.Where(f => f.Guid == fileGuid).Any() == false) { CFDataFile file = Db.XmlModels.Where(m => m.MappedGuid == fileGuid) .Select(m => m as CFDataFile) .FirstOrDefault(); if (file != null) { dstItem.AddData(file); //since the data object has now been inserted into the submission item, it is no longer needed //to stay as a stanalone object in the XmlModel table. Db.XmlModels.Remove(file); //moving the physical files from the temporary upload folder to a folder identified by the GUID of the //item inside the uploaded data folder string dstDir = Path.Combine(ConfigHelper.DataRoot, dstItem.MappedGuid); if (!Directory.Exists(dstDir)) { Directory.CreateDirectory(dstDir); } string srcFile = Path.Combine(file.Path, file.LocalFileName); string dstFile = Path.Combine(dstDir, file.LocalFileName); File.Move(srcFile, dstFile); //moving the thumbnail, if it's not a shared one if (file.ThumbnailType == CFDataFile.eThumbnailTypes.NonShared) { string srcThumbnail = Path.Combine(file.Path, file.Thumbnail); string dstThumbnail = Path.Combine(dstDir, file.Thumbnail); File.Move(srcThumbnail, dstThumbnail); } //updating the file path file.Path = dstDir; } } } }
public CFDataFile InjestFile(Stream srcStream, string inputFileName, string contentType, string dstPath) { dstPath = Path.Combine(ConfigHelper.UploadRoot, dstPath); if (!Directory.Exists(dstPath)) { Directory.CreateDirectory(dstPath); if (!Directory.Exists(dstPath)) { throw new Exception("Unable to create the upload folder " + dstPath); } } CFDataFile file = new CFDataFile() { FileName = inputFileName, Path = dstPath, ContentType = contentType }; using (FileStream dstFileStream = File.Create(Path.Combine(file.Path, file.LocalFileName))) { srcStream.Seek(0, SeekOrigin.Begin); srcStream.CopyTo(dstFileStream); } if (file.ContentType.StartsWith("image/")) { file.Thumbnail = CreateThumbnailName(file.Guid, file.Extension); file.ThumbnailType = CFDataFile.eThumbnailTypes.NonShared; using (Image image = new Bitmap(file.AbsoluteFilePathName)) { Size thumbSize = image.Width < image.Height ? new Size() { Height = ConfigHelper.ThumbnailSize, Width = (image.Width * ConfigHelper.ThumbnailSize) / image.Height } : new Size() { Width = ConfigHelper.ThumbnailSize, Height = (image.Height * ConfigHelper.ThumbnailSize) / image.Width }; Image thumbnail = image.GetThumbnailImage(thumbSize.Width, thumbSize.Height, null, IntPtr.Zero); ImageFormat format = GetThumbnailFormat(file.Extension); thumbnail.Save(Path.Combine(file.Path, file.Thumbnail), format); } } else { file.Thumbnail = GetThumbnail(file.ContentType); file.ThumbnailType = CFDataFile.eThumbnailTypes.Shared; } return(file); }
public ActionResult File(int id, string guid) { CFDataFile file = DataService.GetFile(id, guid); if (file == null) { return(HttpNotFound("File not found")); } string path_name = Path.Combine(file.Path, file.LocalFileName); return(new FilePathResult(path_name, file.ContentType)); }
public CFDataFile ToDataFile() { CFDataFile dataFile = new CFDataFile(); dataFile.Id = Id; dataFile.FileName = FileName; dataFile.Guid = Guid; dataFile.Path = Path; dataFile.Thumbnail = Thumbnail; dataFile.ThumbnailType = ThumbnailType; dataFile.ContentType = ContentType; return(dataFile); }
private Ingestion DeserializeAggregations(XmlReader reader) { while (reader.Read()) { if (reader.IsStartElement()) { string name = reader.LocalName; CFXmlModel model = null; string strGuid = reader.GetAttribute("guid"); switch (name) { case "collection": model = new CFCollection(); break; case "item": model = new CFItem(); break; case "form": model = new Form(); break; case "file": model = new CFDataFile(); break; default: throw new FormatException("Invalid XML element: " + reader.Name); } if (model != null) { model.Guid = strGuid; model.MappedGuid = strGuid; model.Content = reader.ReadOuterXml(); Aggregations.Add(model); } } if (reader.NodeType == System.Xml.XmlNodeType.EndElement) { if (reader.Name == "aggregations") { return(this); } } } return(this); }
public void RemoveFile(CFDataFile file) { var xpath = "./data/" + CFDataFile.TagName + "[@guid='" + file.Guid + "']"; XElement fileElement = GetChildElements(xpath, Data).FirstOrDefault(); if (fileElement == null) { throw new Exception("File does not exist."); } fileElement.Remove(); file.DeleteFilesFromFileSystem(); LogChange(file.Guid, "Deleted " + file.FileName); }
public ActionResult Thumbnail(int id, string name) { CFDataFile file = DataService.GetFile(id, name); if (file == null) { return(HttpNotFound("File not found")); } var test = file.ThumbnailType; string path_name = file.ThumbnailType == CFDataFile.eThumbnailTypes.Shared ? Path.Combine(FileHelper.GetThumbnailRoot(Request), file.Thumbnail) : Path.Combine(file.Path, file.Thumbnail); return(new FilePathResult(path_name, file.ContentType)); }
public bool DeleteStandaloneFile(string guid) { CFDataFile file = Db.XmlModels.Where(x => x.MappedGuid == guid).FirstOrDefault() as CFDataFile; if (file == null) { return(false); } //Deleting the file from the file system file.DeleteFilesFromFileSystem(); //Deleting the file object from the database Db.XmlModels.Remove(file); return(true); }
private void UpdateFileList(FormField field) { List <CFDataFile> filesList = new List <CFDataFile>(); foreach (CFFileDescription fileDescription in field.Files) { string fileGuid = fileDescription.Guid; CFDataFile file = Db.XmlModels.Where(m => m.MappedGuid == fileGuid) .Select(m => m as CFDataFile) .FirstOrDefault(); if (file != null) { MoveFileToField(file, field); fileDescription.DataFile = file; Db.XmlModels.Remove(file); } } Db.SaveChanges(); }
public ActionResult File(int id, string guid) { //This is an unprotected method so it only returns if the ids of the file is in the session. // i.e. the file was uploaded during the current session if (!FileHelper.CheckGuidCache(Session, guid)) { return(HttpNotFound("File not found")); } CFDataFile file = DataService.GetFile(id, guid); if (file == null) { return(HttpNotFound("File not found")); } string path_name = Path.Combine(file.Path, file.LocalFileName); return(new FilePathResult(path_name, file.ContentType)); }
public ActionResult Thumbnail(int id, string name) { //This is an unprotected method so it only returns if the GUID name of the file is in the session. // i.e. the file was uploaded during the current session if (!FileHelper.CheckGuidCache(Session, name)) { return(HttpNotFound("File not found")); } CFDataFile file = DataService.GetFile(id, name); if (file == null) { return(HttpNotFound("File not found")); } string path_name = file.ThumbnailType == CFDataFile.eThumbnailTypes.Shared ? Path.Combine(FileHelper.GetThumbnailRoot(Request), file.Thumbnail) : Path.Combine(file.Path, file.Thumbnail); return(new FilePathResult(path_name, file.ContentType)); }
private Ingestion DeserializeAggregations(XElement element) { foreach (XElement child in element.Elements()) { string name = child.Name.LocalName; CFXmlModel model = null; string strGuid = child.Attribute("guid").Value; switch (name) { case "collection": model = new CFCollection(); break; case "item": model = new CFItem(); break; case "form": model = new Form(); break; case "file": model = new CFDataFile(); break; } if (model != null) { model.Guid = strGuid; model.MappedGuid = strGuid; model.Content = child.ToString(); Aggregations.Add(model); } } return(this); }
public CFDataFile InjestFile(Stream srcStream, string inputFileName, string contentType, string dstPath, int maxPixelSize) { dstPath = Path.Combine(ConfigHelper.UploadRoot, dstPath); if (!Directory.Exists(dstPath)) { Directory.CreateDirectory(dstPath); if (!Directory.Exists(dstPath)) { throw new Exception("Unable to create the upload folder " + dstPath); } } //change the content type to image/png if the content type is image/tif contentType = contentType == "image/tiff" ? "image/png" : contentType; //Dec 17 2019 -- change the contentType to 'audio/webm' if the file is *.webm contentType = inputFileName.Contains(".webm") ? "audio/webm" : contentType; CFDataFile file = new CFDataFile() { FileName = inputFileName, Path = dstPath, ContentType = contentType }; using (FileStream dstFileStream = File.Create(Path.Combine(file.Path, file.LocalFileName))) { srcStream.Seek(0, SeekOrigin.Begin); srcStream.CopyTo(dstFileStream); } if (file.ContentType.StartsWith("image/")) { file.Thumbnail = CreateThumbnailName(file.Guid, file.Extension); file.ThumbnailType = CFDataFile.eThumbnailTypes.NonShared; //August 1 2018 -- create different size of image file.Small = CreateVariatyImageSizeName(file.Guid, file.Extension, "Small"); file.Medium = CreateVariatyImageSizeName(file.Guid, file.Extension, "Medium"); file.Large = CreateVariatyImageSizeName(file.Guid, file.Extension, "Large"); ImageFormat format = GetImageFormat(file.Extension); Action <string, BitmapData, ColorPalette, int, bool> saveImage = (path, image, palette, sizeVal, ignoreIfSmaller) => { int width = image.Width; int height = image.Height; if (ignoreIfSmaller && width < sizeVal && height < sizeVal) { return; } Size imgSize = width < height ? new Size() { Height = sizeVal, Width = (width * sizeVal) / height } : new Size() { Width = sizeVal, Height = (height * sizeVal) / width }; Image img = ResizeImage(image, palette, imgSize.Width, imgSize.Height); img.Save(path, format); img.Dispose(); }; // Add different image sizes. string resizedFilePath = null; using (Bitmap image = new Bitmap(file.AbsoluteFilePathName)) { var data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); ColorPalette palette = image.Palette; Parallel.Invoke( () => saveImage(Path.Combine(file.Path, file.Thumbnail), data, palette, (int)ConfigHelper.eImageSize.Thumbnail, false), () => saveImage(Path.Combine(file.Path, file.Small), data, palette, (int)ConfigHelper.eImageSize.Small, false), () => saveImage(Path.Combine(file.Path, file.Medium), data, palette, (int)ConfigHelper.eImageSize.Medium, false), () => saveImage(Path.Combine(file.Path, file.Large), data, palette, (int)ConfigHelper.eImageSize.Large, false) ); if (maxPixelSize > 0) { resizedFilePath = Path.Combine(file.Path, CreateVariatyImageSizeName(file.Guid, file.Extension, "Resize")); saveImage(resizedFilePath, data, palette, maxPixelSize, true); } } if (resizedFilePath != null && File.Exists(resizedFilePath)) { string path = Path.Combine(file.Path, file.LocalFileName); File.Delete(path); File.Move(resizedFilePath, path); } } else { file.Thumbnail = GetThumbnail(file.ContentType); file.ThumbnailType = CFDataFile.eThumbnailTypes.Shared; } return(file); }
public DataFileViewModel(CFDataFile dataFile, int parentId) : this() { ParentId = parentId; Guid = dataFile.Guid; MimeType = dataFile.TopMimeType; }
public CFFileDescription() { DataFile = new CFDataFile(); Label = ""; FileOptions = new CFFileOptions(); }
public FileViewModel(CFDataFile src, int?itemId, RequestContext ctx) { InitializeInstance(src, itemId, ctx, "Items"); }
public FileViewModel(CFDataFile src, int?id) { InitializeInstance(src, id, HttpContext.Current.Request.RequestContext, "Items"); }
public FileViewModel(CFDataFile src, int?id, RequestContext ctx, string controller) { InitializeInstance(src, id, ctx, controller); }
//August 1 2018 -- get image with different size /// <summary> /// If no size provided, it will return regular size image /// </summary> /// <param name="id">EntityId</param> /// <param name="guid">File Guid</param> /// <param name="size">image size (i.e: Thumbnail, small, medium, large)</param> /// <returns></returns> public ActionResult Image(int id, string guid, string size = null) { ConfigHelper.eImageSize?eSize = null; if (!string.IsNullOrEmpty(size)) { eSize = (ConfigHelper.eImageSize)Enum.Parse(typeof(ConfigHelper.eImageSize), size); } CFDataFile file = null; if (!string.IsNullOrEmpty(guid)) { file = DataService.GetFile(id, guid); } if (file == null) { return(HttpNotFound("File not found")); } string path_name = string.Empty; string[] fnames = file.LocalFileName.Split('.'); string jpgExt = fnames[1]; //string jpgExt = (fnames[1] == "jpeg" || fnames[1] == "jpg") ? "jpg" : fnames[1]; if (fnames[1] == "jpeg" || fnames[1] == "jpg") { jpgExt = "jpg"; } else if (fnames[1] == "png" || fnames[1] == "tif" || fnames[1] == "tiff") { jpgExt = "png"; } if (eSize == null) //get original size { if (fnames[1] == "tif") { //chrome ccan't display tif image string localFileNamePath = Path.Combine(file.Path, file.LocalFileName); //fnames[0] + ".png"; string pngFilePath = (localFileNamePath.Split('.'))[0] + ".png"; //make a copy of .tif image save it as .png if none existed yet if (!System.IO.File.Exists(pngFilePath)) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(localFileNamePath); bitmap.Save(pngFilePath, ImageFormat.Png); } path_name = pngFilePath; } else { path_name = Path.Combine(file.Path, file.LocalFileName); } } else if (eSize.Equals(ConfigHelper.eImageSize.Thumbnail)) { path_name = Path.Combine(file.Path, fnames[0] + "_t." + jpgExt);// fnames[1]); } else if (eSize.Equals(ConfigHelper.eImageSize.Small)) { path_name = Path.Combine(file.Path, fnames[0] + "_s." + jpgExt); } else if (eSize.Equals(ConfigHelper.eImageSize.Medium)) { path_name = Path.Combine(file.Path, fnames[0] + "_m." + jpgExt); } else if (eSize.Equals(ConfigHelper.eImageSize.Large)) { path_name = Path.Combine(file.Path, fnames[0] + "_l." + jpgExt); } FilePathResult filePathResult = new FilePathResult(path_name, file.ContentType); return(filePathResult); // Json(filePathResult, JsonRequestBehavior.AllowGet); }
private void InitializeDataFile(CFDataFile dataFile) { Data.Add(dataFile.Data); }