コード例 #1
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
        public async Task <System.IO.Stream> downloadFileAsync(object data, BUCommon.FreezeFile file)
        {
            TransferData td = data as TransferData;

            if (string.IsNullOrWhiteSpace(file.fileID))
            {
                throw new ArgumentNullException("fileID");
            }

            if (td.client == null)
            {
                _getDLAuth(td);
            }

            var task = await td.client.Files.DownloadById(file.fileID);

            file.uploaded   = task.UploadTimestampDate;
            file.storedHash = BUCommon.Hash.FromString("SHA1", task.ContentSHA1);
            file.mimeType   = task.ContentType;
            file.path       = task.FileName;
            if (task.FileInfo != null)
            {
                string lastmillis = null;
                /*x-bz-info-src_last_modified_millis */
                if (task.FileInfo.TryGetValue("x-bz-info-src_last_modified_millis", out lastmillis))
                {
                    var dto = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(lastmillis));
                    file.modified = dto.DateTime.ToLocalTime();
                }
            }

            return(new System.IO.MemoryStream(task.FileData));
        }
コード例 #2
0
        public void CmdCopyLocal()
        {
            var acct = _getAcct();

            var cont = acct.service.fileCache.getContainers(acct.id).FirstOrDefault();

            BUCommon.FreezeFile rmt = null;
            if (cont.files.Count > 0)
            {
                rmt = cont.files[0];
            }

            Assert.IsNotNull(rmt);
            Assert.IsNotNull(rmt.fileID);

            var cl = new BackupLib.commands.CopyLocal
            {
                account    = acct
                , key      = privateKey
                , destPath = testDest
                , cont     = cont
                , maxTasks = 1
                , noAction = true
                , filterre = rmt.path
            };

            System.IO.Directory.CreateDirectory(cl.destPath);
            cl.run();
        }
コード例 #3
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
        public async Task <BUCommon.FreezeFile> deleteAsync(BUCommon.FreezeFile file)
        {
            var res = await _client.Files.Delete(file.fileID, file.path);

            var ff = new BUCommon.FreezeFile
            {
                fileID        = res.FileId
                , container   = file.container
                , path        = res.FileName
                , uploaded    = res.UploadTimestampDate
                , mimeType    = res.ContentType
                , storedHash  = BUCommon.Hash.FromString("SHA1", res.ContentSHA1)
                , serviceInfo = res.Action
            };

            if (res.FileInfo.ContainsKey(LAST_MOD_MILLIS))
            {
            }
            if (res.FileInfo.ContainsKey(LOCALHASH))
            {
                ff.enchash = res.FileInfo[LOCALHASH];
            }

            return(ff);
        }
コード例 #4
0
ファイル: UnitTest1.cs プロジェクト: CodeFork/b2_autopush
        public void B2UploadTest()
        {
            BUCommon.AccountList accts = BackupLib.AccountBuilder.BuildAccounts();
            var acct = accts.accounts.FirstOrDefault();

            Assert.AreEqual("CommB2.Connection", acct.svcName);
            acct.service.authorize();

            var cont = acct.service.getContainers().FirstOrDefault();

            Assert.IsNotNull(cont);
            string srcfile = "c:\\tmp\\photos\\2016\\1231-newyears\\DSC06562.ARW";
            var    ff      = new BUCommon.FreezeFile {
                path = "2016/1231-newyears/DSC06562.ARW"
            };

            var rsa     = BackupLib.KeyLoader.LoadRSAKey("c:\\tmp\\id_rsa_1_pub");
            var fe      = new BackupLib.FileEncrypt(rsa);
            var fs      = ff.readStream("c:\\tmp\\photos");
            var encstrm = fe.encrypt(fs);
            var sha1    = System.Security.Cryptography.SHA1Cng.Create();

            var res = sha1.ComputeHash(encstrm);

            encstrm.Seek(0, System.IO.SeekOrigin.Begin);

            DateTime now = DateTime.Now;
            var      td  = acct.service.threadStart();

            acct.service.uploadFile(td, cont, ff, encstrm, null);
            acct.service.threadStop(td);
            Assert.AreEqual(2017, ff.uploaded.Year);
            Assert.AreEqual(now.Month, ff.uploaded.Month);
            Assert.AreEqual(now.Day, ff.uploaded.Day);
        }
コード例 #5
0
ファイル: FileCache.cs プロジェクト: CodeFork/b2_autopush
        /// <summary>
        /// purge the container and all files in it from the cache.
        /// </summary>
        /// <param name="cont"></param>
        public void delete(Container cont)
        {
            var c1 = _containers.Where(x => x.accountID == cont.accountID && x.id == cont.id).FirstOrDefault();

            if (c1 != null)
            {
                _containers.Remove(c1);
                BUCommon.FreezeFile ff = _files.Where(x => x.container.id == c1.id && x.container.accountID == c1.accountID).FirstOrDefault();
                while (ff != null)
                {
                    _files.Remove(ff);
                    ff = _files.Where(x => x.container.id == c1.id && x.container.accountID == c1.accountID).FirstOrDefault();
                }
            }
        }
コード例 #6
0
        public void TestAccountBuilderSave()
        {
            var accts = BackupLib.AccountBuilder.BuildAccounts();

            var cont = new BUCommon.Container {
                accountID = 2, id = @"C:\tmp\b2test\cont1", name = "cont1"
            };

            accts.filecache.add(cont);
            var ff = new BUCommon.FreezeFile {
                container = cont, fileID = @"C:\tmp\b2test\cont1\2016\1231-newyears\DSC06560.ARW", path = "2016/1231-newyears/DSC06560.ARW"
            };

            accts.filecache.add(ff);
            BackupLib.AccountBuilder.Save(accts);
        }
コード例 #7
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
        /// <summary>
        /// get the list of files in the specified bucket.
        /// </summary>
        /// <param name="bucketid"></param>
        /// <returns></returns>
        public IReadOnlyList <BUCommon.FreezeFile> getFiles(BUCommon.Container cont)
        {
            List <BUCommon.FreezeFile> list = new List <BUCommon.FreezeFile>();

            B2Net.Models.B2FileList files = null;
            string startfile = null;

            do
            {
                files = _client.Files.GetList(startfile, null, cont.id).Result;
                foreach (var f in files.Files)
                {
                    BUCommon.Hash h = null;
                    try {
                        h = BUCommon.Hash.FromString("SHA1", f.ContentSHA1);
                    } catch (FormatException fe)
                    {
                        h = BUCommon.Hash.Create("", new byte[] {});
                    }

                    var ff = new BUCommon.FreezeFile
                    {
                        path          = f.FileName
                        , uploaded    = f.UploadTimestampDate
                        , fileID      = f.FileId
                        , storedHash  = h
                        , mimeType    = f.ContentType
                        , serviceInfo = f.Action
                        , container   = cont
                    };

                    if (f.FileInfo.ContainsKey(LAST_MOD_MILLIS))
                    {
                    }
                    if (f.FileInfo.ContainsKey(LOCALHASH))
                    {
                        ff.enchash = f.FileInfo[LOCALHASH];
                    }

                    list.Add(ff);
                }
                startfile = files.NextFileName;
            } while (startfile != null);

            return(list);
        }
コード例 #8
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
        /// <summary>
        ///
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        public IReadOnlyList <BUCommon.FreezeFile> getVersions(BUCommon.Container cont)
        {
            List <BUCommon.FreezeFile> list = new List <BUCommon.FreezeFile>();

            B2Net.Models.B2FileList files = null;
            string startfile   = null;
            string startfileID = null;

            do
            {
                files       = _client.Files.GetVersions(startfile, null, null, cont.id).Result;
                startfile   = files.NextFileName;
                startfileID = files.NextFileId;

                foreach (var f in files.Files)
                {
                    var ff = new BUCommon.FreezeFile
                    {
                        path          = f.FileName
                        , uploaded    = f.UploadTimestampDate
                        , mimeType    = f.ContentType
                        , fileID      = f.FileId
                        , storedHash  = BUCommon.Hash.FromString("SHA1", f.ContentSHA1)
                        , container   = cont
                        , serviceInfo = f.Action
                    };

                    if (f.FileInfo.ContainsKey(LAST_MOD_MILLIS))
                    {
                    }
                    if (f.FileInfo.ContainsKey(LOCALHASH))
                    {
                        ff.enchash = f.FileInfo[LOCALHASH];
                    }

                    list.Add(ff);
                }
            } while (startfile != null);

            return(list);
        }
コード例 #9
0
ファイル: UnitTest2.cs プロジェクト: cptnalf/b2_autopush
        public void CopyLocal()
        {
            var accts = _makeSvc();
            var acct  = accts.accounts[0];

            var conts = new BackupLib.commands.Containers {
                account = acct, cache = accts.filecache
            };

            conts.run();

            BUCommon.FreezeFile rmt = null;

            var cont = accts.filecache.containers[0];

            if (cont.files.Count > 0)
            {
                rmt = cont.files[0];
            }

            Assert.That(rmt, Is.Not.Null);
            Assert.That(rmt.fileID, Is.Not.Null);

            var cl = new BackupLib.commands.CopyLocal
            {
                account    = acct
                , cont     = cont
                , noAction = true
                , filterre = rmt.path
                , key      = @"c:\tmp\id_rsa_1"
                , destPath = @"c:\tmp\photos1"
            };

            System.IO.Directory.CreateDirectory(cl.destPath);
            cl.run();
        }
コード例 #10
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
 public System.IO.Stream downloadFile(object data, BUCommon.FreezeFile file)
 {
     return(downloadFileAsync(data, file).Result);
 }
コード例 #11
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
        public async Task <BUCommon.FreezeFile> uploadFileAsync(object threadData, BUCommon.Container cont, BUCommon.FreezeFile file, System.IO.Stream contents, string enchash)
        {
            TransferData data = threadData as TransferData;

            DateTimeOffset dto    = new DateTimeOffset(file.modified.ToUniversalTime());
            var            millis = dto.ToUnixTimeMilliseconds();
            var            argdic = new Dictionary <string, string>();

            argdic.Add(LAST_MOD_MILLIS, millis.ToString());
            argdic.Add("localhash", enchash);

            byte[] bytes = await BUCommon.IOUtils.ReadStream(contents);

            BUCommon.FreezeFile ff  = null;
            B2Net.Models.B2File res = null;
            bool delay     = false;
            int  pausetime = 1;
            int  maxDelay  = 64;

            for (int i = 0; i < 5; ++i)
            {
                if (data.auth == null)
                {
                    _getULAuth(data, cont.id);
                }

                if (delay)
                {
                    bool iserr = true;
                    while (true)
                    {
                        try {
                            System.Threading.Thread.Sleep(pausetime * 1000);
                            res = await _client.Files.Upload(bytes, file.path, cont.id, argdic);

                            iserr = false;
                        }
                        catch (System.AggregateException ag)
                        {
                            var e1 = ag.InnerExceptions.Where(x => x is System.IO.IOException || x is System.Net.Http.HttpRequestException);
                            if (e1.Any())
                            {
                                /* broken pipe, or other network issue
                                 * ,assume it's on the other end and try another url.
                                 */
                                data.auth = null;
                                delay     = false;
                                break;
                            }
                        }
                        catch (B2Net.Models.B2Exception be1)
                        {
                            if (be1.Status == "503" && pausetime < maxDelay)
                            {
                                pausetime = pausetime * 2;
                            }
                        }
                        catch (Exception e)
                        {
                            if (e is System.IO.IOException || e is System.Net.Http.HttpRequestException)
                            {
                                data.auth = null;
                                delay     = false;
                                break;
                            }
                            else
                            {
                                throw new Exception("unexpected error (timeout?)", e);
                            }
                        }

                        if (!iserr)
                        {
                            break;
                        }
                        if (pausetime > maxDelay)
                        {
                            break;
                        }
                    }

                    pausetime = 1;
                    delay     = false;
                }

                if (res == null && data.auth != null)
                {
                    try {
                        res = await _client.Files.Upload(bytes, file.path, cont.id, argdic);
                    }
                    catch (System.AggregateException ag)
                    {
                        var e1 = ag.InnerExceptions.Where(x => x is System.IO.IOException || x is System.Net.Http.HttpRequestException);
                        if (e1.Any())
                        {
                            /* broken pipe, or other network issue
                             * ,assume it's on the other end and try another url.
                             */
                            data.auth = null;
                        }
                    }
                    catch (B2Net.Models.B2Exception be)
                    {
                        if (be.Status == "401" || be.Status == "503")
                        {
                            data.auth = null;
                        }
                        else if (be.Status == "408" || be.Status == "429")
                        {
                            delay = true; pausetime = 1;
                        }
                        else
                        {
                            throw new Exception("Broken.", be);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is System.IO.IOException || e is System.Net.Http.HttpRequestException)
                        {
                            data.auth = null;
                        }
                        else
                        {
                            throw new Exception("unexpected error", e);
                        }
                    }
                }

                if (res != null)
                {
                    /* create another freezefile for the new bit. */
                    ff = new BUCommon.FreezeFile
                    {
                        fileID       = res.FileId
                        , container  = cont
                        , uploaded   = res.UploadTimestampDate
                        , storedHash = BUCommon.Hash.FromString("SHA1", res.ContentSHA1)
                        , mimeType   = res.ContentType
                        , path       = res.FileName
                        , modified   = file.modified
                    };

                    break;
                }
            }

            return(ff);
        }
コード例 #12
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
 public BUCommon.FreezeFile uploadFile(object threadData, BUCommon.Container cont, BUCommon.FreezeFile file, System.IO.Stream contents, string enchash)
 {
     return(uploadFileAsync(threadData, cont, file, contents, enchash).Result);
 }
コード例 #13
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
        public async Task <BUCommon.FreezeFile> removeAsync(BUCommon.FreezeFile file)
        {
            var res = await _client.Files.Hide(file.path, file.container.id);

            return(file);
        }
コード例 #14
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
 public void remove(BUCommon.FreezeFile file)
 {
     var x = removeAsync(file).Result;
 }
コード例 #15
0
ファイル: Connection.cs プロジェクト: cptnalf/b2_autopush
 public void delete(BUCommon.FreezeFile file)
 {
     var x = deleteAsync(file).Result;
 }