예제 #1
0
        public void AddObject(MiObject obj)
        {
            this._objects.Add(obj);

            UpdateCurrentTimeAndSize();
        }
예제 #2
0
        public static async Task DownloadObject(MiObject obj, string patten, string bucket, string targetRootPath, bool autoDecrypt, Action <MiObject, string> OnDownload)
        {
            if (obj.IsDirectory)
            {
                List <Task> tasks = new List <Task>();
                foreach (var subObj in ((MiDirectoryObject)obj).Objects)
                {
                    tasks.Add(DownloadObject(subObj, patten, bucket, targetRootPath, autoDecrypt, OnDownload));
                }
                await Task.WhenAll(tasks);

                if (OnDownload != null)
                {
                    OnDownload(obj, Path.Combine(targetRootPath, obj.Uri));
                }
            }
            else
            {
                Regex reg = new Regex(patten);
                if (reg.Match(obj.Name).Success)
                {
                    var targetPath      = Path.Combine(targetRootPath, obj.Uri);
                    var tmpPath         = string.Format("{0}.tmp", targetPath);
                    var targetDirectory = Path.GetDirectoryName(targetPath);

                    if (!Directory.Exists(targetDirectory))
                    {
                        Directory.CreateDirectory(targetDirectory);
                    }

                    if (!File.Exists(targetPath))
                    {
                        await Utility.RetryAsync(
                            async() =>
                        {
                            using (var fileStream = File.Create(tmpPath))
                            {
                                using (var responseStream = await MiHttpGet(string.Format("{0}/{1}", bucket, obj.Uri), string.Empty))
                                {
                                    using (var memoryStream = new MemoryStream())
                                    {
                                        bool decryptneeded = false;

                                        await responseStream.CopyToAsync(memoryStream);
                                        memoryStream.Seek(0, SeekOrigin.Begin);
                                        var byte1 = memoryStream.ReadByte();
                                        var byte2 = memoryStream.ReadByte();
                                        memoryStream.Seek(0, SeekOrigin.Begin);

                                        if (autoDecrypt)
                                        {
                                            // gzip file begin with 0x1f, 0x8b
                                            if (byte1 != 0x1f || byte2 != 0x8b)
                                            {
                                                decryptneeded = true;
                                            }
                                        }

                                        if (decryptneeded)
                                        {
                                            //using (var decrypedStream = LogHelper.LogHelper.GetDecryptedStream(memoryStream))
                                            //{
                                            //    await decrypedStream.CopyToAsync(fileStream);
                                            //}
                                        }
                                        else
                                        {
                                            await memoryStream.CopyToAsync(fileStream);
                                        }
                                    }
                                }
                            }
                        }, TimeSpan.FromSeconds(1), 3);

                        File.Move(tmpPath, targetPath);

                        if (OnDownload != null)
                        {
                            OnDownload(obj, targetPath);
                        }
                    }
                }
            }
        }