Exemple #1
0
        public FolderProcess AddOrUpdateByName(string authKey, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            FolderProcess folderProcess;

            using (CdnContext context = new CdnContext())
            {
                Client client = context.clients.Where(x => x.authKey.Trim().Equals(authKey.Trim())).FirstOrDefault();
                if (client == null)
                {
                    throw new NullReferenceException("There is no client with this authentication key. : " + authKey);
                }

                Folder folder = client.folders.FirstOrDefault(x => x.name.ToLower().Trim().Equals(name.ToLower().Trim()) && x.isDeleted == false);
                if (folder != null)
                {
                    folderProcess = FolderProcess.Updated;
                }
                else
                {
                    Directory.CreateDirectory(ParameterUtil.GetCdnPath() + name);
                    context.folders.Add(new Folder {
                        name = name, client = client
                    });
                    context.SaveChanges();
                    folderProcess = FolderProcess.Added;
                }
                return(folderProcess);
            }
        }
Exemple #2
0
        public List <string> GetFilesByFolderName(string authKey, string name)
        {
            List <string> result = new List <string>();

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            using (CdnContext context = new CdnContext())
            {
                Client client = context.clients.Where(x => x.authKey.Trim().Equals(authKey.Trim())).FirstOrDefault();
                if (client == null)
                {
                    throw new NullReferenceException("There is no client with this authentication key. : " + authKey);
                }

                Folder folder = client.folders.FirstOrDefault(x => x.name.ToLower().Trim().Equals(name.ToLower().Trim()) && x.isDeleted == false);
                if (folder == null)
                {
                    throw new NullReferenceException("There is no folder with this folder name. : " + name);
                }

                List <Models.File> files = folder.files.Where(x => x.isDeleted == false).ToList();
                if (files.Count > 0)
                {
                    result.AddRange(files.Select(x => x.name));
                }
            }
            return(result);
        }
        public async Task <bool> EditPartner(PartnerRequestModel partnerModel, string cdnPath, string webRootPath)
        {
            if (partnerModel == null)
            {
                throw new ArgumentNullException(nameof(partnerModel));
            }

            var userEntityToUpdate = await _db.Users.GetAsync(partnerModel.PartnerId);

            if (userEntityToUpdate == null)
            {
                return(false);
            }

            var updatedPartnerDescriptionEntity = new DescriptionEntityModel
            {
                Id          = userEntityToUpdate.DescriptionId,
                Description = partnerModel.Description,
            };
            await _db.Descriptions.UpdateAsync(updatedPartnerDescriptionEntity);

            if (partnerModel.PartnerImage != null)
            {
                var newPartnerImageEntity = new ImageEntityModel();
                var cdnContext            = new CdnContext(cdnPath, webRootPath);
                newPartnerImageEntity.FileName = await cdnContext.Upload(partnerModel.PartnerImage);

                newPartnerImageEntity.InsertDate = DateTime.Now;
                newPartnerImageEntity.Id         = await _db.Images.CreateAsync(newPartnerImageEntity);

                userEntityToUpdate.ImageId = newPartnerImageEntity.Id;
            }

            if (!string.IsNullOrEmpty(partnerModel.Password) || !string.IsNullOrWhiteSpace(partnerModel.Password))
            {
                var hashContext       = new HashContext();
                var newHashedPassword = hashContext.MD5(partnerModel.Password);
                userEntityToUpdate.Password = newHashedPassword;
            }
            userEntityToUpdate.Login       = partnerModel.Login;
            userEntityToUpdate.UserName    = partnerModel.Username;
            userEntityToUpdate.Email       = partnerModel.Email;
            userEntityToUpdate.PhoneNumber = partnerModel.PhoneNumber;
            await _db.Users.UpdateAsync(userEntityToUpdate);

            return(true);
        }
Exemple #4
0
        public void DeleteFileByName(string authKey, string folderName, string name)
        {
            try
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException(nameof(name));
                }

                if (string.IsNullOrEmpty(folderName))
                {
                    throw new ArgumentNullException(nameof(folderName));
                }

                using (CdnContext context = new CdnContext())
                {
                    Client client = context.clients.Where(x => x.authKey.Trim().Equals(authKey.Trim())).FirstOrDefault();
                    if (client == null)
                    {
                        throw new NullReferenceException("There is no client with this authentication key. : " + authKey);
                    }

                    Folder folder = client.folders.FirstOrDefault(x => x.name.ToLower().Trim().Equals(folderName.ToLower().Trim()) && x.isDeleted == false);
                    if (folder == null)
                    {
                        throw new Exception("There is no folder with this name and client : " + folderName);
                    }

                    Models.File file = folder.files.FirstOrDefault(x => x.name.ToLower().Trim().Equals(name.ToLower().Trim()) && x.isDeleted == false);
                    if (file == null)
                    {
                        throw new Exception("There is no file to delete under " + folderName + " with this file name. : " + name);
                    }
                    else
                    {
                        System.IO.File.Delete(ParameterUtil.GetCdnPath() + folderName + "\\" + name);
                        context.files.Remove(file);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                ActivityService.LogException(ex);
                throw ex;
            }
        }
Exemple #5
0
        public async Task EditGood(GoodRequestModel model, string cdnPath, string webRootPath)
        {
            if (model == null || string.IsNullOrEmpty(cdnPath) || string.IsNullOrEmpty(webRootPath))
            {
                throw new ArgumentNullException();
            }
            if (model.GoodId == null)
            {
                throw new ArgumentException();
            }
            var goodToUpdate = await _db.Goods.GetAsync(model.GoodId.Value);

            if (goodToUpdate == null)
            {
                return;
            }

            var descriptionModel = new DescriptionEntityModel()
            {
                Id          = goodToUpdate.DescriptionId,
                Description = model.GoodDescription
            };
            await _db.Descriptions.UpdateAsync(descriptionModel);

            goodToUpdate.CategoryId    = model.CategoryId;
            goodToUpdate.DescriptionId = descriptionModel.Id;
            goodToUpdate.InsertDate    = DateTime.Now;
            goodToUpdate.Name          = model.GoodName;
            goodToUpdate.Removed       = !model.Active;
            goodToUpdate.UserId        = model.PartnerId;
            goodToUpdate.Id            = model.GoodId.Value;

            if (model.GoodImage != null)
            {
                var newGoodImage = new ImageEntityModel();
                var cdnContext   = new CdnContext(cdnPath, webRootPath);
                newGoodImage.FileName = await cdnContext.Upload(model.GoodImage);

                newGoodImage.InsertDate = DateTime.Now;
                newGoodImage.Id         = await _db.Images.CreateAsync(newGoodImage);

                goodToUpdate.ImageId = newGoodImage.Id;
            }
            await _db.Goods.UpdateAsync(goodToUpdate);
        }
Exemple #6
0
        public List <string> GetFolders(string authKey)
        {
            List <string> result = new List <string>();

            using (CdnContext context = new CdnContext())
            {
                Client client = context.clients.Where(x => x.authKey.Trim().Equals(authKey.Trim())).FirstOrDefault();
                if (client == null)
                {
                    throw new NullReferenceException("There is no client with this authentication key. : " + authKey);
                }

                List <Folder> folders = client.folders.Where(x => x.isDeleted == false).ToList();
                if (folders.Count > 0)
                {
                    result.AddRange(folders.Select(x => x.name));
                }
            }
            return(result);
        }
        public async Task CreateNewPartner(PartnerRequestModel partnerRequestModel, string cdnPath, string webRootPath)
        {
            if (partnerRequestModel == null)
            {
                throw new ArgumentNullException(nameof(partnerRequestModel));
            }
            var newPartnerDescriptionEntity = new DescriptionEntityModel
            {
                Description = partnerRequestModel.Description,
            };

            newPartnerDescriptionEntity.Id = await _db.Descriptions.CreateAsync(newPartnerDescriptionEntity);

            var newPartnerImageEntity = new ImageEntityModel();
            var cdnContext            = new CdnContext(cdnPath, webRootPath);

            newPartnerImageEntity.FileName = await cdnContext.Upload(partnerRequestModel.PartnerImage);

            newPartnerImageEntity.InsertDate = DateTime.Now;
            newPartnerImageEntity.Id         = await _db.Images.CreateAsync(newPartnerImageEntity);

            var hashContext    = new HashContext();
            var hashedPassword = hashContext.MD5(partnerRequestModel.Password);

            var newPartner = new UserEntityModel()
            {
                DescriptionId = newPartnerDescriptionEntity.Id,
                ImageId       = newPartnerImageEntity.Id,
                InsertDate    = DateTime.Now,
                Login         = partnerRequestModel.Login,
                Password      = hashedPassword,
                Removed       = false,
                RoleId        = RolesContext.Partner,
                UserName      = partnerRequestModel.Username,
                Email         = partnerRequestModel.Email,
                PhoneNumber   = partnerRequestModel.PhoneNumber
            };

            newPartner.Id = await _db.Users.CreateAsync(newPartner);
        }
Exemple #8
0
        public async Task <GoodResponseModel> CreateNewGood(GoodRequestModel model, string cdnPath, string webRootPath)
        {
            if (model == null || string.IsNullOrEmpty(cdnPath) || string.IsNullOrEmpty(webRootPath))
            {
                throw new ArgumentNullException();
            }

            var descriptionModel = new DescriptionEntityModel()
            {
                Description = model.GoodDescription
            };

            descriptionModel.Id = await _db.Descriptions.CreateAsync(descriptionModel);

            var newGoodImage = new ImageEntityModel();
            var cdnContext   = new CdnContext(cdnPath, webRootPath);

            newGoodImage.FileName = await cdnContext.Upload(model.GoodImage);

            newGoodImage.InsertDate = DateTime.Now;
            newGoodImage.Id         = await _db.Images.CreateAsync(newGoodImage);

            var entityModel = new GoodEntityModel()
            {
                CategoryId    = model.CategoryId,
                DescriptionId = descriptionModel.Id,
                ImageId       = newGoodImage.Id,
                InsertDate    = DateTime.Now,
                Name          = model.GoodName,
                Removed       = model.Active != true,
                UserId        = model.PartnerId
            };

            entityModel.Id = await _db.Goods.CreateAsync(entityModel);

            var responseModel = await _db.GoodsResponse.GetAsync(entityModel.Id);

            return(responseModel);
        }
Exemple #9
0
        public string GetFileByName(string authKey, string folderName, string name)
        {
            try
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException(nameof(name));
                }
                if (string.IsNullOrEmpty(folderName))
                {
                    throw new ArgumentNullException(nameof(folderName));
                }

                string path = "";
                using (CdnContext context = new CdnContext())
                {
                    Client client = context.clients.Where(x => x.authKey.Trim().Equals(authKey.Trim())).FirstOrDefault();
                    if (client != null)
                    {
                        Folder folder = client.folders.FirstOrDefault(x => x.name.ToLower().Trim().Equals(folderName.ToLower().Trim()) && x.isDeleted == false);
                        if (folder != null)
                        {
                            Models.File file = folder.files.FirstOrDefault(x => x.name.ToLower().Trim().Equals(name.ToLower().Trim()) && x.isDeleted == false);
                            if (file != null)
                            {
                                path = file.accessUrl;
                            }
                        }
                    }
                }
                return(path);
            }
            catch (Exception ex)
            {
                ActivityService.LogException(ex);
                throw ex;
            }
        }
Exemple #10
0
        public static void LogException(Exception exception)
        {
            try
            {
                if (exception != null)
                {
                    StackFrame frame      = new StackFrame(1);
                    MethodBase methodBase = frame.GetMethod();

                    string methodName = methodBase.Name;
                    string className  = methodBase.DeclaringType.Name;

                    string    msg     = "";
                    int       exCount = 1;
                    Exception ex      = exception;
                    while (ex != null)
                    {
                        msg += exCount + "\n" + ex.Message + "\n" + ex.StackTrace + "\n\n";
                        ex   = ex.InnerException;
                        exCount++;
                    }

                    using (CdnContext context = new CdnContext())
                    {
                        ExceptionLog log = new ExceptionLog()
                        {
                            function         = methodName,
                            objectClass      = className,
                            exceptionMessage = msg,
                        };
                        context.exceptions.Add(log);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex) { }
        }
Exemple #11
0
        public string AuthenticateClient(string connectionString)
        {
            string authKey = "";

            using (CdnContext context = new CdnContext())
            {
                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new ArgumentNullException(nameof(connectionString));
                }

                Client client = context.clients.Where(x => x.connectionString.Trim().Equals(connectionString.Trim())).FirstOrDefault();
                if (client == null)
                {
                    throw new NullReferenceException("Connection string is not match with any client.");
                }
                authKey = Guid.NewGuid().ToString();

                client.name    = "client - " + authKey;
                client.authKey = authKey;
                context.SaveChanges();
            }
            return(authKey);
        }
Exemple #12
0
        public string AddOrUpdateByName(string authKey, string folderName, string name, Stream stream, bool rename)
        {
            try
            {
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException(nameof(name));
                }

                if (rename)
                {
                    name = DateTime.Now.Ticks.ToString() + "-" + name;
                }

                if (string.IsNullOrEmpty(folderName))
                {
                    throw new ArgumentNullException(nameof(folderName));
                }

                string accessUrl = "";

                using (CdnContext context = new CdnContext())
                {
                    Client client = context.clients.Where(x => x.authKey.Trim().Equals(authKey.Trim())).FirstOrDefault();
                    if (client == null)
                    {
                        throw new NullReferenceException("There is no client with this authentication key. : " + authKey);
                    }

                    Folder folder = client.folders.FirstOrDefault(x => x.name.ToLower().Trim().Equals(folderName.ToLower().Trim()) && x.isDeleted == false);
                    if (folder == null)//if folder name is not exists, create a new one with sent folder name.
                    {
                        throw new NullReferenceException("There is no folder with this folder name and client. : " + folderName);
                    }

                    Models.File file = folder.files.FirstOrDefault(x => x.name.ToLower().Trim().Equals(name.ToLower().Trim()) && x.isDeleted == false);

                    if (file == null)
                    {
                        FileStream fs = new FileStream(ParameterUtil.GetCdnPath() + folderName + "\\" + name, FileMode.Create);
                        stream.CopyTo(fs);
                        fs.Close();

                        context.files.Add(new Models.File
                        {
                            name      = name,
                            path      = ParameterUtil.GetCdnPath() + folderName + "\\" + name,
                            accessUrl = accessUrl = ParameterUtil.GetCdnUrl() + folderName + "/" + name,
                            folder_id = folder.id,
                            version   = "1"
                        });
                        context.SaveChanges();
                    }
                }
                return(accessUrl);
            }
            catch (Exception ex)
            {
                ActivityService.LogException(ex);
                throw ex;
            }
        }