예제 #1
0
        protected ActivityDocument Seed(CreateActivityDocument command)
        {
            var existingEntity = _entities.Get <ActivityDocument>()
                                 .SingleOrDefault(x => x.ActivityValues.ActivityId == command.ActivityId && x.FileName == command.FileName);

            if (existingEntity == null)
            {
                using (var fileStream = File.OpenRead(string.Format("{0}{1}", BasePath, command.FileName)))
                {
                    command.Content = fileStream.ReadFully();
                    _createDocument.Handle(command);
                    return(command.CreatedActivityDocument);
                }
            }
            return(existingEntity);
        }
예제 #2
0
        public Task<HttpResponseMessage> PostDocuments(int activityId, string activityMode)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            //Activity activity = _queryProcessor.Execute(new ActivityById(activityId));
            //if (activity == null)
            //{
            //    //string message = string.Format("Activity Id {0} not found", activityId);
            //    //return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message);
            //    throw new HttpResponseException(HttpStatusCode.InternalServerError);
            //}

            ActivityValues activityValues =
                _queryProcessor.Execute(new ActivityValuesByActivityIdAndMode(activityId, activityMode));

            if (activityValues == null)
            {
                //string message = string.Format("Activity Id {0} not found", activityId);
                //return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message);
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            var provider = new MultipartMemoryStreamProvider();

            var task = Request.Content.ReadAsMultipartAsync(provider).
                ContinueWith<HttpResponseMessage>(t =>
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                    }

                    //Activity activity = _queryProcessor.Execute(new ActivityById(activityId));
                    //if (activity == null)
                    //{
                    //    string message = string.Format("Activity Id {0} not found", activityId);
                    //    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message);
                    //}

                    foreach (var item in provider.Contents)
                    {
                        string mimeType = item.Headers.ContentType.MediaType;
                        string filename = item.Headers.ContentDisposition.FileName;
                        char[] trimChars = { '"' };
                        filename = filename.Trim(trimChars).WithoutTrailingSlash();
                        string name = Path.GetFileNameWithoutExtension(filename);
                        string extension = Path.GetExtension(filename);

                        if (!String.IsNullOrEmpty(extension))
                        {
                            extension = extension.Substring(1);
                        }
                        else
                        {
                            // TBD - convert mime type to file extension
                            throw new HttpResponseException(HttpStatusCode.InternalServerError);
                        }

                        try
                        {
                            Stream stream = item.ReadAsStreamAsync().Result;
                            CreateActivityDocument activityDocumentCommand = null;

                            if (mimeType.Contains("image/"))
                            {
                                CreateImage createImageCommand = new CreateImage
                                {
                                    SourceStream = stream,
                                    Width = Int32.Parse(ConfigurationManager.AppSettings["ImageWidth"]),
                                    Height = Int32.Parse(ConfigurationManager.AppSettings["ImageHeight"]),
                                    Title = name,
                                    MimeType = mimeType,
                                    Name = name,
                                    Extension = extension,
                                    Size = stream.Length,
                                    Constrained = false
                                };

                                _createImage.Handle(createImageCommand);

                                activityDocumentCommand = new CreateActivityDocument(User)
                                {
                                    ActivityValuesId = activityValues.RevisionId,
                                    ImageId = createImageCommand.CreatedImage.Id,
                                    Mode = activityMode.AsEnum<ActivityMode>(),
                                    Title = name
                                };
                            }
                            else
                            {
                                CreateLoadableFile createLoadableFileCommand = new CreateLoadableFile
                                {
                                    SourceStream = stream,
                                    Name = name,
                                    Extension = extension,
                                    MimeType = mimeType,
                                    Title = name
                                };

                                _createLoadableFile.Handle(createLoadableFileCommand);

                                activityDocumentCommand = new CreateActivityDocument(User)
                                {
                                    ActivityValuesId = activityValues.RevisionId,
                                    FileId = createLoadableFileCommand.CreatedLoadableFile.Id,
                                    Mode = activityMode.AsEnum<ActivityMode>(),
                                    Title = name
                                };
                            }

                            _createActivityDocument.Handle(activityDocumentCommand);
                        }
                        catch (Exception ex)
                        {
                            Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message, "text/plain");
                        }

                    }

                    return Request.CreateResponse(HttpStatusCode.Created);
                });

            return task;
        }
예제 #3
0
 protected ActivityDocument Seed(CreateActivityDocument command)
 {
     var existingEntity = _entities.Get<ActivityDocument>()
         .SingleOrDefault(x => x.ActivityValues.ActivityId == command.ActivityId && x.FileName == command.FileName);
     if (existingEntity == null)
     {
         using (var fileStream = File.OpenRead(string.Format("{0}{1}", BasePath, command.FileName)))
         {
             command.Content = fileStream.ReadFully();
             _createDocument.Handle(command);
             return command.CreatedActivityDocument;
         }
     }
     return existingEntity;
 }