Esempio n. 1
0
        // 1. DeletePhotoFiles(): Delete photo from file at Azure
        // 2. DeletePhotos(): Delete photo from DB with itemId
        // 3. SaveFile(): Save photo(s) into only Azure file,
        // 4. #3 will return it's saved file name list
        public async Task <List <string> > SavePhotos(IFormCollection photos, int itemId)
        {
            var itemPhotos = await GetItemPhotos(itemId);

            if (itemPhotos.Count > 0)
            {
                var itemPhotoId = itemPhotos.FirstOrDefault(c => c.ItemId == itemId).ItemId;
                await DeletePhotoFiles(itemId);
                await DeletePhotos(itemPhotoId);
            }

            Photo         pt    = new Photo();
            List <string> files = new List <string>();

            pt.ItemId    = itemId;
            pt.IsDefault = true;
            foreach (var photo in photos.Files)
            {
                var fileValidate = fileStorageService.CheckFile(photo);
                if (string.IsNullOrEmpty(fileValidate))
                {
                    var savedFileName = await fileStorageService.SaveFile(photo);

                    files.Add(savedFileName);
                    pt.FileName = savedFileName;
                    await InsertPhoto(pt);
                }
                else
                {
                    files.Add(fileValidate);
                }
            }

            return(files);
        }
        public async Task <IActionResult> SaveAvatar()
        {
            var userId         = Request.Form.ToArray()[0].Value;
            var userAvatarFile = UB.GetUserAvatar(userId);

            if (!string.IsNullOrEmpty(userAvatarFile))
            {
                await fileStorageService.DeleteFile(userAvatarFile);
            }

            try
            {
                var fileValidate = fileStorageService.CheckFile(Request.Form.Files[0]);
                if (string.IsNullOrEmpty(fileValidate))
                {
                    var filePath = await fileStorageService.SaveFile(Request.Form.Files[0]);

                    return(Ok(new { filePath }));
                }
                else
                {
                    return(BadRequest(fileValidate));
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                return(BadRequest("Err:File not found"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }