Ejemplo n.º 1
0
        public async Task DeletePackage(UserInfo userInfo, IStorageFeed feed, PackageName packageName, IPackageStorageItem packageItem, IPackage package)
        {
            var statusPackageBuilder = CreatePackage(packageName, package);
            var statusPackageState   = PackageState.Partial;

            try
            {
                var allRead     = true;
                var statusFiles = new List <ImageStatusFile>();

                foreach (var statusPackageFile in package.GetFiles().Where(f => f.Path.EndsWith(".smbsrc")))
                {
                    try
                    {
                        var imageStatus = ReadFile <ImageStatus>(statusPackageFile);
                        statusFiles.Add(new ImageStatusFile(statusPackageFile.Path, imageStatus));
                    }
                    catch (Exception e)
                    {
                        support.TrackException(e, new { packageName });
                        Trace.TraceError("Error while deserializing status {0}:\n{1}", packageName, e);
                        allRead = false;
                        statusPackageBuilder.Files.Add(statusPackageFile);
                    }
                }

                var newStatusFiles = await ProcessThrottled(
                    2, statusFiles,
                    async s => await DeleteImage(feed, packageName, s));

                foreach (var statusFile in newStatusFiles)
                {
                    statusPackageBuilder.Files.Add(CreateFile(statusFile.FilePath, statusFile.ImageStatus));
                }

                if (allRead && newStatusFiles.All(s => s.ImageStatus.Check(false)))
                {
                    statusPackageState = PackageState.Deleted;
                }

                Trace.TraceInformation("Marking package {0} as {1}", packageName, statusPackageState);
            }
            catch (Exception e)
            {
                support.TrackException(e, new { packageName });
                Trace.TraceError("Error while deleting package {0}:\n{1}", packageName, e);
                statusPackageBuilder.Files.Add(CreateFile("error.txt", e.ToString()));
            }

            if (statusPackageBuilder.Files.Count == 0)
            {
                statusPackageBuilder.Files.Add(CreateFile("empty.txt", string.Empty));
            }

            Debug.WriteLine("Saving package processing status {0}", packageName);
            var statusPackageItem = feed.GetPackage(await packageItem.GetUserName(), statusPackageState, packageName);

            using (var statusStream = await statusPackageItem.Put())
                statusPackageBuilder.SaveBuffered(statusStream);

            switch (statusPackageState)
            {
            case PackageState.Partial:
                await notifier.PartiallyDeleted(userInfo, packageName);

                break;

            case PackageState.Deleted:
                await notifier.Deleted(userInfo, packageName);

                break;

            default:
                // ReSharper disable once NotResolvedInText
                throw new ArgumentOutOfRangeException("statusPackageState", statusPackageState, null);
            }
        }