public static DigitalAssetDto ToDto(this DigitalAsset digitalAsset)
 {
     return(new ()
     {
         DigitalAssetId = digitalAsset.DigitalAssetId
     });
 }
        public static TModel FromDigitalAsset <TModel>(DigitalAsset digitalAsset) where
        TModel : DigitalAssetApiModel, new()
        {
            var model = new TModel();

            model.Id = digitalAsset.Id;

            model.Url = digitalAsset.Url;

            model.Bytes = digitalAsset.Bytes;

            model.Folder = digitalAsset.Folder;

            model.Name = digitalAsset.Name;

            model.FileName = digitalAsset.FileName;

            model.Description = digitalAsset.Description;

            model.Created = digitalAsset.Created;

            model.FileModified = digitalAsset.FileModified;

            model.Size = digitalAsset.Size;

            model.ContentType = digitalAsset.ContentType;

            model.UniqueId = digitalAsset.UniqueId;

            model.CreatedOn = digitalAsset.CreatedOn;

            model.CreatedBy = digitalAsset.CreatedBy;

            return(model);
        }
Ejemplo n.º 3
0
            public async Task <UploadDigitalAssetResponse> Handle(UploadDigitalAssetRequest request)
            {
                NameValueCollection formData      = request.Provider.FormData;
                IList <HttpContent> files         = request.Provider.Files;
                List <DigitalAsset> digitalAssets = new List <DigitalAsset>();

                foreach (var file in files)
                {
                    var filename = new FileInfo(file.Headers.ContentDisposition.FileName.Trim(new char[] { '"' })
                                                .Replace("&", "and")).Name;
                    Stream stream = await file.ReadAsStreamAsync();

                    var bytes        = StreamHelper.ReadToEnd(stream);
                    var digitalAsset = new DigitalAsset();
                    digitalAsset.FileName    = filename;
                    digitalAsset.Bytes       = bytes;
                    digitalAsset.ContentType = System.Convert.ToString(file.Headers.ContentType);
                    _context.DigitalAssets.Add(digitalAsset);
                    digitalAssets.Add(digitalAsset);
                }

                await _context.SaveChangesAsync();

                _cache.Add(null, DigitalAssetCacheKeys.DigitalAssets);

                return(new UploadDigitalAssetResponse()
                {
                    DigitalAssets = digitalAssets.Select(x => DigitalAssetApiModel.FromDigitalAsset(x)).ToList()
                });
            }
Ejemplo n.º 4
0
 public static DigitalAssetApiModel FromDigitalAsset(DigitalAsset digitalAsset)
 => new DigitalAssetApiModel
 {
     DigitalAssetId = digitalAsset.DigitalAssetId,
     Name           = digitalAsset.Name,
     Bytes          = digitalAsset.Bytes,
     ContentType    = digitalAsset.ContentType
 };
Ejemplo n.º 5
0
 public static DigitalAssetDto ToDto(this DigitalAsset digitalAsset)
 => new DigitalAssetDto
 {
     DigitalAssetId = digitalAsset.DigitalAssetId,
     Name           = digitalAsset.Name,
     Bytes          = digitalAsset.Bytes,
     ContentType    = digitalAsset.ContentType
 };
        public static DigitalAssetApiModel FromDigitalAsset(DigitalAsset digitalAsset)
        {
            var model = new DigitalAssetApiModel();

            model.DigitalAssetId = digitalAsset.DigitalAssetId;
            model.Name           = digitalAsset.Name;
            return(model);
        }
Ejemplo n.º 7
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var httpContext        = _httpContextAccessor.HttpContext;
                var defaultFormOptions = new FormOptions();
                var digitalAssets      = new List <DigitalAsset>();

                if (!MultipartRequestHelper.IsMultipartContentType(httpContext.Request.ContentType))
                {
                    throw new Exception($"Expected a multipart request, but got {httpContext.Request.ContentType}");
                }

                var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(httpContext.Request.ContentType);

                var boundary = MultipartRequestHelper.GetBoundary(
                    mediaTypeHeaderValue,
                    defaultFormOptions.MultipartBoundaryLengthLimit);

                var reader = new MultipartReader(boundary, httpContext.Request.Body);

                var section = await reader.ReadNextSectionAsync();

                while (section != null)
                {
                    DigitalAsset digitalAsset = default;

                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            using (var targetStream = new MemoryStream())
                            {
                                await section.Body.CopyToAsync(targetStream);

                                var name        = $"{contentDisposition.FileName}".Trim(new char[] { '"' }).Replace("&", "and");
                                var bytes       = StreamHelper.ReadToEnd(targetStream);
                                var contentType = section.ContentType;

                                digitalAsset = new DigitalAsset(name, bytes, contentType);
                            }
                        }
                    }

                    _context.DigitalAssets.Add(digitalAsset);

                    digitalAssets.Add(digitalAsset);

                    section = await reader.ReadNextSectionAsync();
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(new()
                {
                    DigitalAssetIds = digitalAssets.Select(x => x.DigitalAssetId).ToList()
                });
            }
 public static DigitalAssetDto ToDto(this DigitalAsset digitalAsset)
 {
     return(new()
     {
         DigitalAssetId = digitalAsset.DigitalAssetId,
         Bytes = digitalAsset.Bytes,
         ContentType = digitalAsset.ContentType,
         Name = digitalAsset.Name
     });
 }
Ejemplo n.º 9
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var digitalAssets = new HashSet <DigitalAssetApiModel>();

                if (!MultipartRequestHelper.IsMultipartContentType(_httpContext.Request.ContentType))
                {
                    throw new Exception($"Expected a multipart request, but got {_httpContext.Request.ContentType}");
                }

                var boundary = MultipartRequestHelper.GetBoundary(
                    MediaTypeHeaderValue.Parse(_httpContext.Request.ContentType),
                    _defaultFormOptions.MultipartBoundaryLengthLimit);
                var reader = new MultipartReader(boundary, _httpContext.Request.Body);

                var section = await reader.ReadNextSectionAsync();

                while (section != null)
                {
                    ContentDispositionHeaderValue contentDisposition;
                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            using (var targetStream = new MemoryStream())
                            {
                                await section.Body.CopyToAsync(targetStream);

                                var bytes  = StreamHelper.ReadToEnd(targetStream);
                                var tenant = await _context.Tenants.FindAsync(new Guid(_httpContext.Request.GetHeaderValue("Tenant")));

                                var digitalAsset = new DigitalAsset();
                                digitalAsset.Tenant      = tenant;
                                digitalAsset.FileName    = $"{contentDisposition.FileName}".Trim(new char[] { '"' }).Replace("&", "and");
                                digitalAsset.Name        = digitalAsset.FileName;
                                digitalAsset.Bytes       = bytes;
                                digitalAsset.ContentType = section.ContentType;
                                digitalAsset.UploadedOn  = DateTime.UtcNow;
                                _context.DigitalAssets.Add(digitalAsset);
                                await _context.SaveChangesAsync(cancellationToken, request.Username);

                                digitalAssets.Add(DigitalAssetApiModel.FromDigitalAsset(digitalAsset));
                            }
                        }
                    }

                    section = await reader.ReadNextSectionAsync();
                }

                return(new Response()
                {
                });
            }
Ejemplo n.º 10
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var digitalAsset = await _context.DigitalAssets.FindAsync(request.DigitalAsset.DigitalAssetId);

                if (digitalAsset == null)
                {
                    _context.DigitalAssets.Add(digitalAsset = new DigitalAsset(request.DigitalAsset.Name));
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(new() { DigitalAssetId = digitalAsset.DigitalAssetId });
            }
Ejemplo n.º 11
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var digitalAsset = new DigitalAsset();

                _context.DigitalAssets.Add(digitalAsset);

                await _context.SaveChangesAsync(cancellationToken);

                return(new Response()
                {
                    DigitalAsset = digitalAsset.ToDto()
                });
            }
Ejemplo n.º 12
0
        public DigitalAddOrUpdateResponseDto AddOrUpdate(DigitalAssetAddOrUpdateRequestDto request)
        {
            var entity = _repository.GetAll()
                         .FirstOrDefault(x => x.Id == request.Id && x.IsDeleted == false);

            if (entity == null)
            {
                _repository.Add(entity = new DigitalAsset());
            }
            entity.Name = request.Name;
            uow.SaveChanges();
            return(new DigitalAddOrUpdateResponseDto(entity));
        }
Ejemplo n.º 13
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var photographer = await _context.FindAsync <Photographer>(request.PhotographerId);

                var digitalAsset = (await DigitalAsset.Upload(_httpContextAccessor, _context, cancellationToken)).Single();

                photographer.ChangeAvatar(digitalAsset.DigitalAssetId);

                await _context.SaveChangesAsync(cancellationToken);

                return(new Response()
                {
                    DigitalAssetId = digitalAsset.DigitalAssetId
                });
            }
Ejemplo n.º 14
0
            public async Task <Response> Handle(Request request)
            {
                var entity = await _context.DigitalAssets
                             .SingleOrDefaultAsync(x => x.Id == request.DigitalAsset.Id && x.IsDeleted == false);

                if (entity == null)
                {
                    _context.DigitalAssets.Add(entity = new DigitalAsset());
                }
                entity.Name   = request.DigitalAsset.Name;
                entity.Folder = request.DigitalAsset.Folder;
                await _context.SaveChangesAsync();

                return(new Response()
                {
                });
            }
Ejemplo n.º 15
0
            public async Task <UploadDigitalAssetResponse> Handle(Request request)
            {
                var tenant = _context.Tenants.Where(x => x.UniqueId == request.TenantUniqueId).Single();
                var user   = _context.Users
                             .Include(x => x.Tenant)
                             .Where(x => x.Username == request.Username)
                             .Single();

                NameValueCollection formData      = request.Provider.FormData;
                IList <HttpContent> files         = request.Provider.Files;
                List <DigitalAsset> digitalAssets = new List <DigitalAsset>();

                foreach (var file in files)
                {
                    var filename = new FileInfo(file.Headers.ContentDisposition.FileName.Trim(new char[] { '"' })
                                                .Replace("&", "and")).Name;
                    Stream stream = await file.ReadAsStreamAsync();

                    var bytes        = StreamHelper.ReadToEnd(stream);
                    var digitalAsset = new DigitalAsset();
                    digitalAsset.FileName    = filename;
                    digitalAsset.Bytes       = bytes;
                    digitalAsset.IsSecure    = request.IsSecure;
                    digitalAsset.Tenant      = tenant;
                    digitalAsset.ContentType = $"{file.Headers.ContentType}";
                    digitalAsset.UploadedOn  = DateTime.UtcNow;
                    digitalAsset.UploadedBy  = user.Name;
                    digitalAsset.Size        = $"{Math.Round(ByteSize.FromBytes(Convert.ToDouble(digitalAsset.Bytes.Length)).KiloBytes, 1)} KB";
                    _context.DigitalAssets.Add(digitalAsset);
                    digitalAssets.Add(digitalAsset);
                }

                await _context.SaveChangesAsync(request.Username);

                _cache.Remove(DigitalAssetsCacheKeyFactory.Get(request.TenantUniqueId));

                return(new UploadDigitalAssetResponse()
                {
                    DigitalAssets = digitalAssets.Select(x => DigitalAssetApiModel.FromDigitalAsset(x)).ToList()
                });
            }
Ejemplo n.º 16
0
        public static DigitalAssetApiModel FromDigitalAsset(DigitalAsset digitalAsset)
        {
            var model = new DigitalAssetApiModel();

            model.DigitalAssetId = digitalAsset.DigitalAssetId;
            model.Folder         = digitalAsset.Folder;
            model.Name           = digitalAsset.Name;
            model.FileName       = digitalAsset.FileName;
            model.Description    = digitalAsset.Description;
            model.Created        = digitalAsset.Created;
            model.FileModified   = digitalAsset.FileModified;
            model.Size           = digitalAsset.Size;
            model.Bytes          = digitalAsset.Bytes;
            model.ContentType    = digitalAsset.ContentType;
            model.IsSecure       = digitalAsset.IsSecure;
            model.CreatedOn      = digitalAsset.CreatedOn;
            model.CreatedBy      = digitalAsset.CreatedBy;
            model.UploadedOn     = string.Format("{0:yyyy-MM-dd HH:mm}", digitalAsset.UploadedOn);
            model.UploadedBy     = digitalAsset.UploadedBy;
            return(model);
        }
Ejemplo n.º 17
0
    /// <summary>
    /// Event fierd when submit button is triggered.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ProductAdmin AdminAccess = new ProductAdmin();

        DigitalAsset entity = new DigitalAsset();
        entity.DigitalAsset = txtDigitalAsset.Text.Trim();
        entity.ProductID = ItemId;
        entity.OrderLineItemID = null;

        bool Check = AdminAccess.AddDigitalAsset(entity);

        if (Check)
        {
            Response.Redirect("~/admin/secure/catalog/product/view.aspx?mode=digitalAsset&itemid=" + ItemId);
        }
        else
        {
            lblMsg.Text = "Could not add the product category. Please try again.";
            return;
        }
    }
Ejemplo n.º 18
0
        public async Task <HttpResponseMessage> Upload(HttpRequestMessage request)
        {
            var digitalAssets = new List <DigitalAsset>();

            try
            {
                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());

                NameValueCollection formData = provider.FormData;
                IList <HttpContent> files    = provider.Files;

                foreach (var file in files)
                {
                    var filename = new FileInfo(file.Headers.ContentDisposition.FileName.Trim(new char[] { '"' })
                                                .Replace("&", "and")).Name;
                    Stream stream = await file.ReadAsStreamAsync();

                    var bytes        = StreamHelper.ReadToEnd(stream);
                    var digitalAsset = new DigitalAsset();
                    digitalAsset.FileName    = filename;
                    digitalAsset.Bytes       = bytes;
                    digitalAsset.ContentType = System.Convert.ToString(file.Headers.ContentType);
                    _repository.Add(digitalAsset);
                    digitalAssets.Add(digitalAsset);
                }

                _uow.SaveChanges();
            }
            catch (Exception exception)
            {
                var e = exception;
            }

            return(Request.CreateResponse(HttpStatusCode.OK, new DigitalAssetUploadResponseDto(digitalAssets)));
        }
Ejemplo n.º 19
0
            public async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var digitalAsset = await _context.DigitalAssets.FindAsync(request.DigitalAsset.DigitalAssetId);

                if (digitalAsset == null)
                {
                    digitalAsset = new DigitalAsset();
                    await _context.DigitalAssets.AddAsync(digitalAsset);
                }

                digitalAsset.DigitalAssetId = request.DigitalAsset.DigitalAssetId;
                digitalAsset.Name           = request.DigitalAsset.Name;
                digitalAsset.Bytes          = request.DigitalAsset.Bytes;
                digitalAsset.ContentType    = request.DigitalAsset.ContentType;

                await _context.SaveChangesAsync(cancellationToken);

                return(new Response()
                {
                    DigitalAsset = digitalAsset.ToDto()
                });
            }
Ejemplo n.º 20
0
        public async Task <DigitalAssetUploadResponseDto> Upload(NameValueCollection formData, IList <HttpContent> files)
        {
            var digitalAssets = new List <DigitalAsset>();

            foreach (var file in files)
            {
                var filename = new FileInfo(file.Headers.ContentDisposition.FileName.Trim(new char[] { '"' })
                                            .Replace("&", "and")).Name;
                Stream stream = await file.ReadAsStreamAsync();

                var bytes        = StreamHelper.ReadToEnd(stream);
                var digitalAsset = new DigitalAsset();
                digitalAsset.FileName    = filename;
                digitalAsset.Bytes       = bytes;
                digitalAsset.ContentType = System.Convert.ToString(file.Headers.ContentType);
                _repository.Add(digitalAsset);
                digitalAssets.Add(digitalAsset);
            }

            _uow.SaveChanges();

            return(new DigitalAssetUploadResponseDto(digitalAssets));
        }
Ejemplo n.º 21
0
        public HttpResponseMessage Serve([FromUri] Guid uniqueId, int?height = null)
        {
            DigitalAsset        photo  = _cache.FromCacheOrService(() => _repository.GetAll().FirstOrDefault(x => x.UniqueId == uniqueId), uniqueId.ToString());
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

            if (photo == null)
            {
                return(result);
            }
            var memoryStream = new System.IO.MemoryStream(photo.Bytes);

            System.Drawing.Image fullsizeImage = FromStream(memoryStream);
            height = height.HasValue ? height : fullsizeImage.Height;
            var ratio = (float)height.Value / (float)fullsizeImage.Height;
            var width = fullsizeImage.Width * ratio;

            System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage((int)width, height.Value, null, IntPtr.Zero);
            var myResult = new System.IO.MemoryStream();

            newImage.Save(myResult, fullsizeImage.RawFormat);
            result.Content = new ByteArrayContent(myResult.ToArray());
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(photo.ContentType);
            return(result);
        }
Ejemplo n.º 22
0
 public static DigitalAssetDto ToDto(this DigitalAsset digitalAsset)
 => new DigitalAssetDto(digitalAsset.DigitalAssetId, digitalAsset.Name, digitalAsset.Bytes, digitalAsset.ContentType);
Ejemplo n.º 23
0
 public DigitalAssetBuilder()
 {
     _digitalAsset = WithDefaults();
 }
Ejemplo n.º 24
0
 public AddedOrUpdatedDigitalAssetMessage(DigitalAsset digitalAsset, Guid correlationId, Guid tenantId)
 {
     Payload        = new { Entity = digitalAsset, CorrelationId = correlationId };
     TenantUniqueId = tenantId;
 }
Ejemplo n.º 25
0
 public static DigitalAssetDto FromDigitalAsset(DigitalAsset digitalAsset)
 => new()
 public static DigitalAssetApiModel FromDigitalAsset(DigitalAsset digitalAsset)
 => FromDigitalAsset <DigitalAssetApiModel>(digitalAsset);
Ejemplo n.º 27
0
 public static SimplifiedDigitalAssetDto ToSimplifiedDto(this DigitalAsset digitalAsset)
 => new (digitalAsset.DigitalAssetId, digitalAsset.Name, digitalAsset.ContentType);