protected dynamic HandleFileDeleteRequest(dynamic args) { var tableName = (string)args.table_name; var id = (string)args.item_id; var fileName = (string)args.file_name; var directory = this.GetAttachmentFolder(tableName, id); var path = Path.Combine(directory, fileName); if (File.Exists(path)) { File.Delete(path); } dynamic contentItem = this.SiteDatabase.GetByIdAsJObject(tableName, int.Parse(id)); if (contentItem.Attachments != null) { var array = contentItem.Attachments as JArray; for (int i = 0; i < array.Count; i++) { var url = (string)array[i]["Url"]; if (url.EndsWith("/" + fileName)) { array.RemoveAt(i); this.SiteDatabase.UpsertRecord(tableName, contentItem); break; } } } BaseDataModule.AttachmentDeleted(this.Context, tableName, contentItem, path); return(contentItem); }
/// <summary> /// Handles file upload request, currently not consolidated with Attach File function /// </summary> /// <param name="args"></param> /// <returns></returns> protected dynamic HandleFileUploadRequest(dynamic args) { var tableName = (string)args.table_name; var id = (string)args.item_id; var path = this.GetAttachmentFolder(tableName, id); dynamic contentItem = this.SiteDatabase.GetByIdAsJObject(tableName, int.Parse(id)); var newFiles = new List <dynamic>(); string attachmentType = this.Request.Form["attachmentType"] == null ? string.Empty : this.Request.Form["attachmentType"].ToString(); var CreateDate = DateTime.Now; if ((bool)this.Request.Form.attachmentIsUnique == true) { if (this.Request.Files.Count() > 1) { throw new InvalidOperationException("Cannot upload multiple file if attachmentIsUnique is true"); } } foreach (var item in this.Request.Files) { var fileName = Path.GetFileName(item.Name); var filePath = Path.Combine(path, fileName); if (File.Exists(filePath)) { fileName = Path.GetFileNameWithoutExtension(item.Name) + Guid.NewGuid() + Path.GetExtension(item.Name); filePath = Path.Combine(path, fileName); } using (var fs = File.Create(filePath)) { item.Value.CopyTo(fs); newFiles.Add(new { CreateDate = CreateDate, AttachmentType = attachmentType, DisplayOrder = 0, Caption = string.Empty, Url = Path.Combine( "/Site", "attachments", tableName, id, fileName).Replace('\\', '/') }); } } if (contentItem.Attachments == null) { contentItem.Attachments = JArray.FromObject(newFiles); } else { if ((bool)this.Request.Form.attachmentIsUnique == true) { bool wasSet = false; foreach (JObject item in contentItem.Attachments as JArray) { if (item["AttachmentType"].ToString() == attachmentType.ToString()) { // same type with current type item["CreateDate"] = newFiles[0].CreateDate; // delete the one being replaced var directory = this.GetAttachmentFolder(tableName, id); var toDelete = Path.Combine(directory, Path.GetFileName(item["Url"].ToString())); // delete when url is not null if (!string.IsNullOrEmpty(item["Url"].ToString())) { File.Delete(toDelete); } BaseDataModule.AttachmentDeleted(this.Context, tableName, contentItem, item["Url"].ToString()); // set to new one item["Url"] = newFiles[0].Url; wasSet = true; break; } } if (wasSet == false) { contentItem.Attachments.Add(JObject.FromObject(newFiles.First())); } } else { foreach (var item in newFiles) { contentItem.Attachments.Add(JObject.FromObject(item)); } } } this.SiteDatabase.UpsertRecord(tableName, contentItem); BaseDataModule.NewAttachments(this.Context, tableName, contentItem, newFiles); return(contentItem); }