Example #1
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             _timer.Dispose();
             _semaphore.Dispose();
         }
         disposedValue = true;
     }
 }
        public async System.Threading.Tasks.Task <SoftmakeAll.SDK.OperationResult <System.Byte[]> > DownloadAsync(System.String ShareName, System.Collections.Generic.Dictionary <System.String, System.String> StorageFileNames)
        {
            SoftmakeAll.SDK.CloudStorage.Azure.Environment.Validate();

            SoftmakeAll.SDK.OperationResult <System.Byte[]> OperationResult = new SoftmakeAll.SDK.OperationResult <System.Byte[]>();

            if ((System.String.IsNullOrWhiteSpace(ShareName)) || (StorageFileNames == null) || (StorageFileNames.Count == 0))
            {
                OperationResult.Message = "The ShareName and StorageFileName cannot be null.";
                return(OperationResult);
            }

            try
            {
                global::Azure.Storage.Files.Shares.ShareClient          ShareClient          = new global::Azure.Storage.Files.Shares.ShareClient(SoftmakeAll.SDK.CloudStorage.Azure.Environment._ConnectionString, ShareName);
                global::Azure.Storage.Files.Shares.ShareDirectoryClient ShareDirectoryClient = ShareClient.GetRootDirectoryClient();
                if (StorageFileNames.Count == 1)
                {
                    global::Azure.Storage.Files.Shares.ShareFileClient ShareFileClient = ShareDirectoryClient.GetFileClient(StorageFileNames.First().Key);

                    if (!(await ShareFileClient.ExistsAsync()))
                    {
                        OperationResult.Message = "File not found.";
                        return(OperationResult);
                    }

                    global::Azure.Storage.Files.Shares.Models.ShareFileDownloadInfo ShareFileDownloadInfo = await ShareFileClient.DownloadAsync();

                    using (System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream())
                    {
                        ShareFileDownloadInfo.Content.CopyTo(MemoryStream);
                        OperationResult.Data = MemoryStream.ToArray();
                    }
                }
                else
                {
                    System.Threading.SemaphoreSlim SemaphoreSlim = new System.Threading.SemaphoreSlim(StorageFileNames.Count);
                    System.Collections.Generic.List <System.Threading.Tasks.Task> DownloadTasks = new System.Collections.Generic.List <System.Threading.Tasks.Task>();
                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> StorageFileName in StorageFileNames)
                    {
                        global::Azure.Storage.Files.Shares.ShareFileClient ShareFileClient = ShareDirectoryClient.GetFileClient(StorageFileName.Key);
                        if (!(await ShareFileClient.ExistsAsync()))
                        {
                            continue;
                        }

                        await SemaphoreSlim.WaitAsync();

                        DownloadTasks.Add(System.Threading.Tasks.Task.Run(async() =>
                        {
                            global::Azure.Storage.Files.Shares.Models.ShareFileDownloadInfo ShareFileDownloadInfo = await ShareFileClient.DownloadAsync();
                            using (System.IO.FileStream FileStream = System.IO.File.OpenWrite(StorageFileName.Key))
                                ShareFileDownloadInfo.Content.CopyTo(FileStream);

                            SemaphoreSlim.Release();
                        }
                                                                          ));
                    }
                    await System.Threading.Tasks.Task.WhenAll(DownloadTasks);

                    SemaphoreSlim.Dispose();

                    OperationResult.Data = SoftmakeAll.SDK.Files.Compression.CreateZipArchive(StorageFileNames);

                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> StorageFileName in StorageFileNames)
                    {
                        if (System.IO.File.Exists(StorageFileName.Key))
                        {
                            try { System.IO.File.Delete(StorageFileName.Key); } catch { }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                if (StorageFileNames.Count > 0)
                {
                    foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> StorageFileName in StorageFileNames)
                    {
                        if (System.IO.File.Exists(StorageFileName.Key))
                        {
                            try { System.IO.File.Delete(StorageFileName.Key); } catch { }
                        }
                    }
                }

                OperationResult.Message = ex.Message;
                return(OperationResult);
            }

            OperationResult.ExitCode = 0;
            return(OperationResult);
        }