-
Notifications
You must be signed in to change notification settings - Fork 1
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
feature/DIMOC-208/fileupload-fe #119
Merged
Merged
Changes from 24 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c91e10d
Added uploadFile and deleteFile functions to FileService (w.i.p.)
WilcoLouwerse 0402ab7
Some changes to fix create and to get update & delete working for files
WilcoLouwerse 9185381
WIP
remko48 98d2d45
Merge remote-tracking branch 'origin/development' into feature/DIMOC-…
remko48 085107e
lint fix
remko48 c7705e4
Update src from remark-lint
actions-user 752ed40
Update src from PHP Codesniffer
actions-user 8ab675d
Merge branch 'development' into feature/DIMOC-208/fileupload-fe
remko48 4258282
Added file bijlagen
remko48 6568aac
removed unused code
remko48 1b18039
Merge remote-tracking branch 'origin/development' into feature/DIMOC-…
remko48 f099f35
Merge branch 'feature/DIMOC-48/update-directory-service' into feature…
rjzondervan 631d158
disabled buttons and removed console errors
remko48 d9cd2d2
Merge branch 'feature/DIMOC-208/fileupload-fe' of https://github.com/…
remko48 63c86ed
Merge remote-tracking branch 'origin/development' into feature/DIMOC-…
remko48 d614e67
Merge remote-tracking branch 'origin/development' into feature/DIMOC-…
remko48 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 |
---|---|---|
|
@@ -121,3 +121,4 @@ | |
.errorMessage { | ||
color: var(--color-error); | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -51,11 +51,29 @@ private function getCurrentDomain(): string | |
return $protocol . $host; | ||
} | ||
|
||
/** | ||
* Gets and returns an array with information about the current user. | ||
* TODO: Username and password used for auth are currently set in config, this should (/could) be dynamic. | ||
* | ||
* @return array An array containing 'username', 'password' for auth and the 'currentUsername'. | ||
*/ | ||
private function getUserInfo(): array | ||
{ | ||
// Get the current user | ||
$currentUser = $this->userSession->getUser(); | ||
|
||
return [ | ||
'username' => $this->config->getValueString(app: $this->appName, key: 'adminUsername', default: 'admin'), | ||
'password' => $this->config->getValueString(app: $this->appName, key: 'adminPassword', default: 'admin'), | ||
'currentUsername' => $currentUser ? $currentUser->getUID() : 'Guest' | ||
]; | ||
} | ||
|
||
/** | ||
* Creates and returns a share link for a file (or folder). | ||
* (https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-share-api.html#create-a-new-share) | ||
* | ||
* @param string $path Path to the file/folder which should be shared. | ||
* @param string $path Path (from root) to the file/folder which should be shared. | ||
* @param int|null $shareType 0 = user; 1 = group; 3 = public link; 4 = email; 6 = federated cloud share; 7 = circle; 10 = Talk conversation | ||
* @param int|null $permissions 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1) | ||
* | ||
|
@@ -67,17 +85,12 @@ public function createShareLink(string $path, ?int $shareType = 3, ?int $permiss | |
// API endpoint to create a share | ||
$url = "{$this->getCurrentDomain()}/ocs/v2.php/apps/files_sharing/api/v1/shares"; | ||
|
||
// Get the admin username & password for auth | ||
$username = $this->config->getValueString(app: $this->appName, key: 'adminUsername', default: 'admin'); | ||
$password = $this->config->getValueString(app: $this->appName, key: 'adminPassword', default: 'admin'); | ||
|
||
// Get the current username | ||
$currentUser = $this->userSession->getUser(); | ||
$currentUsername = $currentUser ? $currentUser->getUID() : 'Guest'; | ||
// Get the admin username & password for auth & get the current username | ||
$userInfo = $this->getUserInfo(); | ||
|
||
// Data for the POST request | ||
$options = [ | ||
'auth' => [$username, $password], | ||
'auth' => [$userInfo['username'], $userInfo['password']], | ||
'headers' => [ | ||
'OCS-APIREQUEST' => 'true', | ||
'Content-Type' => 'application/x-www-form-urlencoded' | ||
|
@@ -86,7 +99,7 @@ public function createShareLink(string $path, ?int $shareType = 3, ?int $permiss | |
'path' => $path, | ||
'shareType' => $shareType, | ||
'permissions' => $permissions, | ||
'shareWith' => $currentUsername | ||
'shareWith' => $userInfo['currentUsername'] | ||
] | ||
]; | ||
|
||
|
@@ -100,4 +113,74 @@ public function createShareLink(string $path, ?int $shareType = 3, ?int $permiss | |
} | ||
} | ||
|
||
/** | ||
* Uploads a file to nextCloud. Will overwrite a file if it already exists and create a new one if it doesn't exist. | ||
* | ||
* @param mixed $content The content of the file. | ||
* @param string|null $filePath Path (from root) where to save the file. NOTE: this should include the name and extension/format of the file as well! (example.pdf) | ||
* @param bool|null $update If set to true, the response status code 204 will also be seen as a success result. (NextCloud will return 204 when successfully updating a file) | ||
* | ||
* @return bool True if successful. | ||
* @throws GuzzleException In case the Guzzle call returns an exception. | ||
*/ | ||
public function uploadFile(mixed $content, ?string $filePath = '', ?bool $update = false): bool | ||
{ | ||
// Get the admin username & password for auth & get the current username | ||
$userInfo = $this->getUserInfo(); | ||
|
||
// API endpoint to upload the file | ||
$url = $this->getCurrentDomain() . '/remote.php/dav/files/' | ||
. $userInfo['currentUsername'] . '/' . ltrim(string: $filePath, characters: '/'); | ||
|
||
try { | ||
$response = $this->client->request('PUT', $url, [ | ||
'auth' => [$userInfo['username'], $userInfo['password']], | ||
'body' => $content | ||
]); | ||
|
||
if ($response->getStatusCode() === 201 || ($update === true && $response->getStatusCode() === 204)) { | ||
return true; | ||
} | ||
} catch (\Exception $e) { | ||
$str = $update === true ? 'update' : 'upload'; | ||
$this->logger->error("File $str failed: " . $e->getMessage()); | ||
throw $e; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Deletes a file from nextCloud. | ||
* | ||
* @param string $filePath Path (from root) to the file you want to delete. | ||
* | ||
* @return bool True if successful. | ||
* @throws GuzzleException|Exception In case the Guzzle call returns an exception. | ||
*/ | ||
public function deleteFile(string $filePath): bool | ||
{ | ||
// Get the admin username & password for auth & get the current username | ||
$userInfo = $this->getUserInfo(); | ||
|
||
// API endpoint to upload the file | ||
$url = $this->getCurrentDomain() . '/remote.php/dav/files/' | ||
. $userInfo['currentUsername'] . '/' . ltrim(string: $filePath, characters: '/'); | ||
|
||
try { | ||
$response = $this->client->request('DELETE', $url, [ | ||
'auth' => [$userInfo['username'], $userInfo['password']], | ||
]); | ||
|
||
if ($response->getStatusCode() === 204) { | ||
return true; | ||
} | ||
} catch (\Exception $e) { | ||
$this->logger->error('File deletion failed: ' . $e->getMessage()); | ||
throw $e; | ||
} | ||
|
||
return false; | ||
} | ||
Comment on lines
+161
to
+184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem, waarom buitenom als er een interne route voor is? |
||
|
||
} |
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.
Waarom buitenom via de API als er een interne route voor bestaat?