Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
AttachmentController
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 9
182
0.00% covered (danger)
0.00%
0 / 1
 getFile
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getList
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 deleteFile
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getData
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getAttachmentPage
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getAttachmentsPages
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 postFile
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 getRequestedAttachmentById
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 isGroupAllowed
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4// TODO unit tests !
5// TODO api-specs
6
7use Slim\Exception\HttpBadRequestException;
8use Slim\Exception\HttpForbiddenException;
9use Slim\Exception\HttpNotFoundException;
10use Slim\Http\ServerRequest as Request;
11use Slim\Http\Response;
12
13class AttachmentController extends Controller {
14  public static function getFile(Request $request, Response $response): Response {
15    $attachment = AttachmentController::getRequestedAttachmentById($request);
16    $filePath = AttachmentFiles::getAttachmentFilePath(
17      self::authToken($request)->getWorkspaceId(),
18      (string) $request->getAttribute('fileId'),
19      $attachment
20    );
21
22    $response->write(file_get_contents($filePath));
23
24    return $response->withHeader('Content-Type', FileExt::getMimeType($filePath));
25  }
26
27  public static function getList(Request $request, Response $response): Response {
28    $authToken = self::authToken($request);
29    $groupNames = [$authToken->getGroup()];
30
31    return $response->withJson(self::adminDAO()->getAttachments($authToken->getWorkspaceId(), $groupNames));
32  }
33
34  public static function deleteFile(Request $request, Response $response): Response {
35    AttachmentFiles::deleteFile(
36      self::authToken($request)->getWorkspaceId(),
37      (string) $request->getAttribute('fileId'),
38      self::getRequestedAttachmentById($request)
39    );
40
41    return $response->withStatus(200);
42  }
43
44  public static function getData(Request $request, Response $response): Response {
45    $attachment = AttachmentController::getRequestedAttachmentById($request);
46    return $response->withJson($attachment);
47  }
48
49  public static function getAttachmentPage(Request $request, Response $response): Response {
50    $attachment = AttachmentController::getRequestedAttachmentById($request);
51    $labelTemplate = $request->getParam('labelTemplate');
52
53    $pdfString = AttachmentTemplate::render($labelTemplate, $attachment);
54
55    $response->write($pdfString);
56    return $response
57      ->withHeader('Content-Type', "application/pdf")
58      ->withHeader('Content-Disposition', "attachment; filename=pages.zip")
59      ->withHeader('Content-length', strlen($pdfString));
60  }
61
62  public static function getAttachmentsPages(Request $request, Response $response): Response {
63    $authToken = self::authToken($request);
64    $groupNames = [$authToken->getGroup()];
65    $labelTemplate = $request->getParam('labelTemplate');
66
67    $attachments = self::adminDAO()->getAttachments($authToken->getWorkspaceId(), $groupNames);
68    $pdfString = AttachmentTemplate::render($labelTemplate, ...$attachments);
69
70    $response->write($pdfString);
71    return $response
72      ->withHeader('Content-Type', "application/pdf")
73      ->withHeader('Content-Disposition', "attachment; filename=pages.pdf")
74      ->withHeader('Content-length', strlen($pdfString));
75  }
76
77  public static function postFile(Request $request, Response $response): Response {
78    $attachmentId = (string) $request->getAttribute('attachmentId');
79    if (!$attachmentId) {
80      throw new HttpBadRequestException($request, "AttachmentId Missing!");
81    }
82
83    $type = $request->getParam('type');
84    if (!$type) {
85      throw new HttpBadRequestException($request, "No type given");
86    }
87
88    $workspace = new Workspace(self::authToken($request)->getWorkspaceId());
89    $workspacePath = $workspace->getWorkspacePath();
90    $attachment = AttachmentController::getRequestedAttachmentById($request);
91
92    $uploadedFiles = UploadedFilesHandler::handleUploadedFiles($request, 'attachment', $workspacePath);
93
94    AttachmentFiles::importFiles(
95      $workspace->getId(),
96      $uploadedFiles,
97      $attachment,
98      $type
99    );
100
101    return $response->withStatus(201);
102  }
103
104  private static function getRequestedAttachmentById(Request $request): Attachment {
105    $authToken = self::authToken($request);
106
107    $attachmentId = (string) $request->getAttribute('attachmentId');
108    $attachment = AttachmentController::adminDAO()->getAttachmentById($attachmentId);
109
110    if (!AttachmentController::isGroupAllowed($authToken, $attachment->_groupName)) {
111      throw new HttpForbiddenException($request, "Access to attachment `$attachmentId` not given");
112    }
113
114    return $attachment;
115  }
116
117  private static function isGroupAllowed(AuthToken $authToken, string $groupName): bool {
118    if ($authToken->getMode() == 'monitor-group') {
119      return $authToken->getGroup() == $groupName;
120    }
121
122    return false;
123  }
124}