Search in sources :

Example 11 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project ArTEMiS by ls1intum.

the class AttachmentUnitIntegrationTest method initTestCase.

@BeforeEach
public void initTestCase() throws Exception {
    this.database.addUsers(1, 1, 0, 1);
    this.attachment = ModelFactory.generateAttachment(null);
    this.attachment.setLink("files/temp/example.txt");
    this.lecture1 = this.database.createCourseWithLecture(true);
    this.attachmentUnit = new AttachmentUnit();
    this.attachmentUnit.setDescription("Lorem Ipsum");
    // Add users that are not in the course
    userRepository.save(ModelFactory.generateActivatedUser("student42"));
    userRepository.save(ModelFactory.generateActivatedUser("tutor42"));
    userRepository.save(ModelFactory.generateActivatedUser("instructor42"));
}
Also used : AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project Artemis by ls1intum.

the class LectureImportService method cloneLectureUnit.

/**
 * This helper function clones the {@code importedLectureUnit} and returns it
 *
 * @param importedLectureUnit The original lecture unit to be copied
 * @param newLecture The new lecture to which the lecture units are appended
 * @return The cloned lecture unit
 */
private LectureUnit cloneLectureUnit(final LectureUnit importedLectureUnit, final Lecture newLecture) {
    log.debug("Creating a new LectureUnit from lecture unit {}", importedLectureUnit);
    if (importedLectureUnit instanceof TextUnit) {
        TextUnit textUnit = new TextUnit();
        textUnit.setName(importedLectureUnit.getName());
        textUnit.setReleaseDate(importedLectureUnit.getReleaseDate());
        textUnit.setContent(((TextUnit) importedLectureUnit).getContent());
        return textUnit;
    } else if (importedLectureUnit instanceof VideoUnit) {
        VideoUnit videoUnit = new VideoUnit();
        videoUnit.setName(importedLectureUnit.getName());
        videoUnit.setReleaseDate(importedLectureUnit.getReleaseDate());
        videoUnit.setDescription(((VideoUnit) importedLectureUnit).getDescription());
        videoUnit.setSource(((VideoUnit) importedLectureUnit).getSource());
        return videoUnit;
    } else if (importedLectureUnit instanceof AttachmentUnit) {
        // Create and save the attachment unit, then the attachment itself, as the id is needed for file handling
        AttachmentUnit attachmentUnit = new AttachmentUnit();
        attachmentUnit.setDescription(((AttachmentUnit) importedLectureUnit).getDescription());
        attachmentUnit.setLecture(newLecture);
        lectureUnitRepository.save(attachmentUnit);
        Attachment attachment = cloneAttachment(((AttachmentUnit) importedLectureUnit).getAttachment());
        attachment.setAttachmentUnit(attachmentUnit);
        attachmentRepository.save(attachment);
        attachmentUnit.setAttachment(attachment);
        return attachmentUnit;
    } else if (importedLectureUnit instanceof ExerciseUnit) {
        // We have a dedicated exercise import system, so this is left out for now
        return null;
    }
    return null;
}
Also used : Attachment(de.tum.in.www1.artemis.domain.Attachment)

Example 13 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project Artemis by ls1intum.

the class FileIntegrationTest method uploadAttachmentUnit.

private AttachmentUnit uploadAttachmentUnit(MockMultipartFile file, Long lectureId, HttpStatus expectedStatus) throws Exception {
    Lecture lecture = lectureRepo.findByIdWithPostsAndLectureUnitsAndLearningGoals(lectureId).get();
    AttachmentUnit attachmentUnit = database.createAttachmentUnit(false);
    // upload file
    JsonNode response = request.postWithMultipartFile("/api/fileUpload?keepFileName=true", file.getOriginalFilename(), "file", file, JsonNode.class, expectedStatus);
    if (expectedStatus != HttpStatus.CREATED) {
        return null;
    }
    String responsePath = response.get("path").asText();
    // move file from temp folder to correct folder
    var targetFolder = Path.of(FilePathService.getAttachmentUnitFilePath(), String.valueOf(attachmentUnit.getId())).toString();
    fileService.manageFilesForUpdatedFilePath(null, responsePath, targetFolder, lecture.getId(), true);
    var attachmentPath = targetFolder + "/" + file.getOriginalFilename();
    attachmentUnit.getAttachment().setLink(attachmentPath);
    attachmentRepo.save(attachmentUnit.getAttachment());
    return attachmentUnit;
}
Also used : AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 14 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project Artemis by ls1intum.

the class FileIntegrationTest method createLectureWithLectureUnits.

public Lecture createLectureWithLectureUnits(HttpStatus expectedStatus) throws Exception {
    Lecture lecture = database.createCourseWithLecture(true);
    lecture.setTitle("Test title");
    lecture.setDescription("Test");
    lecture.setStartDate(ZonedDateTime.now().minusHours(1));
    lectureRepo.save(lecture);
    Long lectureId = lecture.getId();
    // create pdf file 1
    AttachmentUnit unit1;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PDDocument doc1 = new PDDocument()) {
        doc1.addPage(new PDPage());
        doc1.addPage(new PDPage());
        doc1.addPage(new PDPage());
        doc1.save(outputStream);
        MockMultipartFile file1 = new MockMultipartFile("file", "file.pdf", "application/json", outputStream.toByteArray());
        unit1 = uploadAttachmentUnit(file1, lectureId, expectedStatus);
    }
    // create image file
    MockMultipartFile file2 = new MockMultipartFile("file", "filename2.png", "application/json", "some text".getBytes());
    AttachmentUnit unit2 = uploadAttachmentUnit(file2, lectureId, expectedStatus);
    // create pdf file 3
    AttachmentUnit unit3;
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PDDocument doc2 = new PDDocument()) {
        doc2.addPage(new PDPage());
        doc2.addPage(new PDPage());
        doc2.save(outputStream);
        MockMultipartFile file3 = new MockMultipartFile("file", "filename3.pdf", "application/json", outputStream.toByteArray());
        unit3 = uploadAttachmentUnit(file3, lectureId, expectedStatus);
    }
    lecture = database.addLectureUnitsToLecture(lecture, Set.of(unit1, unit2, unit3));
    return lecture;
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 15 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project Artemis by ls1intum.

the class AttachmentUnitResource method getAttachmentUnit.

/**
 * GET /lectures/:lectureId/attachment-units/:attachmentUnitId : gets the attachment unit with the specified id
 *
 * @param attachmentUnitId the id of the attachmentUnit to retrieve
 * @param lectureId the id of the lecture to which the unit belongs
 * @return the ResponseEntity with status 200 (OK) and with body the attachment unit, or with status 404 (Not Found)
 */
@GetMapping("/lectures/{lectureId}/attachment-units/{attachmentUnitId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<AttachmentUnit> getAttachmentUnit(@PathVariable Long attachmentUnitId, @PathVariable Long lectureId) {
    log.debug("REST request to get AttachmentUnit : {}", attachmentUnitId);
    AttachmentUnit attachmentUnit = attachmentUnitRepository.findByIdElseThrow(attachmentUnitId);
    if (attachmentUnit.getLecture() == null || attachmentUnit.getLecture().getCourse() == null) {
        throw new ConflictException("Lecture unit must be associated to a lecture of a course", "AttachmentUnit", "lectureOrCourseMissing");
    }
    if (!attachmentUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Requested lecture unit is not part of the specified lecture", "AttachmentUnit", "lectureIdMismatch");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, attachmentUnit.getLecture().getCourse(), null);
    return ResponseEntity.ok().body(attachmentUnit);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

AttachmentUnit (de.tum.in.www1.artemis.domain.lecture.AttachmentUnit)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)8 LectureUnit (de.tum.in.www1.artemis.domain.lecture.LectureUnit)6 Lecture (de.tum.in.www1.artemis.domain.Lecture)4 BadRequestException (javax.ws.rs.BadRequestException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Attachment (de.tum.in.www1.artemis.domain.Attachment)2 ExerciseUnit (de.tum.in.www1.artemis.domain.lecture.ExerciseUnit)2 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2