/// <summary> /// Read the config file and populate this class /// </summary> /// <param name="file"> /// A <see cref="System.String"/> /// </param> /// <returns> /// A <see cref="System.Boolean"/> /// </returns> public static bool Init(string file) { if (!Tools.DirectoryExists(TmpDir)) { Log.Warning("Tmpdir doesn't exist"); return false; } FileInfo fi = new FileInfo(file); if (!fi.Exists) { Console.Error.Write("{0} doesn't exist!\n", file); return false; } File = file; MediaTypes = new List<MediaType>(); Previews = new ArrayList(); DatabaseInfo = new DBInfo(); BasePaths = new List<BasePath>(); Directories = new List<MediaDB.Backend.Directory>(); FileIndex = new List<string>(); XmlDocument xdoc = new XmlDocument(); xdoc.Load(File); if (xdoc.FirstChild.Name != "mediadb") { Log.Warning("Malformed config file. Root node name isn't \"mediadb\"!"); return false; } ArrayList basePaths = new ArrayList(); foreach (XmlNode child in xdoc.FirstChild.ChildNodes) { if (child.NodeType != XmlNodeType.Element) continue; switch (child.Name) { case "threads": Threads = Convert.ToInt32(child.FirstChild.Value); break; case "database": foreach (XmlNode c in child.ChildNodes) { if (c.NodeType != XmlNodeType.Element) continue; string v = c.FirstChild.Value; switch (c.Name) { case "host": DatabaseInfo.Host = v; break; case "database": DatabaseInfo.Name = v; break; case "username": DatabaseInfo.Username = v; break; case "password": DatabaseInfo.Password = v; break; } } break; case "mediatypes": foreach (XmlNode c in child.ChildNodes) { if (c.Name == "mediatype") { MediaType mt = new MediaType(); mt.Extension = c.Attributes["extension"].Value.ToString(); mt.Mimetype = c.Attributes["mimetype"].Value.ToString(); MediaTypes.Add(mt); } } break; case "maxbytes": MaxBytes = (1024*1024)*long.Parse(child.FirstChild.Value); break; case "previews": previewQuality = child.Attributes["quality"].Value.ToString(); foreach (XmlNode c in child.ChildNodes) { if (c.Name == "preview") { Preview pv = new Preview(); pv.Name = c.Attributes["name"].Value.ToString(); pv.Width = Convert.ToInt32(c.Attributes["width"].Value); pv.Height = Convert.ToInt32(c.Attributes["height"].Value); if (PreviewMinWidth == 0 || pv.Width < PreviewMinWidth) PreviewMinWidth = pv.Width; if (PreviewMinHeight == 0 || pv.Height < PreviewMinHeight) PreviewMinHeight = pv.Height; Previews.Add(pv); } } break; case "paths": foreach (XmlNode c in child.ChildNodes) { if (c.Name == "path") basePaths.Add(c.FirstChild.Value); } break; } } Previews.Sort(); Previews.Reverse(); dbstr = String.Format("server={0};database={1};userid={2};password={3};", DatabaseInfo.Host, DatabaseInfo.Name, DatabaseInfo.Username, DatabaseInfo.Password); Log.Debug(">>> Connecting to database..."); try { dbcon = new MySqlConnection(dbstr); dbcon.Open(); #if DEBUG DB.Query("TRUNCATE `file`"); DB.Query("TRUNCATE `preview`"); DB.Query("TRUNCATE `keyword`"); DB.Query("TRUNCATE `keyword_rel`"); #endif Log.Debug("OK!\n"); } catch (Exception e) { Log.Debug("FAILED! "); Log.Werror("{0}\n", e.Message); return false; } var db = new DbManager(); // Collect base paths from database if (db.QueryReader("SELECT * FROM base_path")) { while (db.DataReader.Read()) { string p = db.DataReader.GetString("path"); long id = db.DataReader.GetInt64("id"); if (!(basePaths.Contains(p) && Tools.DirectoryExists(p))) { Log.Notice("Base path \"{0}\" is removed from config or file " + "system! Removing from database...\n", p); // NOTE! This uses the global DB object. It's not supposed to // use the local one since it would conflict with the DataReader. DB.Query("DELETE FROM base_path WHERE path = @path", DB.Param("path", p)); } else BasePaths.Add(new BasePath(id, p)); } } // Sync paths from config file with path from database foreach (string path in basePaths) { var t = BasePaths.Find(delegate(BasePath tp) { return tp.Name == path; }); if (t == null) { if (!Tools.DirectoryExists(path)) { Log.Warning("Path \"{0}\" in config file doesn't exits in file " + "system!\n"); } else { Log.Debug("+++ Add {0} to base paths\n", path); long myid; string sql = "INSERT INTO base_path(path) VALUES (@path)"; // NOTE! This uses the global DB object. It's not supposed to // use the local one since it would conflict with the DataReader. if (DB.QueryInsert(out myid, sql, DB.Param("path", path))) BasePaths.Add(new BasePath(myid, path)); else Log.Warning("Failed adding {0} to base_path!\n", path); } } } // Setup the directory list if (db.QueryReader("SELECT * FROM directory")) { while (db.DataReader.Read()) { MediaDB.Backend.Directory d = MediaDB.Backend.Directory.FromSql(db.DataReader); if (Tools.DirectoryExists(d.FullName) && InBasePaths(d.FullName)) { Directories.Add(d); } else { DB.Query("DELETE FROM directory WHERE id='" + d.Id + "'"); } } } // Setup the list of files if (db.QueryReader("SELECT fullname FROM `file`")) { while (db.DataReader.Read()) FileIndex.Add(db.DataReader.GetString("fullname")); } filesCount = FileIndex.Count; db.Dispose(); return true; }
/// <summary> /// Get a <see cref="FileHandler"/> object for <paramref name="file"/> of /// mediatype <paramref name="type"/>. /// </summary> /// <param name="file"> /// A <see cref="FileInfo"/> /// </param> /// <param name="type"> /// A <see cref="MediaType"/> /// </param> /// <returns> /// A <see cref="FileHandler"/> /// </returns> public static FileHandler GetFileHandler(FileInfo file, MediaType type) { switch (type.Mimetype) { case "image/bmp": case "image/gif": case "image/png": case "image/jpeg": case "image/tiff": return new IMGHandler(file, type); case "image/x-eps": case "application/illustrator": return new EPSHandler(file, type); case "application/pdf": return new PDFHandler(file, type); case "image/svg+xml": return new SVGHandler(file, type); } return null; }