Exemple #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;
            }
        }
Exemple #2
0
        /// <summary>
        /// Upload the Assignment into Upload Respository
        /// </summary>
        /// <param name="file"></param>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public async Task <Upload> UploadFileAsync(IFormFile file, string fileName)
        {
            Upload upload;

            try
            {
                using (Stream stream = new MemoryStream())
                {
                    string filePath = "";
                    file.OpenReadStream();
                    await file.CopyToAsync(stream);

                    stream.Seek(0, SeekOrigin.Begin);
                    bool doesBlockExitsAsync = await _blobHelper.DoesBlobExistsAsync("assignments", fileName);

                    if (doesBlockExitsAsync)
                    {
                        bool deleteBlobAsync = await _blobHelper.DeleteBlobAsync("assignments", fileName);

                        if (deleteBlobAsync)
                        {
                            filePath = await _blobHelper.UploadFileToBlobAsync("assignments", fileName, stream);
                        }
                    }
                    else
                    {
                        filePath = await _blobHelper.UploadFileToBlobAsync("assignments", fileName, stream);
                    }
                    upload = new Upload {
                        Id = Guid.NewGuid(), FileName = fileName, FilePath = filePath
                    };
                    await _uploadRepository.UploadFileAsync(upload);
                }
            }
            catch (Exception ex)
            {
                Logging.Logger.LogException(ex);
                return(null);
            }
            return(upload);
        }
Exemple #3
0
        private async Task <Upload> UploadFileAsync(IFormFile file, String fileName)
        {
            Upload upload  = null;
            string fileUri = string.Empty;

            using (Stream stream = new MemoryStream())
            {
                file.OpenReadStream();
                await file.CopyToAsync(stream);

                stream.Seek(0, SeekOrigin.Begin);
                fileUri = await _blobHelper.UploadFileToBlobAsync("assignments", fileName, stream);
            }

            if (fileUri != string.Empty)
            {
                upload = new Upload {
                    Id = Guid.NewGuid(), FileName = fileName, FilePath = fileUri
                };
                int uploadResponseByLearner = await _requestRepository.UploadFileAsync(upload);
            }
            return(upload);
        }