Пример #1
0
        /// <summary>
        /// Attaches a file to a mission
        /// </summary>
        /// <param name="id"></param>
        /// <param name="attachmentDTO"></param>
        /// <param name="currentUserId"></param>
        /// <returns></returns>
        public MissionAttachmentDTO Attach(int id, MissionAttachmentDTO attachmentDTO, string currentUserId)
        {
            if (attachmentDTO.MissionId != id)
            {
                throw new ArgumentException("Trying to attach a file to a different mission.", nameof(attachmentDTO.Mission));
            }

            var attachment = AutoMapper.Mapper.Map <MissionAttachment>(attachmentDTO);

            // Mission
            attachment.CreatedByUserId = currentUserId;
            attachment.CreatedDate     = DateTime.Now;

            ValidateAttachment(attachment);

            using (var unitOfWork = new UnitOfWork())
            {
                // hard-delete any existing attachments for this mission
                var existingAttachments = unitOfWork.MissionAttachments.Find(x => x.MissionId == id);
                if (existingAttachments != null)
                {
                    unitOfWork.MissionAttachments.HardDeleteRange(existingAttachments);
                }
                unitOfWork.MissionAttachments.Add(attachment);
                unitOfWork.SaveChanges();
                attachment    = unitOfWork.MissionAttachments.GetFull(attachment.Id);
                attachmentDTO = AutoMapper.Mapper.Map <MissionAttachmentDTO>(attachment);
            }

            return(attachmentDTO);
        }
Пример #2
0
        public async Task <IHttpActionResult> Upload(int id)
        {
            var identity = RequestContext.Principal.Identity;
            var userId   = identity.GetUserId();

            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root     = HttpContext.Current.Server.MapPath("~/Uploads");
            var    provider = new CustomMultipartFormDataStreamProvider(root);

            try
            {
                // Read the form data.
                await Request.Content.ReadAsMultipartAsync(provider);

                var file = provider.FileData.FirstOrDefault();
                if (file == null)
                {
                    return(BadRequest("Cannot fetch the uploaded file."));
                }

                // This illustrates how to get the file names.
                var files = new List <string>
                {
                    Path.GetFileName(file.LocalFileName)
                };
                System.Diagnostics.Debug.WriteLine(file.Headers.ContentDisposition.FileName);
                System.Diagnostics.Debug.WriteLine("Server file path: " + file.LocalFileName);

                var attachmentDTO = new MissionAttachmentDTO
                {
                    MissionId        = id,
                    OriginalFilename = Path.GetFileName(file.LocalFileName),
                    FileType         = Path.GetExtension(file.LocalFileName),
                    FileName         = Path.Combine(root, file.LocalFileName),
                    AWSPublicUrl     = $@"{Api.ApiBaseUrl}/Uploads/{Path.GetFileName(file.LocalFileName)}"
                };

                attachmentDTO = AppService.Missions.Attach(id, attachmentDTO, userId);

                return(Ok(attachmentDTO));
            }
            catch (Exception ex)
            {
                AppLogger.Logger.Error(ex);

                return(InternalServerError(ex));
            }
        }