public async Task <SubjectForClass> Update(SubjectForClass picture, IFormFile newPic, string folderName)
        {
            bool res = await Delete(picture);

            string path, fileName;

            Save(newPic, folderName, out path, out fileName);
            if (fileName != "" && path != "")
            {
                picture.FileName    = fileName;
                picture.VirtualPath = path;

                _context.Entry(picture).State = EntityState.Modified;

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch
                {
                    return(null);
                }

                return(picture);
            }
            return(null);
        }
        /// <summary>
        // Multi Picture save
        /// </summary>
        /// <param name="images"></param>
        /// <param name="folderName"></param>
        /// <returns></returns>
        public async Task <List <SubjectForClass> > Create(List <IFormFile> images, string folderName)
        {
            List <SubjectForClass> pictures = new List <SubjectForClass>();

            foreach (var image in images)
            {
                SubjectForClass picture = new SubjectForClass();
                string          path, fileName;

                Save(image, folderName, out path, out fileName);
                picture.FileName = fileName;

                if (path != "")
                {
                    picture.VirtualPath = path;
                    _context.SubjectForClass.Add(picture);

                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        return(null);
                    }
                }

                pictures.Add(picture);
            }


            return(pictures);
        }
Exemplo n.º 3
0
        public SubjectForClass AddImage(IFormFile image, string folderName)
        {
            var    subject = new SubjectForClass();
            string path, fileName;

            Save(image, folderName, out path, out fileName);
            subject.FileName    = fileName;
            subject.VirtualPath = path;

            if (path == "" & fileName == "")
            {
                return(null);
            }

            return(subject);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="picture"></param>
        /// <returns></returns>
        public async Task <bool> Delete(SubjectForClass picture)
        {
            string root        = _hostingEnvironment.WebRootPath;
            string virtualPath = picture.VirtualPath;
            string fullPath    = root + virtualPath;

            string path = Path.Combine(root, virtualPath);


            _context.SubjectForClass.Remove(picture);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                return(false);
            }


            // Check if file exists with its full path
            if (File.Exists(fullPath))
            {
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        // If file found, delete it
                        File.Delete(fullPath);
                    }

                    catch (IOException)
                    {
                        Thread.Sleep(1000);
                        if (i == 2)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
        public bool DeleteFromRoot(SubjectForClass picture)
        {
            string root = _hostingEnvironment.WebRootPath;

            // Check if file exists with its full path
            if (File.Exists(root + picture.VirtualPath))
            {
                try
                {
                    // If file found, delete it
                    File.Delete(root + picture.VirtualPath);
                }
                catch (IOException)
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        // Single Picture Save
        /// </summary>
        /// <param name="images"></param>
        /// <param name="folderName"></param>
        /// <returns></returns>
        public async Task <SubjectForClass> Create(IFormFile image, string folderName)
        {
            SubjectForClass picture = new SubjectForClass();
            string          path, fileName;

            Save(image, folderName, out path, out fileName);
            picture.FileName = fileName;

            if (path != "")
            {
                picture.VirtualPath = path;
                _context.SubjectForClass.Add(picture);

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            return(picture);
        }
 Task <Subjects> IPictureManager.Update(SubjectForClass picture, IFormFile newPic, string folderName)
 {
     throw new NotImplementedException();
 }