Ejemplo n.º 1
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    itemsTreeCache.Dispose();
                    UploadService.Dispose();
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.
                disposedValue = true;
            }
        }
Ejemplo n.º 2
0
        public FSProvider(AmazonDrive amazon)
        {
            this.amazon     = amazon;
            SmallFilesCache = new SmallFilesCache(amazon);
            SmallFilesCache.OnDownloadStarted = (id) =>
            {
                Interlocked.Increment(ref downloadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);
            };
            SmallFilesCache.OnDownloaded = (id) =>
            {
                Interlocked.Decrement(ref downloadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);
            };
            SmallFilesCache.OnDownloadFailed = (id) =>
            {
                Interlocked.Decrement(ref downloadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);
            };

            UploadService = new UploadService(2, amazon);
            UploadService.OnUploadFailed = (uploaditem, reason) =>
            {
                Interlocked.Decrement(ref uploadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);

                var olditemPath = Path.Combine(UploadService.CachePath, uploaditem.Id);
                File.Delete(olditemPath);

                switch (reason)
                {
                case FailReason.ZeroLength:
                    var item = GetItem(uploaditem.Path);
                    item?.MakeNotUploading();
                    return;

                case FailReason.Conflict:
                    return;
                }

                itemsTreeCache.DeleteFile(uploaditem.Path);
            };
            UploadService.OnUploadFinished = (item, node) =>
            {
                Interlocked.Decrement(ref uploadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);

                var newitem     = FSItem.FromNode(item.Path, node);
                var olditemPath = Path.Combine(UploadService.CachePath, item.Id);
                var newitemPath = Path.Combine(SmallFilesCache.CachePath, node.id);

                if (!File.Exists(newitemPath))
                {
                    File.Move(olditemPath, newitemPath);
                }
                else
                {
                    File.Delete(olditemPath);
                }

                SmallFilesCache.AddExisting(newitem);
                itemsTreeCache.Update(newitem);
            };
            UploadService.OnUploadResumed = item =>
            {
                itemsTreeCache.Add(item);
                Interlocked.Increment(ref uploadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);
            };
            UploadService.Start();
        }
Ejemplo n.º 3
0
        public IBlockStream OpenFile(string filePath, FileMode mode, FileAccess fileAccess, FileShare share, FileOptions options)
        {
            var item = GetItem(filePath);

            if (fileAccess == FileAccess.Read)
            {
                if (item == null)
                {
                    return(null);
                }

                item = WaitForReal(item, 25000);

                Log.Trace($"Opening {filePath} for Read");

                if (item.Length < SmallFileSizeLimit)
                {
                    return(SmallFilesCache.OpenReadWithDownload(item));
                }

                var result = SmallFilesCache.OpenReadCachedOnly(item);
                if (result != null)
                {
                    return(result);
                }

                Interlocked.Increment(ref downloadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);
                var buffered = new BufferedAmazonBlockReader(item, amazon);
                buffered.OnClose = () =>
                {
                    Interlocked.Decrement(ref downloadingCount);
                    OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);
                };

                return(buffered);
            }

            if (item == null || item.Length == 0)
            {
#if TRACE
                Log.Trace($"Creating {filePath} as New because mode:{mode} and {((item == null) ? "item is null" : "length is 0")}");
#endif

                var dir     = Path.GetDirectoryName(filePath);
                var name    = Path.GetFileName(filePath);
                var dirItem = GetItem(dir);

                item = FSItem.MakeUploading(filePath, Guid.NewGuid().ToString(), dirItem.Id, 0);

                var file = UploadService.OpenNew(item);
                Interlocked.Increment(ref uploadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);

                itemsTreeCache.Add(item);

                return(file);
            }

            if (item == null)
            {
                return(null);
            }

            item = WaitForReal(item, 25000);

            if ((mode == FileMode.Create || mode == FileMode.Truncate) && item.Length > 0)
            {
#if TRACE
                Log.Trace($"Opening {filePath} as Truncate because mode:{mode} and length {item.Length}");
#endif
                item.Length = 0;
                SmallFilesCache.Delete(item);
                item.MakeUploading();
                var file = UploadService.OpenTruncate(item);
                Interlocked.Increment(ref uploadingCount);
                OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);

                return(file);
            }

            if (mode == FileMode.Open || mode == FileMode.Append || mode == FileMode.OpenOrCreate)
            {
#if TRACE
                Log.Trace($"Opening {filePath} as ReadWrite because mode:{mode} and length {item.Length}");
#endif
                if (item.Length < SmallFileSizeLimit)
                {
                    var file = SmallFilesCache.OpenReadWrite(item);
                    file.OnChangedAndClosed = (it, path) =>
                    {
                        it.LastWriteTime = DateTime.UtcNow;
                        Interlocked.Increment(ref uploadingCount);
                        OnStatisticsUpdated?.Invoke(downloadingCount, uploadingCount);

                        if (!it.IsUploading)
                        {
                            it.MakeUploading();
                            var olditemPath = Path.Combine(SmallFilesCache.CachePath, item.Id);
                            var newitemPath = Path.Combine(UploadService.CachePath, item.Id);

                            if (File.Exists(newitemPath))
                            {
                                File.Delete(newitemPath);
                            }

                            SymbolicLink.CreateFile(GetRelativePath(olditemPath, Path.GetDirectoryName(newitemPath)), newitemPath);
                            SmallFilesCache.AddExisting(it);
                        }

                        UploadService.AddOverwrite(it);
                    };

                    return(file);
                }

                Log.Warn("File is too big for ReadWrite: " + filePath);
            }

            return(null);
        }