<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Amazon;
using Amazon.EC2.Model;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using BloomTemp;
using L10NSharp;
using Palaso.Code;
using Palaso.Progress;
using Palaso.UI.WindowsForms.Progress;
using RestSharp.Contrib;

namespace Bloom.WebLibraryIntegration
{
	/// <summary>
	/// Handles Bloom file/folder operations on Amazon Web Services S3 service.
	/// </summary>
	public class BloomS3Client:IDisposable
	{
		private IAmazonS3 _amazonS3;
		private TransferUtility _transferUtility;
		private readonly string _bucketName;
		public const string kDirectoryDelimeterForS3 = "/";
		public const string UnitTestBucketName = "BloomLibraryBooks-UnitTests";
		public const string SandboxBucketName = "BloomLibraryBooks-Sandbox";
		public const string ProductionBucketName = "BloomLibraryBooks";

		public BloomS3Client(string bucketName)
		{
			_bucketName = bucketName;
			_amazonS3 = AWSClientFactory.CreateAmazonS3Client(KeyManager.S3AccessKey,
				KeyManager.S3SecretAccessKey, new AmazonS3Config { ServiceURL = "https://s3.amazonaws.com" });
			Guard.AgainstNull(_amazonS3, "Connection to AWS");
			_transferUtility = new TransferUtility(_amazonS3);
		}

		/// <summary>
		/// This is set during UploadBook to the URL holding files like various thumbnails, preview, etc.
		/// It ends up in a parse.com column named "baseUrl", and the angular appends things like "/thumbnail256.png" to it.
		/// It only contains useful information after UploadBook.
		/// </summary>
		public string BaseUrl { get; private set; }

		// Similarly for the book order file.
		public string BookOrderUrl { get; private set; }

		internal string BucketName {get { return _bucketName; }}

		public bool GetBookExists(string key)
		{
			return GetBookFileCount(key) > 0;
		}

		internal int GetBookFileCount(string key)
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName,
				Prefix = key
			});
			var count = matchingFilesResponse.S3Objects.Count;
			return count;
		}

		public int GetCountOfAllFilesInBucket()
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName
			});
			return matchingFilesResponse.S3Objects.Count;
		}


		public IEnumerable<string> GetFilePaths()
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName
			});
			return from x in matchingFilesResponse.S3Objects select x.Key;
		}

		public void DeleteBookData(string key)
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName,
				Prefix = key
			});
			if (matchingFilesResponse.S3Objects.Count == 0)
				return;

			var deleteObjectsRequest = new DeleteObjectsRequest()
			{
				BucketName = UnitTestBucketName,
				Objects = matchingFilesResponse.S3Objects.Select(s3Object => new KeyVersion() { Key = s3Object.Key }).ToList()
			};

			var response = _amazonS3.DeleteObjects(deleteObjectsRequest);<commit_msg>BL-678 delete from wrong bucket<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Amazon;
using Amazon.EC2.Model;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using BloomTemp;
using L10NSharp;
using Palaso.Code;
using Palaso.Progress;
using Palaso.UI.WindowsForms.Progress;
using RestSharp.Contrib;

namespace Bloom.WebLibraryIntegration
{
	/// <summary>
	/// Handles Bloom file/folder operations on Amazon Web Services S3 service.
	/// </summary>
	public class BloomS3Client:IDisposable
	{
		private IAmazonS3 _amazonS3;
		private TransferUtility _transferUtility;
		private readonly string _bucketName;
		public const string kDirectoryDelimeterForS3 = "/";
		public const string UnitTestBucketName = "BloomLibraryBooks-UnitTests";
		public const string SandboxBucketName = "BloomLibraryBooks-Sandbox";
		public const string ProductionBucketName = "BloomLibraryBooks";

		public BloomS3Client(string bucketName)
		{
			_bucketName = bucketName;
			_amazonS3 = AWSClientFactory.CreateAmazonS3Client(KeyManager.S3AccessKey,
				KeyManager.S3SecretAccessKey, new AmazonS3Config { ServiceURL = "https://s3.amazonaws.com" });
			Guard.AgainstNull(_amazonS3, "Connection to AWS");
			_transferUtility = new TransferUtility(_amazonS3);
		}

		/// <summary>
		/// This is set during UploadBook to the URL holding files like various thumbnails, preview, etc.
		/// It ends up in a parse.com column named "baseUrl", and the angular appends things like "/thumbnail256.png" to it.
		/// It only contains useful information after UploadBook.
		/// </summary>
		public string BaseUrl { get; private set; }

		// Similarly for the book order file.
		public string BookOrderUrl { get; private set; }

		internal string BucketName {get { return _bucketName; }}

		public bool GetBookExists(string key)
		{
			return GetBookFileCount(key) > 0;
		}

		internal int GetBookFileCount(string key)
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName,
				Prefix = key
			});
			var count = matchingFilesResponse.S3Objects.Count;
			return count;
		}

		public int GetCountOfAllFilesInBucket()
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName
			});
			return matchingFilesResponse.S3Objects.Count;
		}


		public IEnumerable<string> GetFilePaths()
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName
			});
			return from x in matchingFilesResponse.S3Objects select x.Key;
		}

		public void DeleteBookData(string key)
		{
			var matchingFilesResponse = _amazonS3.ListObjects(new ListObjectsRequest()
			{
				BucketName = _bucketName,
				Prefix = key
			});
			if (matchingFilesResponse.S3Objects.Count == 0)
				return;

			var deleteObjectsRequest = new DeleteObjectsRequest()
			{
				BucketName = _bucketName,
				Objects = matchingFilesResponse.S3Objects.Select(s3Object => new KeyVersion() { Key = s3Object.Key }).ToList()
			};

			var response = _amazonS3.DeleteObjects(deleteObjectsRequest);
{'fraction_non_alphanumeric': 0.08876839659178931, 'fraction_numerical': 0.01192873741285825, 'mean_word_length': 4.2918032786885245, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5211629', 'n_tokens_mistral': 2178, 'n_tokens_neox': 1905, 'n_words': 503}