Пример #1
0
        public static bool DownloadFile(string psURL, string psFile, string psSavePath)
        {
            bool returnValue = false;

            try
            {
                Uri    filename       = new Uri(psURL + psFile);
                string server         = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
                string serverrelative = filename.AbsolutePath;

                Microsoft.SharePoint.Client.ClientContext   clientContext = new Microsoft.SharePoint.Client.ClientContext(server);
                Microsoft.SharePoint.Client.FileInformation f             = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverrelative);

                string fileName = psFile.Substring(psFile.LastIndexOf('/') + 1);

                using (var fileStream = new FileStream(psSavePath + fileName, FileMode.Create))
                {
                    f.Stream.CopyTo(fileStream);
                }

                returnValue = true;
            }
            catch
            {
                returnValue = false;
            }

            return(returnValue);
        }
Пример #2
0
        private void Download()
        {
            byte[] buffer   = new byte[buffersize];
            int    sizeread = 0;

            while (true)
            {
                try
                {
                    this.Log.Add(plmOS.Logging.Log.Levels.DEB, "Starting to download from SharePoint: " + this.URL);

                    using (Microsoft.SharePoint.Client.ClientContext SPContext = new Microsoft.SharePoint.Client.ClientContext(this.SiteURL.AbsoluteUri))
                    {
                        SPContext.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(this.Username, this.Password);

                        // Open Base Folder
                        Microsoft.SharePoint.Client.Folder SPBaseFolder = this.OpenBaseFolder(SPContext);

                        // Open Supplier Folder
                        Microsoft.SharePoint.Client.Folder SPSupplierFolder = this.OpenFolder(SPContext, SPBaseFolder, this.SupplierID);

                        // Open Project Folder
                        Microsoft.SharePoint.Client.Folder SPProjectFolder = this.OpenFolder(SPContext, SPSupplierFolder, this.ProjectID);

                        // Open Root Folder
                        Microsoft.SharePoint.Client.Folder SPRootFolder = this.OpenFolder(SPContext, SPProjectFolder, "Database");

                        // Get Listing of Transaction files on SharePoint
                        SPContext.Load(SPRootFolder.Files);
                        SPContext.ExecuteQuery();

                        // Create list of comitted Transactions
                        List <Int64> committedtransactions = new List <Int64>();

                        foreach (Microsoft.SharePoint.Client.File transactionfile in SPRootFolder.Files)
                        {
                            Int64 transactiondate = -1;

                            if (Path.GetExtension(transactionfile.Name).ToLower() == ".comitted")
                            {
                                if (Int64.TryParse(Path.GetFileNameWithoutExtension(transactionfile.Name), out transactiondate))
                                {
                                    if (!committedtransactions.Contains(transactiondate))
                                    {
                                        committedtransactions.Add(transactiondate);
                                    }
                                }
                            }
                        }

                        // Create List of Transactions that need to be Downloaded
                        List <Microsoft.SharePoint.Client.File> tobedownlaoded = new List <Microsoft.SharePoint.Client.File>();

                        foreach (Microsoft.SharePoint.Client.File transactionfile in SPRootFolder.Files)
                        {
                            Int64 transactiondate = -1;

                            if (Path.GetExtension(transactionfile.Name).ToLower() == ".zip")
                            {
                                if (Int64.TryParse(Path.GetFileNameWithoutExtension(transactionfile.Name), out transactiondate))
                                {
                                    if (committedtransactions.Contains(transactiondate))
                                    {
                                        if (!this.Downloaded.Contains(transactiondate))
                                        {
                                            tobedownlaoded.Add(transactionfile);
                                        }
                                    }
                                }
                            }
                        }

                        if (tobedownlaoded.Count > 0)
                        {
                            this.ReadingTotal  = tobedownlaoded.Count;
                            this.ReadingNumber = 0;
                            this.Reading       = true;

                            foreach (Microsoft.SharePoint.Client.File transactionfile in tobedownlaoded)
                            {
                                Int64 transactiondate = -1;

                                if (Int64.TryParse(Path.GetFileNameWithoutExtension(transactionfile.Name), out transactiondate))
                                {
                                    if (!this.Downloaded.Contains(transactiondate))
                                    {
                                        // Check if Transaction Folder Exists in Local Cache
                                        Boolean downloadneeded = true;

                                        DirectoryInfo localtransactionfolder    = new DirectoryInfo(this.LocalRootFolder.FullName + "\\" + transactiondate.ToString());
                                        DirectoryInfo localtransactiontmpfolder = new DirectoryInfo(this.LocalRootFolder.FullName + "\\" + transactiondate.ToString() + ".download");
                                        FileInfo      committed = new FileInfo(localtransactionfolder.FullName + "\\committed");

                                        if (localtransactionfolder.Exists)
                                        {
                                            if (committed.Exists)
                                            {
                                                downloadneeded = false;
                                            }
                                        }
                                        else
                                        {
                                            localtransactionfolder.Create();
                                        }

                                        if (downloadneeded)
                                        {
                                            // Download Transaction File from SharePoint
                                            FileInfo localtransactionfile = new FileInfo(this.LocalRootFolder.FullName + "\\" + transactionfile.Name);
                                            Microsoft.SharePoint.Client.FileInformation transactionfileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(SPContext, transactionfile.ServerRelativeUrl);

                                            using (FileStream sw = System.IO.File.OpenWrite(localtransactionfile.FullName))
                                            {
                                                using (transactionfileInfo.Stream)
                                                {
                                                    while ((sizeread = transactionfileInfo.Stream.Read(buffer, 0, buffersize)) > 0)
                                                    {
                                                        sw.Write(buffer, 0, sizeread);
                                                    }
                                                }
                                            }

                                            // Extract files from ZIP File
                                            if (!localtransactiontmpfolder.Exists)
                                            {
                                                localtransactiontmpfolder.Create();
                                            }
                                            else
                                            {
                                                foreach (FileInfo file in localtransactiontmpfolder.GetFiles())
                                                {
                                                    file.Delete();
                                                }
                                            }

                                            ZipFile.ExtractToDirectory(localtransactionfile.FullName, localtransactiontmpfolder.FullName);

                                            // Move XML Files to Transaction Directory
                                            foreach (FileInfo tmpxmlfile in localtransactiontmpfolder.GetFiles("*.xml"))
                                            {
                                                FileInfo xmlfile = new FileInfo(localtransactionfolder.FullName + "\\" + tmpxmlfile.Name);

                                                if (xmlfile.Exists)
                                                {
                                                    xmlfile.Delete();
                                                    xmlfile.Refresh();
                                                }

                                                tmpxmlfile.MoveTo(xmlfile.FullName);
                                            }

                                            // Move Vault Files to Vault
                                            foreach (FileInfo tmpvaultfile in localtransactiontmpfolder.GetFiles("*.dat"))
                                            {
                                                FileInfo vaultfile = new FileInfo(this.LocalVaultFolder.FullName + "\\" + tmpvaultfile.Name);

                                                if (vaultfile.Exists)
                                                {
                                                    vaultfile.Delete();
                                                    vaultfile.Refresh();
                                                }

                                                tmpvaultfile.MoveTo(vaultfile.FullName);
                                            }

                                            // Delete Temp Folder
                                            localtransactiontmpfolder.Delete(true);

                                            // Delete ZIP File
                                            localtransactionfile.Delete();

                                            // Create Committ File
                                            committed.Create();

                                            this.Downloaded.Add(transactiondate);
                                        }
                                        else
                                        {
                                            this.Downloaded.Add(transactiondate);
                                        }
                                    }
                                }

                                this.ReadingNumber++;
                            }

                            this.Reading       = false;
                            this.ReadingTotal  = 0;
                            this.ReadingNumber = 0;
                        }

                        // Set Initialised to true once done one Sync
                        if (!this.Initialised)
                        {
                            // Set to Initialised
                            this.Initialised = true;

                            // Start Upload
                            this.UploadThread = new Thread(this.Upload);
                            this.UploadThread.IsBackground = true;
                            this.UploadThread.Start();
                        }
                    }
                }
                catch (Exception e)
                {
                    this.Log.Add(plmOS.Logging.Log.Levels.ERR, "SharePoint download failed: " + e.Message);
                    this.Log.Add(plmOS.Logging.Log.Levels.DEB, "SharePoint download failed: " + e.Message + Environment.NewLine + e.StackTrace);
                }

                // Delay to next check
                Thread.Sleep(this.SyncDelay * 1000);
            }
        }