public async Task <SuggestionModel> GetAndSetAsReadAsync(int id, string mediaFolderPath)
        {
            var suggestion = await biblioEntities.Suggestions.Include(x => x.User)
                             .SingleOrDefaultAsync(x => x.Id == id);

            if (suggestion != null)
            {
                suggestion.IsReaded = true;
                await biblioEntities.SaveChangesAsync();

                var mediaBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty));
                return(GetSuggestionModel(suggestion, mediaFolderPath));
            }
            return(null);
        }
        public async Task RemoveAsync(int id, string mediaFolderPath)
        {
            var category = await biblioEntities.Categories.FindAsync(id);

            if (category != null)
            {
                biblioEntities.Categories.Remove(category);
                await biblioEntities.SaveChangesAsync();

                if (!string.IsNullOrEmpty(category.Image))
                {
                    var mediaBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty));
                    Tools.OssFile.DeleteFile(category.Image, mediaBasePath);
                }
            }
        }
Beispiel #3
0
        public async Task <ProfileModel> EditProfilAsync(ProfileModel profileModel,
                                                         HttpRequest request, string mediaFolderPath, string prefixPhotoProfileName)
        {
            string newImageName          = null;
            var    mediaAbsoluteBasePath = Path.Combine(env.WebRootPath, mediaFolderPath?.Replace("~/", string.Empty));

            try
            {
                string oldImageName = null;
                var    currentUser  = await biblioEntities.Users.SingleOrDefaultAsync
                                      (
                    x => x.Account.Equals(request.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value, StringComparison.OrdinalIgnoreCase)
                                      );

                if (currentUser == null)
                {
                    throw new KeyNotFoundException("Profile");
                }

                bool deleteCurrentImage = false;
                oldImageName = currentUser.Image;
                if (Tools.OssFile.HasImage(profileModel.ImageUploaded))
                {
                    newImageName       = Tools.OssFile.SaveImage(profileModel.ImageUploaded, 300, 300, 100 * 1024, 200 * 1024, prefixPhotoProfileName, mediaAbsoluteBasePath);;
                    deleteCurrentImage = true;
                }
                else if (!string.IsNullOrEmpty(currentUser.Image) && profileModel.DeleteImage)
                {
                    deleteCurrentImage = true;
                }
                else
                {
                    newImageName = currentUser.Image;
                }

                var  newPassword = GetPassword(currentUser.Password, profileModel.Password);
                User newUser     = new User
                                   (
                    currentUser.Id,
                    currentUser.Account,
                    newPassword,
                    currentUser.FullName,
                    currentUser.Role,
                    newImageName,
                    currentUser.Status
                                   );

                biblioEntities.Entry(currentUser).CurrentValues.SetValues(newUser);
                await biblioEntities.SaveChangesAsync();

                if (deleteCurrentImage)
                {
                    Tools.OssFile.DeleteFile(oldImageName, mediaAbsoluteBasePath);
                }

                profileModel = new ProfileModel(newUser, mediaFolderPath);
                return(profileModel);
            }
            catch (Exception ex)
            {
                if (Tools.OssFile.HasImage(profileModel.ImageUploaded) && !string.IsNullOrEmpty(newImageName))
                {
                    Tools.OssFile.DeleteFile(newImageName, mediaAbsoluteBasePath);
                }
                throw ex;
            }
        }
        public async Task UpdateMetaData(string mediaFolderPath, string prefixDocumentFileName, DocumentModel documentModel)
        {
            try
            {
                var document = await biblioEntities.Documents
                               .SingleOrDefaultAsync(x => x.Id == documentModel.Id);

                if (document != null)
                {
                    string inputFile = Path.Combine(env.WebRootPath, documentModel.FileLink.Replace("~/", string.Empty).Replace("/", @"\"));

                    string mediaBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty));

                    string newFileName = OssFile.GetNewFileName(inputFile, prefixDocumentFileName);

                    string outputFile = Path.Combine(mediaBasePath, newFileName);

                    using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        PdfReader  reader  = new PdfReader(inputFile);
                        PdfStamper stamper = new PdfStamper(reader, fs);

                        Hashtable info = reader.Info;
                        if (info.ContainsKey("ISBN"))
                        {
                            info.Remove("ISBN");
                        }
                        info.Add("ISBN", documentModel.Code);
                        if (info.ContainsKey("Title"))
                        {
                            info.Remove("Title");
                        }
                        info.Add("Title", documentModel.Title);
                        if (info.ContainsKey("Subtitle"))
                        {
                            info.Remove("Subtitle");
                        }
                        info.Add("Subtitle", documentModel.Subtitle);
                        if (info.ContainsKey("Subject"))
                        {
                            info.Remove("Subject");
                        }
                        info.Add("Subject", documentModel.Description);
                        if (info.ContainsKey("Creator"))
                        {
                            info.Remove("Creator");
                        }
                        info.Add("Creator", documentModel.Publisher);
                        if (documentModel.PublishDate.HasValue)
                        {
                            if (info.ContainsKey("PublishDate"))
                            {
                                info.Remove("PublishDate");
                            }
                            info.Add("PublishDate", documentModel.PublishDate.Value.ToString("yyyy-MM-dd"));
                        }
                        if (info.ContainsKey("Author"))
                        {
                            info.Remove("Author");
                        }
                        info.Add("Author", documentModel.Authors);
                        if (info.ContainsKey("Contributors"))
                        {
                            info.Remove("Contributors");
                        }
                        info.Add("Contributors", documentModel.Contributors);
                        if (info.ContainsKey("Language"))
                        {
                            info.Remove("Language");
                        }
                        info.Add("Language", documentModel.Language);
                        if (info.ContainsKey("CategoryId"))
                        {
                            info.Remove("CategoryId");
                        }
                        info.Add("CategoryId", documentModel.CategoryId.ToString());
                        if (info.ContainsKey("CategoryName"))
                        {
                            info.Remove("CategoryName");
                        }
                        info.Add("CategoryName", documentModel.CategoryName);
                        stamper.MoreInfo = info;
                        stamper.Close();
                        reader.Close();
                    }
                    document.File = newFileName;
                    await biblioEntities.SaveChangesAsync();

                    try
                    {
                        File.Delete(inputFile);
                    }
                    catch (Exception ex)
                    {
                        throw new FileLoadException(inputFile, ex);
                    }
                }
            }
            catch (FileLoadException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new MethodAccessException("UpdateMetaData", ex);
            }
        }