-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for file operations #103
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
841b5b2
part
seba-aln 2f683af
File Sharing episode 3: Revenge of the file
seba-aln 607aa6a
Handle File Upload
seba-aln 839fd59
Add Tests to file sharing
seba-aln 922b768
Add missing test file
seba-aln 43c51a1
Post review fixes
seba-aln b41730f
PubNub SDK v6.3.0 release.
pubnub-release-bot a5a53fd
Update .pubnub.yml
techwritermat 46f93cc
Update CHANGELOG.md
techwritermat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
require_once(__DIR__ . '/../vendor/autoload.php'); | ||
|
||
use PubNub\PubNub; | ||
use PubNub\PNConfiguration; | ||
|
||
$channelName = "file-channel"; | ||
$fileName = "pn.gif"; | ||
|
||
$config = new PNConfiguration(); | ||
$config->setSubscribeKey(getenv('SUBSCRIBE_KEY', 'demo')); | ||
$config->setPublishKey(getenv('PUBLISH_KEY', 'demo')); | ||
$config->setUserId('example'); | ||
|
||
$pubnub = new PubNub($config); | ||
|
||
// Sending file | ||
// $fileHandle = fopen(__DIR__ . DIRECTORY_SEPARATOR . $fileName, "r"); | ||
// $sendFileResult = $pubnub->sendFile() | ||
// ->channel($channelName) | ||
// ->fileName($fileName) | ||
// ->message("Hello from PHP SDK") | ||
// ->fileHandle($fileHandle) | ||
// ->sync(); | ||
|
||
// fclose($fileHandle); | ||
|
||
// Listing files in the channel | ||
$channelFiles = $pubnub->listFiles()->channel($channelName)->sync(); | ||
if ($channelFiles->getCount() > 0) { | ||
print("There are {$channelFiles->getCount()} files in the channel {$channelName}\n"); | ||
foreach ($channelFiles->getFiles() as $idx => $file) { | ||
print("File[{$idx}]: {$file->getName()} with ID: {$file->getId()}," | ||
. "size {$file->getSize()}, created at: {$file->getCreationTime()}\n"); | ||
} | ||
} else { | ||
print("There are no files in the channel {$channelName}\n"); | ||
} | ||
|
||
$file = $channelFiles->getFiles()[0]; | ||
|
||
print('Getting download URL for the file...'); | ||
$downloadUrl = $pubnub->getFileDownloadUrl() | ||
->channel($channelName) | ||
->fileId($file->getId()) | ||
->fileName($file->getName()) | ||
->sync(); | ||
|
||
printf("To download the file use the following URL: %s\n", $downloadUrl->getFileUrl()); | ||
|
||
print("Downloading file..."); | ||
$downloadFile = $pubnub->downloadFile() | ||
->channel($channelName) | ||
->fileId($file->getId()) | ||
->fileName($file->getName()) | ||
->sync(); | ||
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . $file->getName(), $downloadFile->getFileContent()); | ||
print("done. File saved as: {$file->getName()}\n"); | ||
|
||
// deleting file | ||
$deleteFile = $pubnub->deleteFile() | ||
->channel($channelName) | ||
->fileId($file->getId()) | ||
->fileName($file->getName()) | ||
->sync(); | ||
|
||
if ($deleteFile->getStatus() === 200) { | ||
print("File deleted successfully\n"); | ||
} else { | ||
print("Failed to delete file\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace PubNub\Endpoints\FileSharing; | ||
|
||
use PubNub\Enums\PNHttpMethod; | ||
use PubNub\Enums\PNOperationType; | ||
use PubNub\Models\Consumer\FileSharing\PNDeleteFileResult; | ||
use PubNub\PubNubUtil; | ||
|
||
class DeleteFile extends FileSharingEndpoint | ||
{ | ||
protected const ENDPOINT_URL = "/v1/files/%s/channels/%s/files/%s/%s"; | ||
|
||
protected function createResponse($result) | ||
{ | ||
return new PNDeleteFileResult($result); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
protected function getOperationType() | ||
{ | ||
return PNOperationType::PNDeleteFileOperation; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
protected function isAuthRequired() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return null|string | ||
*/ | ||
protected function buildData() | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function buildPath() | ||
{ | ||
return sprintf( | ||
self::ENDPOINT_URL, | ||
$this->pubnub->getConfiguration()->getSubscribeKey(), | ||
PubNubUtil::urlEncode($this->channel), | ||
PubNubUtil::urlEncode($this->fileId), | ||
PubNubUtil::urlEncode($this->fileName) | ||
); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function customParams() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @return string PNHttpMethod | ||
*/ | ||
protected function httpMethod() | ||
{ | ||
return PNHttpMethod::DELETE; | ||
} | ||
|
||
public function name() | ||
{ | ||
return "Delete file"; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about introducing:
$fileCount = $channelFiles->getCount();
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would improve readability