Bloom.WebLibraryIntegration.BloomS3Client.UploadBook C# (CSharp) Method

UploadBook() public method

The thing here is that we need to guarantee unique names at the top level, so we wrap the books inside a folder with some unique name. As this involves copying the folder it is also a convenient place to omit any PDF files except the one we want.
public UploadBook ( string storageKeyOfBookFolder, string pathToBloomBookDirectory, IProgress progress, string pdfToInclude = null ) : void
storageKeyOfBookFolder string
pathToBloomBookDirectory string
progress IProgress
pdfToInclude string
return void
        public void UploadBook(string storageKeyOfBookFolder, string pathToBloomBookDirectory, IProgress progress,  string pdfToInclude = null)
        {
            BaseUrl = null;
            BookOrderUrlOfRecentUpload = null;
            DeleteBookData(_bucketName, storageKeyOfBookFolder); // In case we're overwriting, get rid of any deleted files.

            //first, let's copy to temp so that we don't have to worry about changes to the original while we're uploading,
            //and at the same time introduce a wrapper with the last part of the unique key for this person+book
            string prefix = ""; // storageKey up to last slash (or empty)
            string tempFolderName = storageKeyOfBookFolder; // storage key after last slash (or all of it)

            // storageKeyOfBookFolder typically has a slash in it, email/id.
            // We only want the id as the temp folder name.
            // If there is anything before it, though, we want that as a prefix to make a parent 'folder' on parse.com.
            int index = storageKeyOfBookFolder.LastIndexOf('/');
            if (index >= 0)
            {
                prefix = storageKeyOfBookFolder.Substring(0, index + 1); // must include the slash
                tempFolderName = storageKeyOfBookFolder.Substring(index + 1);
            }

            var wrapperPath = Path.Combine(Path.GetTempPath(), tempFolderName);

            //If we previously uploaded the book, but then had a problem, this directory could still be on our harddrive. Clear it out.
            if (Directory.Exists(wrapperPath))
            {
                DeleteFileSystemInfo(new DirectoryInfo(wrapperPath));
            }

            Directory.CreateDirectory(wrapperPath);

            var destDirName = Path.Combine(wrapperPath, Path.GetFileName(pathToBloomBookDirectory));
            CopyDirectory(pathToBloomBookDirectory, destDirName);
            // Don't upload audio (todo: test).
            string audioDir = Path.Combine(destDirName, "audio");
            if (Directory.Exists(audioDir))
                SIL.IO.RobustIO.DeleteDirectory(audioDir, true);
            var unwantedPdfs = Directory.EnumerateFiles(destDirName, "*.pdf").Where(x => Path.GetFileName(x) != pdfToInclude);
            foreach (var file in unwantedPdfs)
                RobustFile.Delete(file);
            UploadDirectory(prefix, wrapperPath, progress);

            DeleteFileSystemInfo(new DirectoryInfo(wrapperPath));
        }

Usage Example

Example #1
0
        static int Main(string[] arguments)
        {
            if (arguments.Length != 1)
            {
                Console.WriteLine("Usage: BloomBookUploader path-to-folder-containing-books");
                return 1;
            }
            if (!Directory.Exists(arguments[0]))
            {
                Console.WriteLine(arguments[0]+" not found");
                return 3;
            }

            var t = new Bloom.WebLibraryIntegration.BloomS3Client(BloomS3Client.SandboxBucketName);
            t.UploadBook(Guid.NewGuid().ToString(), arguments[0]);

            return 0;
        }
All Usage Examples Of Bloom.WebLibraryIntegration.BloomS3Client::UploadBook