protected override void AdjustModel(TPreviewableDomainModel model)
        {
            base.AdjustModel(model);
            var    ravenEntityPreviewStorage = SourceContainer.Resolve <TPreviewRepository>();
            var    destinationModelId        = GetDestinationModelId(model);
            Stream stream;
            var    previewFile = ravenEntityPreviewStorage.GetPreviewFile(destinationModelId, out stream);

            if (previewFile != null && stream != null)
            {
                var location = previewFile.Id.Split('/').FirstOrDefault() ?? "unidentified";
                previewFile = PreviewFile.Create(previewFile.FileName, previewFile.ContentType, previewFile.ContentLength, location);
                using (var ms = new MemoryStream())
                {
                    stream.CopyTo(ms);
                    _ = ms.Seek(0, SeekOrigin.Begin);
                    previewFile.SetContent(ms.ToByteArray());
                    stream.Dispose();
                }
                SetDestinationModelPreview(model, previewFile);
            }

            // Fix situation when referenced file can not be retriewed
            if (previewFile == null)
            {
                SetDestinationModelPreview(model, null);
            }
        }
Example #2
0
        public IHttpActionResult Post([FromUri] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!Request.Content.IsMimeMultipartContent())
            {
                return(StatusCode(HttpStatusCode.UnsupportedMediaType));
            }

            var multipartStreamProvider = Request.Content.ReadAsMultipartAsync().Result;
            var sContent = multipartStreamProvider.Contents.SingleOrDefault();

            if (sContent == null)
            {
                return(StatusCode(HttpStatusCode.NotAcceptable));
            }

            var user = _usersRepository.GetById(id);

            if (user == null)
            {
                return(NotFound());
            }

            var headers     = sContent.Headers;
            var previewFile = PreviewFile.Create(
                filename: headers.ContentDisposition?.FileName,
                contentType: headers.ContentType.MediaType,
                contentLength: headers.ContentLength,
                location: "users");

            using (var contentStream = sContent.ReadAsStreamAsync().Result)
            {
                _usersRepository.SetPreviewFile(entityId: id, previewFile, contentStream);
            }
            _usersRepository.SaveChanges();

            return(Ok());
        }