Skip to content

Commit

Permalink
Added Log For FS-11830 (#67)
Browse files Browse the repository at this point in the history
* Update UploadProcessor.php

* Add files via upload
  • Loading branch information
shanmugam001 authored Sep 11, 2024
1 parent 23230fd commit b550c0d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions filestack/UploadProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

use Filestack\Mixins\LoggingTrait;

/**
* Object used by the Filestack client to process an
* upload task.
*/
class UploadProcessor
{
use Mixins\CommonMixin;
use LoggingTrait;

public $api_key;
protected $security;
Expand Down Expand Up @@ -98,8 +101,15 @@ public function registerUploadTask($api_key, $metadata)
$this->appendSecurity($data);

$url = $this->getCustomUrl(FilestackConfig::UPLOAD_URL) . '/multipart/start';

$this->log("Method: POST");
$this->log("URL: " . $url);
$this->log("Input Data: " . json_encode($data, JSON_PRETTY_PRINT));

$response = $this->sendRequest('POST', $url, ['multipart' => $data]);

$json = $this->handleResponseDecodeJson($response);
$this->log("Output Data: " . json_encode($json, JSON_PRETTY_PRINT));

return $json;
}
Expand Down Expand Up @@ -322,8 +332,16 @@ protected function processChunks($part, $chunks)
$part['part_size'] += $current_chunk['size'];

$data = $this->buildChunkData($part, $current_chunk);

$this->log("Method: POST");
$this->log("URL: " . $upload_url);
$this->log("Input Data: " . json_encode($data, JSON_PRETTY_PRINT));

$response = $this->sendRequest('POST', $upload_url, ['multipart' => $data]);

$json = $this->handleResponseDecodeJson($response);
$this->log("Output Data: " . json_encode($json, JSON_PRETTY_PRINT));

try {
$json = $this->handleResponseDecodeJson($response);
$url = $json['url'];
Expand Down Expand Up @@ -463,9 +481,17 @@ protected function registerComplete($api_key, $parts_etags, $upload_data,
$this->appendSecurity($data);

$url = $this->getCustomUrl(FilestackConfig::UPLOAD_URL) . '/multipart/complete';

$this->log("Method: POST");
$this->log("URL: " . $url);
$this->log("Input Data: " . json_encode($data, JSON_PRETTY_PRINT));

$response = $this->sendRequest('POST', $url, ['multipart' => $data]);
$status_code = $response->getStatusCode();

$json = $this->handleResponseDecodeJson($response);
$this->log("Output Data: " . json_encode($json, JSON_PRETTY_PRINT));

$filelink = null;
if ($status_code == 200) {
$filelink = $this->handleResponseCreateFilelink($response);
Expand Down
26 changes: 26 additions & 0 deletions filestack/mixins/LoggingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Filestack\Mixins;

trait LoggingTrait
{
public function log($message)
{
$timestamp = date('Y-m-d H:i:s');
$formattedMessage = "[$timestamp] [LOG]: $message" . PHP_EOL;
$filePath = './access.log';

if (!file_exists($filePath)) {
$file = fopen($filePath, 'w');

if ($file === false) {
throw new \RuntimeException('Unable to create file: ' . $filePath);
}

fclose($file);
}

file_put_contents($filePath, $formattedMessage, FILE_APPEND);
}
}

0 comments on commit b550c0d

Please sign in to comment.