Example #1
0
        public void load(string pServerPathType, clsServer recServer)
        {
            dbSharePointEntities db = new dbSharePointEntities();

            db.tblPath.Load();


            string[] files;
            if (recServer.topFoldersExclude == null)
            {
                if (pServerPathType == "File")
                {
                    files = System.IO.Directory.GetFiles(recServer.serverDrev, "*", System.IO.SearchOption.AllDirectories);
                }
                else // == "Folder"
                {
                    files = System.IO.Directory.GetDirectories(recServer.serverDrev, "*", System.IO.SearchOption.AllDirectories);
                }
            }
            else
            {
                string[]      tffiles;
                List <string> filesList         = new List <string>();
                var           topFoldersExclude = recServer.topFoldersExclude;

                var topFolders = System.IO.Directory.GetDirectories(recServer.serverDrev, "*", System.IO.SearchOption.TopDirectoryOnly);
                var topFiles   = System.IO.Directory.GetFiles(recServer.serverDrev, "*", System.IO.SearchOption.TopDirectoryOnly);

                foreach (var tf in topFolders)
                {
                    var bfilter = false;
                    foreach (var f in topFoldersExclude)
                    {
                        if (tf.ToUpperInvariant().Contains(f.ToUpperInvariant()))
                        {
                            bfilter = true;
                            break;
                        }
                    }
                    if (bfilter)
                    {
                        continue;
                    }

                    if (pServerPathType == "File")
                    {
                        tffiles = System.IO.Directory.GetFiles(tf, "*", System.IO.SearchOption.AllDirectories);
                        filesList.AddRange(topFiles);
                    }
                    else // == "Folder"
                    {
                        tffiles = System.IO.Directory.GetDirectories(tf, "*", System.IO.SearchOption.AllDirectories);
                        filesList.Add(tf);
                    }
                    filesList.AddRange(tffiles);
                }
                files = filesList.ToArray();
            }

            var files1 = files.ToList();

            files1.Sort();
            int i = 0;

            foreach (var file in files1)
            {
                var bfilter = false;
                foreach (var f in filter)
                {
                    if (file.ToUpperInvariant().Contains(f.ToUpperInvariant()))
                    {
                        bfilter = true;
                        break;
                    }
                }
                if (bfilter)
                {
                    continue;
                }

                Console.WriteLine((++i).ToString());
                var qryfile = db.tblPath.Local.Where(f => f.serverPath == file);
                if (qryfile.Count() == 0)
                {
                    tblPath rec = new tblPath()
                    {
                        serverDrev      = recServer.serverDrev,
                        localDrevLetter = recServer.localDrevLetter,
                        serverPath      = file,
                        spSite          = recServer.spSite,
                        spSiteType      = recServer.spSiteType,
                        build           = true,
                        serverPathType  = pServerPathType
                    };
                    try
                    {
                        db.tblPath.Local.Add(rec);
                        db.SaveChanges();
                        Console.WriteLine(file);
                    }
                    catch (Exception e)
                    {
                        int x = 1;
                        //duplicate
                    }
                }
            }
        }
Example #2
0
        public async Task CreateFoldersAsync(clsServer recServer)
        {
            try
            {
                if (recServer.spSiteType == "SharePoint")
                {
                    var site = await clsContent.graphClient.Sites.GetByPath(recServer.spSite, "advnygaard.sharepoint.com").Request().GetAsync();

                    m_targetSiteId = site.Id;
                }
                else
                {
                    m_targetUser = recServer.spSite;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                clsContent.logger.Error(e.Message);
            }



            //var qryfiles = clsContent.m_db.tblPath.Local.Where(f => f.serverPathType == "Folder").Where(f => f.spSite == recServer.spSite).Where(f => f.build == true).Where(f => f.error == null).OrderBy(f => f.serverPath);
            var qryfiles = clsContent.m_db.tblPath.Local.Where(f => f.serverPathType == "Folder").Where(f => f.spSite == recServer.spSite).OrderBy(f => f.serverPath);

            m_totfolders = qryfiles.Count();
            m_ifolder    = 0;
            foreach (var rec in qryfiles)
            {
                m_ifolder++;
                clsContent.RenewAccessToken().Wait();
                var tt = (int)DateTime.Now.Subtract(clsContent.TokenAcquired).TotalSeconds;

                GetspPath(rec);

                DriveItem item;
                try
                {
                    if (recServer.spSiteType == "SharePoint")
                    {
                        item = await clsContent.graphClient.Sites[m_targetSiteId].Drive.Root.ItemWithPath(rec.spPath).Request().GetAsync();
                    }
                    else // OneDrive
                    {
                        item = await clsContent.graphClient.Users[m_targetUser].Drive.Root.ItemWithPath(rec.spPath).Request().GetAsync();
                    }
                    Console.WriteLine(string.Format("{0}-{1}/{2} Exists: {3} {4}", tt, m_ifolder, m_totfolders, recServer.spSite, rec.spPath));
                    rec.spPathExists = true;
                    rec.build        = false;
                    rec.error        = null;
                }
                catch (ServiceException e)
                {
                    Console.WriteLine(string.Format("{0}-{1}/{2} {3} {4}", tt, m_ifolder, m_totfolders, recServer.spSite, rec.spPath));
                    char[]   sp          = { '/' };
                    string[] arrPath     = rec.spPath.Split(sp);
                    var      arrCount    = arrPath.Count();
                    var      arrPathLast = arrPath[arrCount - 1];

                    DriveItem myfolder1 = new DriveItem()
                    {
                        Name   = arrPathLast,
                        Folder = new Folder(),
                        File   = null
                    };

                    try
                    {
                        if (recServer.spSiteType == "SharePoint")
                        {
                            if (arrCount == 1)
                            {
                                item = await clsContent.graphClient.Sites[m_targetSiteId].Drive.Root.Children.Request().AddAsync(myfolder1);
                            }
                            else
                            {
                                var arrPathFirsts = Path.GetDirectoryName(rec.spPath);
                                item = await clsContent.graphClient.Sites[m_targetSiteId].Drive.Root.ItemWithPath(arrPathFirsts).Children.Request().AddAsync(myfolder1);
                            }
                        }
                        else // OneDrive
                        {
                            if (arrCount == 1)
                            {
                                item = await clsContent.graphClient.Users[m_targetUser].Drive.Root.Children.Request().AddAsync(myfolder1);
                            }
                            else
                            {
                                var arrPathFirsts = Path.GetDirectoryName(rec.spPath);
                                item = await clsContent.graphClient.Users[m_targetUser].Drive.Root.ItemWithPath(arrPathFirsts).Children.Request().AddAsync(myfolder1);
                            }
                        }
                        rec.spPathExists = true;
                        rec.build        = false;
                        rec.error        = null;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        clsContent.logger.Error(rec.spPath);
                        clsContent.logger.Error(ex.Message);
                        rec.spPathExists = false;
                        rec.error        = ex.Message;
                    }
                }
                clsContent.m_db.SaveChanges();
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            clsServer recServer = null;

            if (true)
            {
                clsReadDrive objReadDrive = new clsReadDrive();

                //recServer = m_servers.Where(s => s.spSite == "/hn").First();
                //objReadDrive.load("Folder", recServer);
                //objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/hj").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/cm").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/mr").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/holding").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/regnskab").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/billeder").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/drift").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/notater").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/intranet").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/skabeloner").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);

                recServer = m_servers.Where(s => s.spSite == "/klient").First();
                objReadDrive.load("Folder", recServer);
                objReadDrive.load("File", recServer);
            }

            if (true)
            {
                clsContent.init();
                clsProd03 objProd03 = new clsProd03();

                //recServer = m_servers.Where(s => s.spSite == "/hn").First();
                //objProd03.CreateFoldersAsync(recServer).Wait();
                //objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/hj").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/cm").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/mr").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/holding").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/regnskab").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/billeder").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/drift").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/notater").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/intranet").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/skabeloner").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();

                recServer = m_servers.Where(s => s.spSite == "/klient").First();
                objProd03.CreateFoldersAsync(recServer).Wait();
                objProd03.CreateFilesAsync(recServer).Wait();
            }
        }
Example #4
0
        public async Task CreateFilesAsync(clsServer recServer)
        {
            try
            {
                if (recServer.spSiteType == "SharePoint")
                {
                    var site = await clsContent.graphClient.Sites.GetByPath(recServer.spSite, "advnygaard.sharepoint.com").Request().GetAsync();

                    m_targetSiteId = site.Id;
                }
                else
                {
                    m_targetUser = recServer.spSite;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                clsContent.logger.Error(e.Message);
            }


            //var qryfiles = clsContent.m_db.tblPath.Local.Where(f => f.serverPathType == "File").Where(f => f.spSite == recServer.spSite).Where(f => f.build == true).Where(f => f.error == null).OrderBy(f => f.serverPath);
            var qryfiles = clsContent.m_db.tblPath.Local.Where(f => f.serverPathType == "File").Where(f => f.spSite == recServer.spSite).Where(f => f.spPathExists != true).OrderBy(f => f.serverPath);

            m_totfiles = qryfiles.Count();
            m_ifile    = 0;
            foreach (var rec in qryfiles)
            {
                m_ifile++;
                clsContent.RenewAccessToken().Wait();
                var tt = (int)DateTime.Now.Subtract(clsContent.TokenAcquired).TotalSeconds;

                GetspPath(rec);

                try
                {
                    if (recServer.spSiteType == "SharePoint")
                    {
                        var item = await clsContent.graphClient.Sites[m_targetSiteId].Drive.Root.ItemWithPath(rec.spPath).Request().GetAsync();
                    }
                    else // OneDrive
                    {
                        var item = await clsContent.graphClient.Users[m_targetUser].Drive.Root.ItemWithPath(rec.spPath).Request().GetAsync();
                    }
                    Console.WriteLine(string.Format("{0}-{1}/{2} Exists: {3} {4}", tt, m_ifile, m_totfiles, recServer.spSite, rec.spPath));
                    rec.spPathExists = true;
                    rec.build        = false;
                }
                catch (ServiceException e)
                {
                    Console.WriteLine(string.Format("{0}-{1}/{2} {3} {4}", tt, m_ifile, m_totfiles, recServer.spSite, rec.spPath));
                    UploadSession uploadSession = null;
                    try
                    {
                        var localPath = rec.serverPath.Replace(rec.serverDrev, rec.localDrevLetter + @":\");
                        using (FileStream stream = System.IO.File.Open(localPath, FileMode.Open, FileAccess.Read))
                        {
                            try
                            {
                                if (recServer.spSiteType == "SharePoint")
                                {
                                    uploadSession = await clsContent.graphClient.Sites[m_targetSiteId].Drive.Root.ItemWithPath(rec.spPath).CreateUploadSession().Request().PostAsync();
                                }
                                else
                                {
                                    uploadSession = await clsContent.graphClient.Users[m_targetUser].Drive.Root.ItemWithPath(rec.spPath).CreateUploadSession().Request().PostAsync();
                                }
                                var maxChunkSize = 32 * 320 * 1024; // 10 MB - Change this to your chunk size. 5MB is the default.
                                var provider     = new ChunkedUploadProvider(uploadSession, clsContent.graphClient, stream, maxChunkSize);

                                // Setup the chunk request necessities
                                var       chunkRequests     = provider.GetUploadChunkRequests();
                                var       readBuffer        = new byte[maxChunkSize];
                                var       trackedExceptions = new List <Exception>();
                                DriveItem itemResult        = null;

                                //upload the chunks
                                foreach (var request in chunkRequests)
                                {
                                    // Do your updates here: update progress bar, etc.
                                    // ...
                                    // Send chunk request
                                    var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions);

                                    if (result.UploadSucceeded)
                                    {
                                        itemResult       = result.ItemResponse;
                                        rec.spPathExists = true;
                                        rec.build        = false;
                                        rec.error        = null;
                                    }
                                }

                                // Check that upload succeeded
                                if (itemResult == null)
                                {
                                    Console.WriteLine("Upload failed: " + rec.spPath);
                                    clsContent.logger.Error("Upload failed: " + rec.spPath);
                                    rec.spPathExists = false;
                                    rec.error        = "Upload failed";
                                }
                            }
                            catch (ServiceException ex)
                            {
                                Console.WriteLine(ex.Message);
                                clsContent.logger.Error(rec.spPath);
                                clsContent.logger.Error(ex.Message);
                                rec.spPathExists = false;
                                rec.error        = ex.Message;
                            }
                        }
                    }
                    catch (Exception ez)
                    {
                        Console.WriteLine(ez.Message);
                        clsContent.logger.Error(rec.spPath);
                        clsContent.logger.Error(ez.Message);
                        rec.spPathExists = false;
                        rec.error        = ez.Message;
                    }
                }
                clsContent.m_db.SaveChanges();
            }
        }