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); } }
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; } }
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) { } }
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); }
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; } }