/// <summary> /// Turns the on is inherited. /// </summary> public void TurnOnIsInherited() { using (IDataReader fileReader = DBDirectory.GetById(CurrentTimeZoneId, this.OwnerDirectoryId)) { if (fileReader.Read()) { int ParentDirectoryId = (int)fileReader["ParentDirectoryId"]; using (IDataReader aclReader = DBAccessControlList.GetAcl(ParentDirectoryId)) { while (aclReader.Read()) { this.Add(AccessControlEntry.InternalCreate((int)aclReader["AceId"], true, (string)SqlHelper.DBNull2Null(aclReader["role"]), (int)SqlHelper.DBNull2Null(aclReader["principalId"], 0), (string)SqlHelper.DBNull2Null(aclReader["action"]), (bool)(((byte)SqlHelper.DBNull2Null(aclReader["allow"])) == 1), false, (Guid)SqlHelper.DBNull2Null(aclReader["ownerKey"], Guid.Empty))); } } } } _isInherited = true; SetChanged(); }
public DBDirectory CreateDirectory(IUser user, DBDirectory dir) { if(dir == null) return dir; Debug.WriteLine("Server: Try to create. "+dir.Name); try{ DataTable dt = _db.Select("SELECT name FROM directories " + "WHERE subid = '"+dir.Directory+"' AND " + "name LIKE '"+dir.Name+"'"); foreach(DataRow dr in dt.Rows) { if(dr.ItemArray[0].ToString() == dir.Name) { Regex re = new Regex(@"\d+$"); Match m = re.Match(dir.Name); if(m.Success){ int temv = 0; int.TryParse(m.Value,out temv); temv++; dir.Name = Regex.Replace(dir.Name,@"\d+$",temv.ToString()); }else{ dir.Name = dir.Name + "1"; } return CreateDirectory(user, dir); } } string temp = "INSERT INTO directories " + "(name,subid) VALUES " + "('"+dir.Name+"','"+dir.Directory+"')"; _db.Insert(temp); dir.Status = DirectoryStatus.Created; return dir; }catch(Exception e){ Debug.WriteLine("Server: "+e.Message);} dir.Status = DirectoryStatus.Undefined; return dir; }
public DBFile SaveFile(IUser user, FileStream fs, string clientFileHash, DBDirectory dir) { //if not allready done create neccessary dirs CreatePath(); DBFile dbf = new DBFile(); //skip the file if its larger than 5 mbyte if(fs.Length > 1024*1024*5){ dbf.Status = FileStatus.FileTooBig; return dbf;} //check wether the file exists or not if(File.Exists(_serverStoragePath+clientFileHash)){ dbf.Status = FileStatus.FileAllreadyExists; return dbf;} //check if directory exists or not try{ DataTable dt = _db.Select("SELECT id FROM directories " + "WHERE id = '"+dir.ID+"'"); if(dt.Rows.Count < 1 && dir.ID != 0){ dbf.Status = FileStatus.DirectoryNotExists; return dbf; } }catch(Exception e){ Debug.WriteLine("Server: "+e.Message); dbf.Status = FileStatus.DatabaseProblem; return dbf; } //try to upload the file and write it locally try{ FileStream lfs = File.Open(_serverStoragePath+clientFileHash, FileMode.CreateNew); BinaryReader br = new BinaryReader(fs); BinaryWriter bw = new BinaryWriter(lfs); while(br.BaseStream.Length != br.BaseStream.Position){ bw.Write(br.ReadBytes(1024*1024*5));} bw.Close(); br.Close(); lfs.Close(); fs.Close(); }catch(Exception e){ Debug.WriteLine("Server: "+e.Message); dbf.Status = FileStatus.FileUploadFailed; return dbf; } //check crc MD5Class md5 = new MD5Class(_serverStoragePath+clientFileHash); string hash = md5.GetHash(); if(!clientFileHash.Equals(hash)) { try{ File.Delete(_serverStoragePath+clientFileHash); }catch{} dbf.Status = FileStatus.FileCRCFailed; return dbf; } //if everthing went just fine write it in the db try{ FileInfo fi = new FileInfo(fs.Name); FileInfo local = new FileInfo(_serverStoragePath+hash); _db.Insert("INSERT INTO files (md5,filename,user,size,directory) " + "VALUES ('"+hash+"','"+fi.Name+"','"+user.ID+"','"+local.Length+"','"+dir.ID+"')"); dbf.Status = FileStatus.FileUploaded; dbf.Name = fi.Name; dbf.User = user.ID; DataTable dt = _db.Select("SELECT id FROM files " + "WHERE md5 = '"+hash+"'"); if(dt != null && dt.Rows.Count == 1 && dt.Rows[0].ItemArray.Length == 1) dbf.ID = int.Parse(dt.Rows[0].ItemArray[0].ToString()); else throw new Exception("File inserted, but hash not found?"); return dbf; }catch(Exception e){ Debug.WriteLine("Server: "+e.Message); dbf.Status = FileStatus.DatabaseProblem; return dbf;} }
public DBDirectory RenameDirectory(IUser user, DBDirectory dir, string newName) { return dir; }
public List<DBItem> GetItems(IUser user, DBDirectory dir) { List<DBItem> items = new List<DBItem>(); foreach(DBDirectory db in GetDirectoryInfos( user, dir )) items.Add(db); foreach(DBFile df in GetFiles( user, dir )) items.Add(df); return items; }
public List<DBFile> GetFiles(IUser user, DBDirectory dir) { List<DBFile> list = new List<DBFile>(); if(dir == null) return list; try{ //just in case the dir is empty (easier for gui getting root) int dirID = 0; if(dir != null) dirID = dir.ID; DataTable dt = _db.Select("SELECT id,filename,user " + "FROM files " + "WHERE directory = '"+dirID+"'"); foreach(DataRow r in dt.Rows) { DBFile file = new DBFile(); file.ID = int.Parse(r.ItemArray[0].ToString()); file.Name = r.ItemArray[1].ToString(); file.User = int.Parse(r.ItemArray[2].ToString()); list.Add(file); } }catch(Exception e){ Debug.WriteLine("Server: "+e.Message); } return list; }
public List<DBDirectory> GetDirectoryInfos(IUser user, DBDirectory dir) { List<DBDirectory> list = new List<DBDirectory>(); try{ //just in case the dir is empty (easier for gui getting root) int dirID = 0; if(dir != null) dirID = dir.ID; DataTable dt = _db.Select("SELECT id,name,subid " + "FROM directories " + "WHERE subid ='"+dirID+"'"); foreach(DataRow r in dt.Rows) { DBDirectory dirs = new DBDirectory(r.ItemArray[1].ToString(), DirectoryType.Normal); dirs.ID = int.Parse(r.ItemArray[0].ToString()); int temp; int.TryParse(r.ItemArray[2].ToString(),out temp); dirs.Directory = temp; list.Add(dirs); } }catch(Exception e){ Debug.WriteLine("Server: "+e.Message); } return list; }
public DBDirectory DeleteDirectory(IUser user, DBDirectory dir) { Debug.WriteLine("Server: Try to delete. "+dir.ID); try{ // not empty cause dirs DataTable dt = _db.Select("SELECT id FROM directories " + "WHERE subid = '"+dir.ID+"'"); if(dt.Rows.Count > 0){ dir.Status = DirectoryStatus.DirectoryNotEmpty; return dir;} // not empty cause files dt = _db.Select("SELECT id FROM files " + "WHERE directory = '"+dir.ID+"'"); if(dt.Rows.Count > 0){ dir.Status = DirectoryStatus.DirectoryNotEmpty; return dir;} dt = _db.Select("SELECT id FROM directories " + "WHERE id = '"+dir.ID+"'"); if(dt.Rows.Count <= 0){ dir.Status = DirectoryStatus.NotInDatabase; return dir; }else if(dt.Rows.Count > 1){ dir.Status = DirectoryStatus.IDNotUnique; return dir; }else if(dt.Rows.Count == 1){ string temp = "DELETE FROM directories " + "WHERE id = '"+dir.ID+"'"; _db.Insert(temp); dir.Status = DirectoryStatus.Deleted; return dir; } }catch(Exception e){ Debug.WriteLine("Server: "+e.Message);} dir.Status = DirectoryStatus.Undefined; return dir; }
//gui starts saving the file -> async public void SaveFile(IUser user, FileStream fs, string clientFileHash, DBDirectory dir) { AsyncCallback call = new AsyncCallback( ServerFileSaved ); SavingFile process = _ro.SaveFile; IAsyncResult ar = process.BeginInvoke(user, fs , clientFileHash, dir, call, null); }
public void GetDBItems(DBDirectory dir) { AsyncCallback call = new AsyncCallback( ServerGotDBItems ); GettingDBItems process = _ro.GetItems; IAsyncResult ar = process.BeginInvoke(_user, dir, call, null); }
//gui starts saving the file -> async public void GetDBFiles(IUser user,DBDirectory dir) { AsyncCallback call = new AsyncCallback( ServerGotDBFiles ); GettingDBFiles process = _ro.GetFiles; IAsyncResult ar = process.BeginInvoke(user, dir, call, null); }
public void DeleteDBDirectory(IUser user, DBDirectory dir) { AsyncCallback call = new AsyncCallback( ServerDeletedDBDirectory ); DeletingDBDirectory process = _ro.DeleteDirectory; IAsyncResult ar = process.BeginInvoke(user, dir, call, null); }
public void CreateDBDirectory(DBDirectory dir) { AsyncCallback call = new AsyncCallback( ServerCreatedDBDirectory ); CreatingDBDirectory process = _ro.CreateDirectory; IAsyncResult ar = process.BeginInvoke(_user, dir, call, null); }
/// <summary> /// Gets the container key. /// </summary> /// <param name="DirectoryId">The directory id.</param> /// <returns></returns> public static string GetContainerKey(int DirectoryId) { return(DBDirectory.GetContainerKey(DirectoryId)); }