Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $nstack = new \NStack\NStack($config);
[x] Content Localize resources
[x] Content Localize languages
[x] Content Localize proposals
[ ] Content Files
[x] Content Files
[ ] Content Collections
[ ] Notify updates
[ ] UGC pushlogs
Expand Down
88 changes: 88 additions & 0 deletions src/Clients/FilesClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace NStack\Clients;

use DateTime;
use NStack\Exceptions\FailedToParseException;
use NStack\Models\File;

/**
* Class FilesClient
*
* @package NStack\Clients
* @author Tiago Araujo <tiar@nodesagency.com>
*/
class FilesClient extends NStackClient
{
/** @var string */
protected $path = 'content/files';

/**
* store
*
* @param String $name
* @param array $tagsArray
* @param DateTime $goneAt
* @param String $privacy
* @param String $imagePath
* @return File
* @throws FailedToParseException
*/
public function store(String $name, String $privacy, String $imagePath, array $tagsArray = null, DateTime $goneAt = null)
{
$response = $this->client->post($this->buildPath($this->path), [
'form_params' => [
'name' => $name,
'tags' => is_null($tagsArray) ? null : implode(",", $tagsArray),
'gone_at' => $goneAt,
'privacy' => $privacy,
'file' => fopen($imagePath, 'r'),
]
]);
$contents = $response->getBody()->getContents();
$data = json_decode($contents, true);

return new File($data['data']);
}

/**
* storeWithPath
*
* @param String $name
* @param String $privacy
* @param String $path
* @param String $mime
* @param int $size
* @param array|null $tagsArray
* @param DateTime|null $goneAt
* @return File
* @throws FailedToParseException
*/
public function storeWithPath(
String $name,
String $privacy,
String $path,
String $mime,
int $size,
array $tagsArray = null,
DateTime $goneAt = null
)
{
$response = $this->client->post($this->buildPath($this->path . '/path'), [
'form_params' => [
'name' => $name,
'tags' => is_null($tagsArray) ? null : implode(",", $tagsArray),
'gone_at' => $goneAt,
'privacy' => $privacy,
'path' => $path,
'mime' => $mime,
'size' => $size,
]
]);
$contents = $response->getBody()->getContents();
$data = json_decode($contents, true);

return new File($data['data']);
}

}
93 changes: 93 additions & 0 deletions src/Models/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace NStack\Models;

/**
* Class File
*
* @package NStack\Models
* @author Tiago Araujo <tiar@nodesagency.com>
*/
class File extends Model
{
/** @var int */
protected $id;

/** @var string */
protected $name;

/** @var string */
protected $tags;

/** @var string */
protected $privacy;

/** @var string */
protected $goneAt;

/** @var string */
protected $mime;

/** @var int */
protected $size;

/** @var string */
protected $password;

/** @var string */
protected $url;

/** @var string */
protected $cdnUrl;

/** @var string */
protected $showUrl;

/** @var string */
protected $downloadUrl;

/**
* parse
*
* @param array $data
*/
public function parse(array $data)
{
$this->id = (int)$data['id'];
$this->name = (string)$data['name'];
$this->tags = (string)$data['tags'];
$this->privacy = (string)$data['privacy'];
$this->goneAt = (string)$data['gone_at'];
$this->size = (string)$data['size'];
$this->mime = (string)$data['mime'];
$this->password = (string)$data['password'];
$this->url = (string)$data['url'];
$this->cdnUrl = (string)$data['cdn_url'];
$this->showUrl = (string)$data['show_url'];
$this->downloadUrl = (string)$data['download_url'];
}

/**
* toArray
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'tags' => $this->tags,
'privacy' => $this->privacy,
'gone_at' => $this->goneAt,
'size' => $this->size,
'password' => $this->password,
'url' => $this->url,
'cdn_url' => $this->cdnUrl,
'show_url' => $this->showUrl,
'download_url' => $this->downloadUrl,
'mime' => $this->mime,
];
}

}
44 changes: 44 additions & 0 deletions tests/FilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace NStack\Tests;

use DateTime;
use NStack\Clients\FilesClient;
use NStack\Models\File;

class FilesTest extends TestCase
{
public function testStore()
{
$client = $this->getClientWithMockedPost('files-store.json');

$client = new FilesClient($this->getConfig(), $client);
$entry = $client->store(
'test',
'private-password',
__DIR__ . '/mocks/test.jpg',
['test', 'tag'],
new DateTime()
);

$this->assertInstanceOf(File::class, $entry);
}

public function testStoreWithPath()
{
$client = $this->getClientWithMockedPost('files-store-with-path.json');

$client = new FilesClient($this->getConfig(), $client);
$entry = $client->storeWithPath(
'test',
'private-password',
'appId',
'image/jpeg',
16980,
['test', 'tag'],
new DateTime()
);

$this->assertInstanceOf(File::class, $entry);
}
}
16 changes: 16 additions & 0 deletions tests/mocks/files-store-with-path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": {
"id": 629,
"name": "test",
"tags": "test,mytest",
"privacy": "private-password",
"gone_at": "2019-01-01 00:00:00",
"mime": "image/jpeg23",
"size": "16980",
"password": "seSo9TK9",
"url": "https://nstack-staging.vapor.cloud/files/629",
"cdn_url": null,
"show_url": "https://nstack-staging.vapor.cloud/files/629?show=1",
"download_url": "https://nstack-staging.vapor.cloud/files/629?download=1"
}
}
16 changes: 16 additions & 0 deletions tests/mocks/files-store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": {
"id": 625,
"name": "test",
"tags": "test,mytest",
"privacy": "private-password",
"gone_at": "2019-01-01 00:00:00",
"mime": "image/jpeg",
"size": 16980,
"password": "WZwZAztQ",
"url": "https://nstack-staging.vapor.cloud/files/625",
"cdn_url": null,
"show_url": "https://nstack-staging.vapor.cloud/files/625?show=1",
"download_url": "https://nstack-staging.vapor.cloud/files/625?download=1"
}
}
Binary file added tests/mocks/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.