/* Internal synchronized method to add file to the class map*/
 private void addFileToMapSynchronized(UserFile file)
 {
     lock (this.privateLock) {
         file.initializePrivateLock();                  // this is because the files sent over the network do not have their private locks initialized
         this.filemap[file.filemetadata.filepath] = file;
     }
 }
        /*  Synchronized method to add file to the file system.
         *      If the file is not present then the file is added. If the file
         *      is already present it is overwritten if the new file has a
         *      higher version number
         */
        public bool addFileSynchronized(UserFile file)
        {
//			Logger.Debug("Adding file with file name : " + file.filemetadata.filepath);

            UserFile existingFile = getFileSynchronized(file.filemetadata.filepath);

            bool retval = true;

            if (existingFile != null)
            {
                if (file.filemetadata.versionNumber > existingFile.filemetadata.versionNumber)
                {
                    addFileToMapSynchronized(file);
                }
                else
                {
//					Logger.Debug("Existing file with higher version number found for file : " + file.filemetadata.filepath +
//					             " , skipping updation");
                    retval = false;
                }
            }
            else
            {
                addFileToMapSynchronized(file);
            }

            return(retval);
        }
        /* Internal synchronized method to get file from the user file system
         *      Use this to read from the map
         */
        private UserFile getFileSynchronized(string filename)
        {
            UserFile existingFile = null;

            lock (this.privateLock) {
                if (this.filemap.ContainsKey(filename))
                {
                    existingFile = this.filemap [filename];
                }
            }
            return(existingFile);
        }
Beispiel #4
0
        private void BtnUpload_Click(object sender, EventArgs e)
        {
            var BrowseWin = new System.Windows.Forms.OpenFileDialog();

            BrowseWin.Filter = "All Files (*.*)|*.*";
            var ret = BrowseWin.ShowDialog();

            if (ret == System.Windows.Forms.DialogResult.OK)
            {
                long   FileVer  = -1;
                string FileName = DirectoryTextBox.Text + BrowseWin.SafeFileName;
                foreach (FileMetaData file in FileList.fileMDList)
                {
                    if (file.filepath == FileName)
                    {
                        FileVer = 1 + file.versionNumber;
                        break;
                    }
                }
                if (FileVer == -1)
                {// false
                    FileVer = 1;
                }


                UpdateFile arg           = new UpdateFile();
                string     LocalFilePath = BrowseWin.FileName;
                UserFile   FileToUpload  = new UserFile(FileName, ConnectedUser.ClientId);

                //byte[] fileStream = File.ReadAllBytes(LocalFilePath);`
                string fileStream = File.ReadAllText(LocalFilePath);
                //bool done = FileToUpload.SetFileContent(fileStream, (long)0);
                FileToUpload.SetFileContent(Encoding.UTF8.GetBytes(fileStream), FileVer);
                arg.file = FileToUpload;
                string requestUrl = string.Format("/updatefile/{0}/{1}", ConnectedUser.ClientId, ConnectedUser.Password);
                ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
                JsonServiceClient Updateclient = new JsonServiceClient(CLOUD_SERVICE_ENDPOINT);
                //Updateclient.ContentType = "application/json";
                try
                {
                    Updateclient.Post <Object>(requestUrl, arg);
                    ClientFileSystem.addFileSynchronized(FileToUpload);             // update clientfilesystem on upload
                }
                catch (Exception ex)
                {
                    MessageBox.Show("This File has been changed by another client. Please download the latest copy and try to uplaod");
                }

                //send file for Upload

                this.UpdateFileList();
            }
        }
Beispiel #5
0
        private void DownLoadFileShared(string FileName)
        {
            Stream FileStream;
            var    SaveWin = new System.Windows.Forms.SaveFileDialog();

            string[] F_name = FileName.Split('_');
            SaveWin.FileName = F_name[F_name.Length - 1];               // to mane the file names same
            var ret = SaveWin.ShowDialog();

            if (ret == DialogResult.OK)
            {
                if ((FileStream = SaveWin.OpenFile()) != null)
                {
                    UserFile FileToSave = new UserFile("DownloadedFile", ConnectedUser.ClientId);
                    // Code to write the stream goes here.
                    if (ClientFileSystem.filemap.ContainsKey(FileName))
                    {
                        FileToSave = ClientFileSystem.filemap[FileName];
                    }
                    else
                    {
                        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
                        JsonServiceClient client = new JsonServiceClient(CLOUD_SERVICE_ENDPOINT);
                        foreach (SharedFile file in FileList.sharedFileList)
                        {
                            if (file.filename == FileName)
                            {
                                string owner      = file.owner;
                                string GetFileUrl = string.Format("/file/{0}/{1}/{2}/{3}", ConnectedUser.ClientId, ConnectedUser.Password, FileName, owner);
                                try
                                {
                                    FileToSave = client.Get <UserFile>(GetFileUrl);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                                ClientFileSystem.addFileSynchronized(FileToSave);
                            }
                            FileStream.Write(Encoding.UTF8.GetString(FileToSave.filecontent));
                            FileStream.Close();
                        }
                    }
                }
            }
        }
        /* 	Synchronized method to add file to the file system.
            If the file is not present then the file is added. If the file
         	is already present it is overwritten if the new file has a
         	higher version number
        */
        public bool addFileSynchronized(UserFile file)
        {
            //			Logger.Debug("Adding file with file name : " + file.filemetadata.filepath);

            UserFile existingFile = getFileSynchronized(file.filemetadata.filepath);

            bool retval = true;

            if (existingFile != null) {
                if( file.filemetadata.versionNumber > existingFile.filemetadata.versionNumber){
                    addFileToMapSynchronized(file);
                }else{
            //					Logger.Debug("Existing file with higher version number found for file : " + file.filemetadata.filepath +
            //					             " , skipping updation");
                    retval = false;
                }
            } else {
                addFileToMapSynchronized(file);
            }

            return retval;
        }
 private void DownLoadFileShared(string FileName)
 {
     Stream FileStream;
     var SaveWin = new System.Windows.Forms.SaveFileDialog();
     string[] F_name = FileName.Split('_');
     SaveWin.FileName = F_name[F_name.Length - 1];               // to mane the file names same
     var ret = SaveWin.ShowDialog();
     if (ret == DialogResult.OK)
     {
         if ((FileStream = SaveWin.OpenFile()) != null)
         {
             UserFile FileToSave = new UserFile("DownloadedFile", ConnectedUser.ClientId);
             // Code to write the stream goes here.
             if (ClientFileSystem.filemap.ContainsKey(FileName))
             {
                 FileToSave = ClientFileSystem.filemap[FileName];
             }
             else
             {
                 ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
                 JsonServiceClient client = new JsonServiceClient(CLOUD_SERVICE_ENDPOINT);
                 foreach (SharedFile file in FileList.sharedFileList)
                 {
                     if (file.filename == FileName)
                     {
                         string owner = file.owner;
                         string GetFileUrl = string.Format("/file/{0}/{1}/{2}/{3}", ConnectedUser.ClientId, ConnectedUser.Password, FileName, owner);
                         try
                         {
                             FileToSave = client.Get<UserFile>(GetFileUrl);
                         }
                         catch (Exception ex)
                         {
                             MessageBox.Show(ex.Message);
                         }
                         ClientFileSystem.addFileSynchronized(FileToSave);
                     }
                     FileStream.Write(Encoding.UTF8.GetString(FileToSave.filecontent));
                     FileStream.Close();
                 }
             }
         }
     }
 }
        private void BtnUpload_Click(object sender, EventArgs e)
        {
            var BrowseWin = new System.Windows.Forms.OpenFileDialog();
            BrowseWin.Filter = "All Files (*.*)|*.*";
            var ret = BrowseWin.ShowDialog();
            if (ret == System.Windows.Forms.DialogResult.OK)
            {
                long FileVer = -1;
                string FileName = DirectoryTextBox.Text + BrowseWin.SafeFileName;
                foreach (FileMetaData file in FileList.fileMDList)
                {
                    if (file.filepath == FileName)
                    {
                        FileVer = 1 + file.versionNumber;
                        break;
                    }
                }
                if(FileVer == -1)
                {// false
                    FileVer = 1;
                }

                UpdateFile arg = new UpdateFile();
                string LocalFilePath = BrowseWin.FileName;
                UserFile FileToUpload = new UserFile(FileName, ConnectedUser.ClientId);

                //byte[] fileStream = File.ReadAllBytes(LocalFilePath);`
                string fileStream = File.ReadAllText(LocalFilePath);
                //bool done = FileToUpload.SetFileContent(fileStream, (long)0);
                FileToUpload.SetFileContent(Encoding.UTF8.GetBytes(fileStream), FileVer);
                arg.file = FileToUpload;
                string requestUrl = string.Format("/updatefile/{0}/{1}", ConnectedUser.ClientId, ConnectedUser.Password);
                ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
                JsonServiceClient Updateclient = new JsonServiceClient(CLOUD_SERVICE_ENDPOINT);
                //Updateclient.ContentType = "application/json";
                try
                {
                    Updateclient.Post<Object>(requestUrl, arg);
                    ClientFileSystem.addFileSynchronized(FileToUpload);             // update clientfilesystem on upload
                }
                catch(Exception ex)
                {
                        MessageBox.Show("This File has been changed by another client. Please download the latest copy and try to uplaod");
                }

                //send file for Upload

                this.UpdateFileList();
            }
        }
Beispiel #9
0
 public bool SetFileContentSynchronized(UserFile newfile)
 {
     return SetFileContentSynchronized( newfile.filecontent, newfile.filemetadata.versionNumber);
 }
 /* Internal synchronized method to add file to the class map*/
 private void addFileToMapSynchronized(UserFile file)
 {
     lock (this.privateLock) {
         file.initializePrivateLock (); // this is because the files sent over the network do not have their private locks initialized
         this.filemap[file.filemetadata.filepath] =  file;
     }
 }
Beispiel #11
0
 public bool SetFileContentSynchronized(UserFile newfile)
 {
     return(SetFileContentSynchronized(newfile.filecontent, newfile.filemetadata.versionNumber));
 }