Example #1
0
        private FileCache _buildCache()
        {
            var uc = FileCache.Load("./foo.cache");

            var c = new BUCommon.Container {
                accountID = 1, id = "blargacct", name = "blarg account", type = "blarg"
            };
            var ff = new FreezeFile
            {
                fileID      = "blarg"
                , localHash = new BUCommon.Hash {
                    type = "SHA0", raw = new byte[] { 0, 22, 44, 11 }
                }
                , mimeType   = "application/byte-stream"
                , modified   = new DateTime(2016, 12, 01)
                , path       = "blarg/blarg1.obj"
                , storedHash = BUCommon.Hash.Create("SHA0", new byte[] { 22, 44, 0, 89 })
                , uploaded   = new DateTime(2016, 12, 03)
                , container  = c
            };

            uc.add(c);
            uc.add(ff);

            return(uc);
        }
Example #2
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);
        }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <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);
        }
Example #5
0
        /// <summary>
        /// populate the list of
        /// </summary>
        public IReadOnlyList <BUCommon.Container> getContainers()
        {
            // if (string.IsNullOrWhiteSpace(_opts.AuthorizationToken)) { throw new ArgumentException("Open the connection first."); }
            var res = _client.Buckets.GetList().Result;

            List <BUCommon.Container> buckets = new List <BUCommon.Container>();

            foreach (var x in res)
            {
                var cb = new BUCommon.Container
                {
                    id          = x.BucketId
                    , accountID = account.id
                    , name      = x.BucketName
                    , type      = x.BucketType
                };

                buckets.Add(cb);
            }

            return(buckets);
        }
Example #6
0
        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);
        }
Example #7
0
 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);
 }