Ejemplo n.º 1
0
        public async Task <Data.Models.FileInfo> MoveTempFile(string tempFileName, string relativeFolderPath, string fileName)
        {
            var(fileNameWithoutExt, extension) = FilesHelpers.GetFileNameAndExtension(fileName);
            var fileInfo = await fileProvider.MoveTempFile(tempFileName, relativeFolderPath, fileNameWithoutExt, extension);

            var file = await CreateFileInfoToDb(relativeFolderPath, fileNameWithoutExt, extension);

            return(file);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PostSubmitTask(CreateTaskSubmissionRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var studentTask = await _context.StudentAssignedTasks
                              .FirstOrDefaultAsync(st => st.Id == request.StudentTaskId);

            if (studentTask is null)
            {
                return(NotFound());
            }

            if (studentTask.StudentId != userManager.GetUserId(User))
            {
                return(Forbid());
            }

            var submission = new TaskSubmission {
                AssignedTaskId = studentTask.Id,
                State          = TaskSubmissionState.Submitted,
                DateAdded      = DateTime.Now,
                Files          = new List <TaskSubmissionFile>(),
                Description    = request.Description
            };

            _context.TaskSubmissions.Add(submission);
            await _context.SaveChangesAsync();

            foreach (var uploadedFile in request.UploadedFiles)
            {
                var submissionFolder = $"StudentSubmissions/{studentTask.StudentId}/{studentTask.Id}/{submission.Id}";

                // TODO: validate file extension.
                var(fileName, extension) = FilesHelpers.GetFileNameAndExtension(uploadedFile.OriginalName);

                var file = await fileService.MoveTempFile(uploadedFile.TempFileName, submissionFolder, uploadedFile.OriginalName);

                submission.Files.Add(new TaskSubmissionFile {
                    FileId         = file.Id,
                    FileType       = TaskSubmissionFileType.SourceCode,
                    TaskSubmission = submission
                });
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task <Data.Models.FileInfo> MoveFile(Guid sourceFileId, string relativeFolderPath, string fileName)
        {
            var(fileNameWithoutExtension, extension) = FilesHelpers.GetFileNameAndExtension(fileName);

            var file = await context.Files.FindAsync(sourceFileId);

            if (file is null)
            {
                throw new InvalidOperationException("No file with the provided sourceFileId exists.");
            }

            var sourceFileInfo = await fileProvider.GetFile(file.RelativeFolderPath, file.FileName, file.Extension);

            var fileInfo = await fileProvider.MoveFile(sourceFileInfo, relativeFolderPath, fileNameWithoutExtension, extension);

            file.RelativeFolderPath = relativeFolderPath;
            file.FileName           = fileNameWithoutExtension;
            file.Extension          = extension;
            await context.SaveChangesAsync();

            return(file);
        }