1 <?php
2 /* --------------------------------------------------------------
3 AttachmentsApiV2Controller.inc.php 2016-03-07
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2016 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('HttpApiV2Controller');
13
14 /**
15 * Class AttachmentsApiV2Controller
16 *
17 * This controller enables the API consumers to handle email attachments which can be later
18 * used in emails. The most common scenario is that if an email has attachments, they must
19 * be already uploaded before the email is sent.
20 *
21 * @category System
22 * @package ApiV2Controllers
23 */
24 class AttachmentsApiV2Controller extends HttpApiV2Controller
25 {
26 /**
27 * AttachmentsHandler
28 *
29 * @var AttachmentsHandler
30 */
31 protected $attachmentsHandler;
32
33
34 /**
35 * Initialize API Controller
36 */
37 public function __initialize()
38 {
39 $this->attachmentsHandler = MainFactory::create('AttachmentsHandler', DIR_FS_CATALOG . 'uploads');
40 }
41
42
43 /**
44 * @api {post} /attachments Upload Attachment
45 * @apiVersion 2.1.0
46 * @apiName UploadAttachments
47 * @apiGroup Emails
48 *
49 * @apiDescription
50 * If an email contains an attachment this must be uploaded before the email is sent. This
51 * method accepts the upload of one file at a time. It will return its temporary path which can
52 * be used as the attachment path in the email JSON data. The name of the file form field is not
53 * taken into concern (can be whatever). The important rule is that only one file will be uploaded
54 * at a time.
55 *
56 * @apiExample {curl} Upload Attachment
57 * curl --user admin@shop.de:12345 -F name=test -F filedata=@localfile.jpg http://shop.de/api.php/v2/attachments
58 *
59 * @apiSuccessExample {json} Success-Response
60 * {
61 * "code": 201,
62 * "status": "success",
63 * "action": "upload",
64 * "path": "/var/www/html/uploads/tmp/myfilename.txt"
65 * }
66 */
67 public function post()
68 {
69 if(!isset($_FILES) || empty($_FILES))
70 {
71 throw new HttpApiV2Exception('No attachment file was provided.', 400);
72 }
73
74 // Get the first item of $_FILES array.
75 $emailAttachment = $this->attachmentsHandler->uploadAttachment(array_shift($_FILES));
76
77 // Return success response to client.
78 $response = array(
79 'code' => 201,
80 'status' => 'success',
81 'action' => 'upload',
82 'path' => basename($emailAttachment->getPath())
83 );
84
85 $this->_writeResponse($response, 201);
86 }
87 }