public bool AddFile(Credentials c, MachineContents m, DirectoryContents d,
				FileContents f)
        {
            m.Directories = GetDirList(c, m);
            f.Dir = (from o in m.Directories where o.Name == d.Name select o.Id).Single();

            if (!CheckFileExistence(c, m, d, f)) {
                AddFileContent(f);
                //TypeManipulator.TypeToId(f);
                File f1 = File.CreateFile(1, f.Dir, 1, f.Content, f.Name, f.Size, f.Hash,
                    f.Uploaded, f.Modified);
                using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
                    context.Files.AddObject(f1);
                    context.SaveChanges();
                }
            } else {
                GetFileId(c, m, d, f);
                GetFileContentId(c, m, d, f);
                UpdateFileContent(f);
                //TypeManipulator.TypeToId(f);

                using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
                    File f1 = (from o in context.Files where o.file_id == f.Id select o).Single();
                    f1.file_hash = f.Hash;
                    f1.file_modified = f.Modified;
                    f1.file_size = f.Size;
                    f1.file_uploaded = f.Uploaded;

                    context.SaveChanges();
                }
            }
            return true;
        }
 public bool AddDirectory(Credentials c, MachineContents m, DirectoryContents d)
 {
     var cl = new Ref.FileSyncModelClient();
     try {
         bool result = false;
         result = cl.AddDirectory(c, m, d);
         cl.Close();
         return result;
     } catch (Exception ex) {
         cl.Abort();
         throw new ActionException("Unable to create a new directory in the database.",
             ActionType.Directory, MemeType.Fuuuuu, ex);
     }
 }
        public bool AddFile(Credentials c, MachineContents m, DirectoryContents d,
				FileContents f)
        {
            var cl = new Ref.FileSyncModelClient();
            try {
                bool result = false;
                result = cl.AddFile(c, m, d, f);
                cl.Close();
                return result;
            } catch (Exception ex) {
                cl.Abort();
                throw new ActionException("Error occurred while file was uploaded.",
                    ActionType.File, MemeType.Fuuuuu, ex);
            }
        }
 public bool AddDirectory(Credentials c, MachineContents m, DirectoryContents d)
 {
     m.Directories=GetDirList(c, m);
     int NoSuchNameYet = (from o in m.Directories where o.Name == d.Name select o).Count();
     if (NoSuchNameYet != 0) {
         // throw new Exception("directory with given name already exists");
         //no action needed
     } else {
         using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
             d.Owner = LoginToId(context, c.Login);
         }
         AddDir(d);
         using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
             m.Id = MachineNameToId(context, m.Name);
         }
         AddMachDir(m, d);
     }
     return true;
 }
        public void GetFileContent(Credentials c, MachineContents m, DirectoryContents d,
				FileContents f)
        {
            f.Id = GetFileContentId(c, m, d, f);
            using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
                Content c1 = (from o in context.Contents
                              where o.content_id == f.Content
                              select o).Single();
                f.Data = c1.content_data;
            }
        }
Exemple #6
0
 public void GetFileContent(Credentials c, MachineContents m, DirectoryContents d,
                            FileContents f)
 {
     throw new Exception("not implemented");
 }
Exemple #7
0
 public bool AddFile(Credentials c, MachineContents m, DirectoryContents d, FileContents f)
 {
     throw new NotImplementedException();
 }
Exemple #8
0
 public FileContents GetFileWithContent(Credentials c, MachineContents m, DirectoryContents d, FileIdentity f)
 {
     throw new NotImplementedException();
 }
        private bool CheckFileExistence(Credentials c, MachineContents m, DirectoryContents d,
				FileContents f)
        {
            GetDirList(c, m);
            d.Id = (from o in m.Directories where o.Name == d.Name select o.Id).Single();
            try {
                using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
                    (from o in context.Files
                     where (o.file_name == f.Name) && (o.dir_id == d.Id)
                     select o.file_id).Single();
                }
            } catch {
                return false;
            }
            return true;
        }
        public MachineContents GetMachineWithDirs(Credentials c, MachineIdentity m)
        {
            MachineContents mc = new MachineContents(m);

            using (filesyncEntitiesNew context = new filesyncEntitiesNew())
            {
                int mach_id = MachineNameToId(context, m.Name);
                if (mach_id == -1)
                    return null;
                mc.Id = mach_id;
                List<DirectoryContents> dirlist = new List<DirectoryContents>();

                foreach (var x in (from md in context.MachineDirs
                                   join d in context.Dirs on md.dir_id equals d.dir_id
                                   where md.machine_id == mach_id
                                   select new { md.dir_realpath, d }))
                {
                    var dir = new DirectoryContents(x.d.dir_name, x.d.dir_description, x.dir_realpath);
                    dir.Id = x.d.dir_id;
                    dir.Owner = x.d.user_ownerid;
                    dirlist.Add(dir);
                }
                mc.Directories = dirlist;
                return mc;
            }
        }
Exemple #11
0
 public DirectoryContents(DirectoryContents dc)
     : this(dc.Name, dc.LocalPath, dc.Description, dc.Files)
 {
     //nothing needed here
 }
        public FileContents GetFileWithContent(Credentials c, MachineContents m,
				DirectoryContents d, FileIdentity fid)
        {
            var cl = new Ref.FileSyncModelClient();
            try {
                FileContents f = null;
                f = cl.GetFileWithContent(c, m, d, f);
                cl.Close();
                if (f == null)
                    throw new ActionException("Received a null object.", ActionType.User);
                return f;
            } catch (Exception ex) {
                cl.Abort();
                throw new ActionException("Error while downloading file contents from database.",
                    ActionType.File, MemeType.Fuuuuu, ex);
            }
        }
 public System.Collections.Generic.List<FileContents> GetFileList(Credentials c, MachineContents m, DirectoryContents d)
 {
     throw new NotImplementedException();
 }
        public DirectoryContents GetDirectoryWithFiles(Credentials c, MachineContents m,
				DirectoryIdentity did)
        {
            var cl = new Ref.FileSyncModelClient();
            try {

                DirectoryContents newD = new DirectoryContents(did);
                newD.Files = cl.GetFileList(c, m, newD);
                cl.Close();
                if (newD == null)
                    throw new ActionException("Received a null object.", ActionType.User);
                return newD;

                //DirectoryContents d = null;
                //d = cl.GetDirectoryWithFiles(c, m, did);
                //cl.Close();
                //return d;

            } catch (ActionException ex) {
                cl.Abort();
                throw new ActionException("Unable to download directory contents.",
                    ActionType.Directory, MemeType.Fuuuuu, ex);
            } catch (Exception ex) {
                cl.Abort();
                throw new ActionException("Error while downloading directory contents.",
                    ActionType.Directory, MemeType.Fuuuuu, ex);
            }
        }
 private void buttonAddDir_Click(object sender, RoutedEventArgs e)
 {
     var pickedFolder = new FolderPickerDialog();
     try {
         if (pickedFolder.ShowDialog() == true) {
             string path = pickedFolder.SelectedPath;
             if (path == null)
                 throw new ActionException("No folder was chosen.", ActionType.Directory);
             //DirIdentity did = new DirIdentity(path.Substring(path.LastIndexOf('\\') + 1),
             //	null, path);
             DirectoryContents dcNew = new DirectoryContents(
                 path.Substring(path.LastIndexOf('\\') + 1), path);
             //path.Substring(path.LastIndexOf('\\') + 1), path, null, null, true);
             dcNew = local.ReadDirectoryContents(dcNew);
             foreach (DirectoryContents dc in Directories)
                 if (dc.Name.Equals(dcNew.Name) || dc.LocalPath.Equals(dcNew.LocalPath))
                     throw new ActionException("Folder already exists on this machine.",
                         ActionType.Directory);
             Directories.Add(dcNew);
             machine.Directories.Add(dcNew);
         }
     } catch (ActionException ex) {
         MessageBox.Show(ex.Message, ex.Title);
     }
 }
        public List<FileContents> GetFileList(Credentials c, MachineContents m, DirectoryContents d)
        {
            List<FileContents> filelist = new List<FileContents>();
            m.Directories=GetDirList(c, m);
            d.Id = (from o in m.Directories where o.Name == d.Name select o.Id).Single();

            using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
                foreach (var x in (from f in context.Files
                                   join t in context.Types on f.type_id equals t.type_id
                                   where f.dir_id == d.Id
                                   select new { f, t.type_name })) {
                    FileIdentity file = new FileIdentity(x.f.file_name, x.f.file_modified,
                        x.f.file_uploaded, FileType.PlainText, x.f.file_size, x.f.file_hash);
                    file.Content = x.f.content_id;
                    file.Id = x.f.file_id;
                    filelist.Add(new FileContents(file));
                }
            }
            //d.Files = filelist;
            return filelist;
        }
 public FileContents GetFileWithContent(Credentials c, MachineContents m, DirectoryContents d,
         FileIdentity f)
 {
     var fc = new FileContents(f);
     GetFileContentId(c, m, d, fc);
     using (filesyncEntitiesNew context = new filesyncEntitiesNew())
     {
         Content c1 = (from o in context.Contents
                       where o.content_id == f.Content
                       select o).Single();
         fc.Data = c1.content_data;
     }
     return fc;
 }
 public bool SaveDirectory(DirectoryContents d)
 {
     string localPath = d.LocalPath;
     throw new NotImplementedException();
 }
        private static void AddDir(DirectoryContents d)
        {
            int AddedDirId;
            Dir d1 = Dir.CreateDir(1, d.Name, d.Owner);
            d1.dir_description = d.Description;

            using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
                context.Dirs.AddObject(d1);
                context.SaveChanges();
                AddedDirId = (from z in context.Dirs select z).ToList().Last().dir_id;
            }

            d.Id = AddedDirId;
        }
        public bool UploadDirectory(IFileSyncModel connection, Credentials c, MachineIdentity m,
				DirectoryContents d)
        {
            //add dir if it does not exist
            if (!connection.AddDirectory(c, new MachineContents(m), d))
                return false;

            foreach (FileContents f in d.Files) {
                try {
                    FileContents fUp = null;
                    if (f.Size == 0)
                        fUp = ReadFileContents(f, d);
                    else
                        fUp = f;

                    if (!UploadFile(connection, c, m, d, f))
                        return false;
                } catch (ActionException ex) {
                    throw new ActionException("Couldn't upload the directory contents.",
                        ActionType.Directory, ex);
                } catch (Exception ex) {
                    throw new ActionException("Error while uploading a directory.",
                        ActionType.Directory, ex);
                }
            }
            return true;
        }
 private static void AddMachDir(MachineContents m, DirectoryContents d)
 {
     MachineDir md1 = MachineDir.CreateMachineDir(m.Id, d.Id, d.LocalPath);
     using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {
         context.MachineDirs.AddObject(md1);
         context.SaveChanges();
     }
 }
        /// <summary>
        /// Removes the duplicate files inside this directory. It performes basing on 
        /// the rule, which states that file names must not repeat.
        /// </summary>
        private void RemoveDuplicateFiles(DirectoryContents d)
        {
            // Possible second rule example (not introduced):
            // If hash of file.txt equals to hash of file.bmp, both are preserved.
            // If, on the other hand, hash of file.txt equals to hash of file2.txt the later file
            // is preserved.

            if (d.Files == null || d.Files.Count <= 1)
                return;

            List<FileContents> sortedFiles = new List<FileContents>(d.Files);
            sortedFiles.Sort(FileComparison);

            for (int i = 0; i < sortedFiles.Count - 1; i++) {
                if (sortedFiles[i].Name.Equals(sortedFiles[i + 1].Name)) {
                    sortedFiles.RemoveAt(i);
                    i--;
                }
            }

            d.Files = sortedFiles;
        }
        private void GetFileId(Credentials c, MachineContents m, DirectoryContents d,
				FileContents f)
        {
            GetDirList(c, m);
            d.Id = (from o in m.Directories where o.Name == d.Name select o.Id).Single();
            using (filesyncEntitiesNew context = new filesyncEntitiesNew()) {

                int file_id = (from o in context.Files
                               where (o.file_name == f.Name) && (o.dir_id == d.Id)
                               select o.file_id).Single();
                f.Id = file_id;
            }
        }
 public bool EraseDirectory(DirectoryContents d)
 {
     throw new NotImplementedException();
 }
Exemple #25
0
 public System.Collections.Generic.List <FileContents> GetFileList(Credentials c, MachineContents m, DirectoryContents d)
 {
     throw new NotImplementedException();
 }
        public DirectoryContents ReadDirectoryContents(DirectoryIdentity did,
				bool addFilesContents = false)
        {
            if (did.LocalPath == null || did.LocalPath.Equals(EmptyLocalPath))
                return null;

            string[] filePaths = Directory.GetFiles(did.LocalPath);

            DirectoryContents d = new DirectoryContents(did, new List<FileContents>());

            if (filePaths == null || filePaths.Length == 0)
                return null;

            foreach (string path in filePaths) {
                FileContents fc = ReadFileContents(path);
                d.Files.Add(fc);
            }
            return d;
        }
 public DirectoryContents(DirectoryContents dc)
     : this(dc.Name, dc.LocalPath, dc.Description, dc.Files)
 {
     //nothing needed here
 }
Exemple #28
0
 public void AddDirectory(Credentials c, MachineContents m, DirectoryContents d)
 {
     throw new Exception("not implemented");
 }