Esempio n. 1
0
        public async Task<FileDownloadResponse> DownloadZipFile(IEnumerable<Upload> uploads)
        {
            try
            {
                var fileDownloadResponse = new FileDownloadResponse();
                string currentFormattedTime = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
                string inputZipFileName = "AssignmentSolution_" + currentFormattedTime;
                string inputZipFileNameWithExtension = inputZipFileName + ".zip";
                string rootPath = _hostingEnvironment.WebRootPath;
                string fullPath = Path.Combine(rootPath, inputZipFileName);
                string zipPath = Path.Combine(rootPath, inputZipFileNameWithExtension);
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }

                int count = 0;

                foreach (var file in uploads)
                {
                    var blobFileStream = await _blobHelper.DownloadBlobAsync("assignments", file.FileName.Trim());
                    if (blobFileStream != null)
                    {
                        string localFilePath = Path.Combine(fullPath, file.FileName);
                        using (var localFileStream = new FileStream(localFilePath, FileMode.OpenOrCreate))
                        {
                            blobFileStream.Position = 0;
                            blobFileStream.CopyTo(localFileStream);
                        }
                        count++;
                    }
                }
                if(count > 0)
                {
                    ZipFile.CreateFromDirectory(fullPath, zipPath);
                    var fileStream = File.OpenRead(zipPath);
                    var blobFileUrl = await _blobHelper.UploadFileToBlobAsync("zipped-files", inputZipFileNameWithExtension,fileStream);
                    if(blobFileUrl != null)
                    {
                        fileDownloadResponse.IsSuccess = true;
                        fileDownloadResponse.Message = "File Downloaded Successfully";
                        fileDownloadResponse.FileUrl = blobFileUrl;
                    }
                    else
                    {
                        fileDownloadResponse = new FileDownloadResponse { Message = "No File Available to Download." };
                    }
                }
                else
                {
                    fileDownloadResponse = new FileDownloadResponse { Message = "No Files Available to Download." };
                }
                Directory.Delete(fullPath, true);
                return fileDownloadResponse;
            }
            catch (Exception ex)
            {
                return null;
            }
        }