public ActionResult GetMediaItem(string id) { ActionResult res = Json(new ResultResponse(false, "Unknown error."), JsonRequestBehavior.AllowGet); try { var storedMediaItem = _mediaItemManager.GetOne(id); if (storedMediaItem != null) { var cd = new System.Net.Mime.ContentDisposition { FileName = storedMediaItem.Name, Inline = true }; Response.AppendHeader("Content-Disposition", cd.ToString()); res = File(storedMediaItem.Data, storedMediaItem.ContentType); } else { res = Json(new ResultResponse(false, "Couldn't find stored media item with ID = " + id + "."), JsonRequestBehavior.AllowGet); } } catch (Exception e) { res = Json(new ResultResponse(false, "Error: " + e.Message), JsonRequestBehavior.AllowGet); } return(res); }
public void SendMail(OutgoingMailModel mailModel) { // Send mail to recipient var attachments = new Dictionary <string, byte[]>(); if (mailModel.attachments != null) { foreach (var mediaId in mailModel.attachments) { var mediaItem = _mediaItemManager.GetOne(mediaId); if (mediaItem != null) { byte[] data = new byte[mediaItem.Data.Length]; mediaItem.Data.Read(data, 0, data.Length); attachments.Add(mediaItem.Name, data); } else { throw new Exception("Failed to fetch media item for id " + mediaId + "."); } // Dispose stream that is no longer needed. Handle this in some better way? mediaItem.Data.Dispose(); } } string body = mailModel.message; _exchangeMailWebApi.ConnectToExchangeService(ConfigurationManager.AppSettings["chalmersIllExhangeLogin"], ConfigurationManager.AppSettings["chalmersIllExhangePass"]); _exchangeMailWebApi.SendMailMessage(mailModel.OrderId, body, ConfigurationManager.AppSettings["chalmersILLMailSubject"], mailModel.recipientName, mailModel.recipientEmail, attachments); }
public ActionResult MigrateOrderItem(int nodeId) { var json = new ResultResponse(); try { var orderItemToBeMoved = _legacyOrderItemManager.GetOrderItem(nodeId); // There is an item to move. if (orderItemToBeMoved != null) { var existingOrderItem = _orderItemManager.GetOrderItem(orderItemToBeMoved.OrderId); // There is no item with the same order id already in the store. if (existingOrderItem == null) { // Copy and clear attachments and create the order item without attachments in the storage. var attachmentsToCopy = new List <OrderAttachment>(); foreach (var attachmentToBeMoved in orderItemToBeMoved.AttachmentList) { attachmentsToCopy.Add(attachmentToBeMoved); } orderItemToBeMoved.AttachmentList.Clear(); orderItemToBeMoved.Attachments = JsonConvert.SerializeObject(orderItemToBeMoved.AttachmentList); var newOrderItemNodeId = _orderItemManager.CreateOrderItemInDbFromOrderItemModel(orderItemToBeMoved); var newOrderItem = _orderItemManager.GetOrderItem(newOrderItemNodeId); if (newOrderItem != null) { // Create all the connected media items in the media storage. var newMediaItems = new List <MediaItemModel>(); bool successfullyAddedAllMediaItems = true; string failedMediaItemId = ""; string failedMediaItemName = ""; foreach (var attachmentToCopy in attachmentsToCopy) { var mediaItemToBeMoved = _legacyMediaItemManager.GetOne(attachmentToCopy.MediaItemNodeId); // Calculate the content type. string contentType = ""; if (attachmentToCopy.Title.EndsWith(".pdf")) { contentType = "application/pdf"; } else if (attachmentToCopy.Title.EndsWith(".txt")) { contentType = "text/plain"; } else if (attachmentToCopy.Title.EndsWith(".tif")) { contentType = "image/tiff"; } else if (attachmentToCopy.Title.EndsWith(".tiff")) { contentType = "image/tiff"; } if (!String.IsNullOrEmpty(contentType)) { var newMediaItem = _mediaItemManager.CreateMediaItem(attachmentToCopy.Title, newOrderItem.NodeId, newOrderItem.OrderId, mediaItemToBeMoved.Data, contentType); newMediaItems.Add(newMediaItem); } else { successfullyAddedAllMediaItems = false; failedMediaItemId = mediaItemToBeMoved.Id; failedMediaItemName = mediaItemToBeMoved.Name; break; } } // Connect all the newly created media items. if (successfullyAddedAllMediaItems) { foreach (var newMediaItem in newMediaItems) { _orderItemManager.AddExistingMediaItemAsAnAttachmentWithoutLogging(newOrderItemNodeId, newMediaItem.Id, newMediaItem.Name, newMediaItem.Url, true, false); } } else { throw new Exception("Failed to create media items connected to order item with ID = " + orderItemToBeMoved.NodeId + ". The troublesome media item has ID = " + failedMediaItemId + " and name '" + failedMediaItemName + "'."); } } else { throw new Exception("Failed to read back order item with ID = " + newOrderItem.NodeId + " after creating it in the current storage."); } } else { throw new Exception("There is already an order item with order ID = " + orderItemToBeMoved.OrderId + " in the current storage. Skipping move."); } } else { throw new Exception("There was no order item with node ID = " + nodeId + ". Nothing to move."); } json.Success = true; json.Message = "Successfully moved order with node ID = " + nodeId + "."; } catch (Exception e) { json.Success = false; json.Message = "Something went horribly wrong: " + e.Message; } return(Json(json, JsonRequestBehavior.AllowGet)); }