Exemplo n.º 1
0
        public async Task AddAsync(ImageUploadBindingModel uploadBindingModel)
        {
            var file = await this.ToByteArrayAsync(uploadBindingModel.File);

            //var fileExtension = Path.GetExtension(uploadBindingModel.File.FileName);
            var fileExtension = this.GetExtension(uploadBindingModel.File.FileName);
            var fileName      = this.GenerateName(uploadBindingModel.File.FileName, fileExtension);

            if (FileIsEmpty(file))
            {
                throw new FileNotFoundException();
            }

            if (!SUPPORTED_FORMATS.Contains(fileExtension))
            {
                throw new FormatException("Please select a .jpeg, .png or .bmp file");
            }

            await this.blobStorageService.UploadFromFileAsync(file, fileName, fileExtension);

            var image = new Image()
            {
                Name        = fileName,
                Description = uploadBindingModel.Description,
                UserId      = this.currentUserId,
                IsPublic    = uploadBindingModel.IsPublic
            };

            await tagService.AddToImageAsync(image);

            await this.images.AddAsync(image);

            await this.images.SaveChangesAsync();
        }