Structure to represent a single account in the database
Example #1
0
        /// <summary>
        /// Update or insert a new entry for the file into the database
        /// </summary>
        /// <returns>
        /// Flase if there was a problem during the update
        /// </returns>
        /// <param name='user'></param>
        /// <param name='thisFile'></param>
        public bool UpdateFile(ServerUser user, MyFile thisFile)
        {
            // TODO: use OwnCloud API calls if possible perhaps: http://owncloud.org/dev/apps/database/

            string   path    = "/" + user.id + "/files/" + thisFile.name;
            string   absPath = GetDataDir(user) + thisFile.name; //Server.baseDataDir + path;
            FileInfo f       = new FileInfo(absPath);
            long     mtime   = Common.DateTimeToUnixTimestamp(f.LastWriteTimeUtc);

            DbCommand command_checkExists = dbConnection.CreateCommand();

            command_checkExists.CommandText = "SELECT count(id) FROM oc_fscache WHERE path='" + path + "'";

            int checkFound = Convert.ToInt32(command_checkExists.ExecuteScalar());

            DbCommand command = dbConnection.CreateCommand();

            if (checkFound > 0)
            {
                command.CommandText = "UPDATE oc_fscache SET mtime='" + mtime + "' WHERE path='" + path + "'";
            }
            else
            {
                // if the entry does not exist, insert it instead of updating it

                long ctime = Common.DateTimeToUnixTimestamp(f.CreationTimeUtc);

                DbCommand command_getParent = dbConnection.CreateCommand();
                command_getParent.CommandText = "SELECT id FROM oc_fscache WHERE path_hash='"
                                                + Common.Md5Hash(path.Substring(0, path.LastIndexOf('/'))) + "'";

                int parentId = Convert.ToInt32(command_getParent.ExecuteScalar());

                string mimetype = MIMEAssistant.GetMIMEType(f.Name);
                string mimepart = mimetype.Substring(0, mimetype.LastIndexOf('/'));

                bool writable  = true;  //!f.IsReadOnly;
                bool encrypted = false; // ?
                bool versioned = false; // ?

                command.CommandText = String.Format("INSERT INTO oc_fscache (parent, name, path, path_hash, size, mtime, ctime, mimetype, mimepart,`user`,writable,encrypted,versioned) "
                                                    + "VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}')",
                                                    parentId, f.Name, path, Common.Md5Hash(path), f.Length, mtime, ctime, mimetype, mimepart, user.id, writable, encrypted, versioned);
            }

            return(command.ExecuteNonQuery() == 1);
        }
Example #2
0
        /// <summary>
        /// Get an account from the database via a known ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns>null if not found</returns>
        public ServerUser GetUserByName(String userName)
        {
            ServerUser user = null;

            try {
                DbCommand command = dbConnection.CreateCommand();
                command.CommandText = "SELECT * FROM users WHERE name='" + userName + "';";
                DbReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    user = new ServerUser(reader["id"].ToString(), reader["name"].ToString(), reader["password"].ToString());
                }

                reader.Close();
            }
            catch (Exception e) {
                Console.WriteLine("There was an error fetching the account " + e.Message);
            }

            return(user);
        }
Example #3
0
        /// <summary>
        /// Get an account from the database via a known ID
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>null if not found</returns>
        public ServerUser GetUserByName(String userName)
        {
            ServerUser account = null;

            try {
                DbCommand command = dbConnection.CreateCommand();
                command.CommandText = "select * from oc_users where uid='" + userName + "';";
                DbReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    account = new ServerUser(reader["uid"].ToString(), reader["uid"].ToString(), reader["password"].ToString());
                }

                reader.Close();
            }
            catch (Exception e) {
                Console.WriteLine("There was an error fetching the account " + e.Message);
            }

            return(account);
        }
Example #4
0
 public String GetDataDir(ServerUser user)
 {
     return(baseDataDir + user.id + "/");
 }
Example #5
0
        /// <summary>
        /// Update or insert a new entry for the file into the database
        /// </summary>
        /// <returns>
        /// Flase if there was a problem during the update
        /// </returns>
        /// <param name='user'></param>
        /// <param name='thisFile'></param>
        public bool UpdateFile(ServerUser user, MyFile thisFile)
        {
            // TODO: use OwnCloud API calls if possible perhaps: http://owncloud.org/dev/apps/database/

              string path = "/" + user.id + "/files/" + thisFile.name;
              string absPath = GetDataDir(user) + thisFile.name; //Server.baseDataDir + path;
              FileInfo f = new FileInfo (absPath);
              long mtime = Common.DateTimeToUnixTimestamp(f.LastWriteTimeUtc);

              DbCommand command_checkExists = dbConnection.CreateCommand();
              command_checkExists.CommandText = "SELECT count(id) FROM oc_fscache WHERE path='"+ path +"'";

              int checkFound = Convert.ToInt32(command_checkExists.ExecuteScalar());

              DbCommand command = dbConnection.CreateCommand();

              if (checkFound > 0) {
            command.CommandText = "UPDATE oc_fscache SET mtime='" + mtime + "' WHERE path='"+ path +"'";
              } else {
            // if the entry does not exist, insert it instead of updating it

            long ctime =  Common.DateTimeToUnixTimestamp(f.CreationTimeUtc);

            DbCommand command_getParent = dbConnection.CreateCommand ();
            command_getParent.CommandText = "SELECT id FROM oc_fscache WHERE path_hash='"
              + Common.Md5Hash(path.Substring(0, path.LastIndexOf('/'))) + "'";

            int parentId = Convert.ToInt32 (command_getParent.ExecuteScalar ());

            string mimetype = MIMEAssistant.GetMIMEType(f.Name);
            string mimepart = mimetype.Substring(0, mimetype.LastIndexOf('/'));

            bool writable = true; //!f.IsReadOnly;
            bool encrypted = false; // ?
            bool versioned = false; // ?

            command.CommandText = String.Format("INSERT INTO oc_fscache (parent, name, path, path_hash, size, mtime, ctime, mimetype, mimepart,`user`,writable,encrypted,versioned) "
                                            + "VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}')",
                                            parentId, f.Name, path, Common.Md5Hash(path), f.Length, mtime, ctime, mimetype, mimepart, user.id, writable, encrypted, versioned);
              }

              return (command.ExecuteNonQuery() == 1);
        }
Example #6
0
        /// <summary>
        /// Removes the file entry from the database.
        /// </summary>
        /// <returns></returns>
        /// <param name='user'></param>
        /// <param name='filePath'></param>
        public bool RemoveFile(ServerUser user, String filePath)
        {
            DbCommand command = dbConnection.CreateCommand();
              command.CommandText = "DELETE FROM oc_fscache WHERE path='"+ "/" + user.id + "/files/" +filePath +"'";

              return (command.ExecuteNonQuery() == 1);
        }
Example #7
0
        /// <summary>
        /// Get an account from the database via a known ID
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>null if not found</returns>
        public ServerUser GetUserByName(String userName)
        {
            ServerUser account = null;

              try {
            DbCommand command = dbConnection.CreateCommand();
            command.CommandText = "select * from oc_users where uid='" + userName + "';";
            DbReader reader = command.ExecuteReader();

            while (reader.Read())
              account = new ServerUser(reader["uid"].ToString(), reader["uid"].ToString(), reader["password"].ToString() );

            reader.Close();
              }
              catch (Exception e) {
            Console.WriteLine("There was an error fetching the account " + e.Message);
              }

              return account;
        }
Example #8
0
        /// <summary>
        /// Gets the file list in a manner that is easy to serialize and send.
        /// </summary>
        /// <returns>
        /// The file list.
        /// </returns>
        /// <param name='user'>
        /// This account.
        /// </param>
        public List<List<string>> GetFileListSerializable(ServerUser user)
        {
            List<List<string>> fileList = new List<List<string>>();

              int startPath = ("/" + user.id + "/files/").Length + 1;

              DbCommand command = dbConnection.CreateCommand ();
              command.CommandText = "SELECT substr(path, " + startPath + ") as path, mtime as modtime, if(mimetype='httpd/unix-directory', 'd','f') as type, size, 0 as checksum FROM oc_fscache WHERE user='******' AND substr(path, " + startPath + ") != ''";
              DbReader reader = command.ExecuteReader ();

              while (reader.Read()) {
            List<string> fileInfo = new List<string>();

            fileInfo.Add(reader["path"].ToString ());
            fileInfo.Add(reader["type"].ToString ());
            fileInfo.Add(reader["modtime"].ToString ());
            fileInfo.Add(reader["size"].ToString());
            fileInfo.Add(reader["checksum"].ToString());

            fileList.Add(fileInfo);
              }

              reader.Close ();

              return fileList;
        }
Example #9
0
 public String GetDataDir(ServerUser user)
 {
     return baseDataDir + user.id + "/files/";
 }
Example #10
0
        /// <summary>
        /// Attempt to authenticate the client via credentials. If it matches an account on the server return true.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private bool attachUser(String userName, String password)
        {
            User = server.serverDB.GetUserByName(userName);

              if (User == null) {
            Console.WriteLine("User does not exist: " + userName); // TODO: return false?
            return false;
              }

              if (!server.serverDB.CheckPassword(password, User.password)) {
            Console.WriteLine("Password incorrect for: " + userName);
            return false;
              }

              dataDir = server.serverDB.GetDataDir(User);

              if (!Directory.Exists(dataDir)) {

            try {
              Directory.CreateDirectory(dataDir); // TODO: make recursive
            } catch (Exception) {
              Console.WriteLine("Unable to find or create data directory: " + dataDir);
              return false;
            }
              }

              Console.WriteLine("Attached account " + userName + " to handle " + handle);
              Console.WriteLine("Local server storage in: " + dataDir);

              return true;
        }