Exemplo n.º 1
0
        public AffiliateLogo UploadLogo(int id, int logoTypeId)
        {
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var request = HttpContext.Current.Request;

            if (request.Files.Count <= 0)
            {
                throw new Exception("No files found for upload");
            }

            var file      = request.Files[0];
            var affiliate = GetAffiliate(id);

            if (affiliate == null)
            {
                throw new Exception("Invalid Affiliate ID");
            }

            var logo = affiliate.Logos.FirstOrDefault(l => l.AffiliateLogoTypeID == logoTypeId);

            if (logo == null)
            {
                throw new Exception("Could not find logo type to update");
            }

            UpdateAuditData(logo);

            var extension = Path.GetExtension(file.FileName).Substring(1);
            var fileName  = string.Format("{0}-{1}.{2}", affiliate.Name.Slugify(), logo.LogoType.Name.Slugify(), extension);

            // Upload the logo
            var fileInfo = _fileService.UploadFile(new Model.File()
            {
                Stream = file.InputStream,
                Info   = new Model.FileInfo()
                {
                    Name         = fileName,
                    Extension    = Path.GetExtension(file.FileName).Substring(1),
                    SizeBytes    = file.ContentLength,
                    CreateUserID = CurrentUser.UserID,
                    CreateDate   = DateTime.Now
                }
            });

            if (fileInfo == null)
            {
                throw new Exception("Could not upload to file service");
            }

            // Set the file reference on the logo
            logo.FileID   = fileInfo.FileID;
            logo.FileInfo = fileInfo;

            // Save it
            _affiliateService.UpdateAffiliateLogo(logo);

            return(logo);
        }