Skip to content

Commit b550c0d

Browse files
authored
Added Log For FS-11830 (#67)
* Update UploadProcessor.php * Add files via upload
1 parent 23230fd commit b550c0d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

filestack/UploadProcessor.php

+26
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
use GuzzleHttp\Client;
99
use GuzzleHttp\Psr7\Request;
1010

11+
use Filestack\Mixins\LoggingTrait;
12+
1113
/**
1214
* Object used by the Filestack client to process an
1315
* upload task.
1416
*/
1517
class UploadProcessor
1618
{
1719
use Mixins\CommonMixin;
20+
use LoggingTrait;
1821

1922
public $api_key;
2023
protected $security;
@@ -98,8 +101,15 @@ public function registerUploadTask($api_key, $metadata)
98101
$this->appendSecurity($data);
99102

100103
$url = $this->getCustomUrl(FilestackConfig::UPLOAD_URL) . '/multipart/start';
104+
105+
$this->log("Method: POST");
106+
$this->log("URL: " . $url);
107+
$this->log("Input Data: " . json_encode($data, JSON_PRETTY_PRINT));
108+
101109
$response = $this->sendRequest('POST', $url, ['multipart' => $data]);
110+
102111
$json = $this->handleResponseDecodeJson($response);
112+
$this->log("Output Data: " . json_encode($json, JSON_PRETTY_PRINT));
103113

104114
return $json;
105115
}
@@ -322,8 +332,16 @@ protected function processChunks($part, $chunks)
322332
$part['part_size'] += $current_chunk['size'];
323333

324334
$data = $this->buildChunkData($part, $current_chunk);
335+
336+
$this->log("Method: POST");
337+
$this->log("URL: " . $upload_url);
338+
$this->log("Input Data: " . json_encode($data, JSON_PRETTY_PRINT));
339+
325340
$response = $this->sendRequest('POST', $upload_url, ['multipart' => $data]);
326341

342+
$json = $this->handleResponseDecodeJson($response);
343+
$this->log("Output Data: " . json_encode($json, JSON_PRETTY_PRINT));
344+
327345
try {
328346
$json = $this->handleResponseDecodeJson($response);
329347
$url = $json['url'];
@@ -463,9 +481,17 @@ protected function registerComplete($api_key, $parts_etags, $upload_data,
463481
$this->appendSecurity($data);
464482

465483
$url = $this->getCustomUrl(FilestackConfig::UPLOAD_URL) . '/multipart/complete';
484+
485+
$this->log("Method: POST");
486+
$this->log("URL: " . $url);
487+
$this->log("Input Data: " . json_encode($data, JSON_PRETTY_PRINT));
488+
466489
$response = $this->sendRequest('POST', $url, ['multipart' => $data]);
467490
$status_code = $response->getStatusCode();
468491

492+
$json = $this->handleResponseDecodeJson($response);
493+
$this->log("Output Data: " . json_encode($json, JSON_PRETTY_PRINT));
494+
469495
$filelink = null;
470496
if ($status_code == 200) {
471497
$filelink = $this->handleResponseCreateFilelink($response);

filestack/mixins/LoggingTrait.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Filestack\Mixins;
4+
5+
trait LoggingTrait
6+
{
7+
public function log($message)
8+
{
9+
$timestamp = date('Y-m-d H:i:s');
10+
$formattedMessage = "[$timestamp] [LOG]: $message" . PHP_EOL;
11+
$filePath = './access.log';
12+
13+
if (!file_exists($filePath)) {
14+
$file = fopen($filePath, 'w');
15+
16+
if ($file === false) {
17+
throw new \RuntimeException('Unable to create file: ' . $filePath);
18+
}
19+
20+
fclose($file);
21+
}
22+
23+
file_put_contents($filePath, $formattedMessage, FILE_APPEND);
24+
}
25+
}
26+

0 commit comments

Comments
 (0)