Exemple #1
0
        public async Task <IActionResult> DownloadFile(string type, string name)
        {
            if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(name))
            {
                return(BadRequest(ApiResponseResource.Fail("Type or name is invalid.")));
            }

            var path = Path.Combine(Directory.GetCurrentDirectory(),
                                    "wwwroot",
                                    type,
                                    name);

            var memory = new MemoryStream();

            try
            {
                await using var stream = new FileStream(path, FileMode.Open);
                await stream.CopyToAsync(memory);
            }
            catch
            {
                return(BadRequest(ApiResponseResource.Fail("Type or name is invalid.")));
            }

            memory.Position = 0;

            return(File(memory, GetContentType(name), name));
        }
Exemple #2
0
        public async Task <IActionResult> SharedFiles(int classId, IFormFile file)
        {
            var userId = HttpContext.User.Claims.First(c => c.Type == "sub").Value;

            if (file is null)
            {
                return(BadRequest(ApiResponseResource.Fail("The file is null.")));
            }

            var fileExtension = Path.GetExtension(file.FileName);

            // Validate file
            if (file.Length <= 0)
            {
                return(BadRequest(ApiResponseResource.Fail("Size of the file is zero.")));
            }
            if (file.Length > MaxSharedFilesAllowedSized)
            {
                return(BadRequest(ApiResponseResource.Fail("Size of the file is bigger than allowed size.")));
            }
            if (string.IsNullOrWhiteSpace(fileExtension) || fileExtension != ".rar" && fileExtension != ".zip")
            {
                return(StatusCode(415, ApiResponseResource.Fail("The file format is not valid.", 415)));
            }

            var theClass = await _context.Classes.FindAsync(classId);

            if (theClass is null)
            {
                return(BadRequest(ApiResponseResource.Fail("The class not found.")));
            }
            if (theClass.InstructorId != userId)
            {
                return(BadRequest(ApiResponseResource.Fail("Only the instructor can upload.")));
            }


            // Store the file on hard disk
            var sharedFilesFolderPath = $"{_environment.WebRootPath}\\Class Shared Files\\";
            var fileName = $"{Guid.NewGuid()}{fileExtension}";
            var filePath = $"{sharedFilesFolderPath}{fileName}";

            await using var stream = System.IO.File.Create(filePath);
            await file.CopyToAsync(stream);

            // Add new file to the class shared files
            theClass.SharedFiles.Add(new SharedFile
            {
                SavedName   = fileName,
                Description = Path.GetFileNameWithoutExtension(file.FileName),
                VolumeInMg  = (file.Length / Mb).ToString("F")
            });
            await _context.SaveChangesAsync();

            return(Ok(ApiResponseResource.Success("New shared file added.")));
        }