Skip to content
Merged
Prev Previous commit
Next Next commit
cleanup server side stuff
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
Julien Veyssier committed Nov 2, 2021
commit d74c36c358277c7ec70147397ec1f806a5a08965
43 changes: 37 additions & 6 deletions lib/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function __construct(string $appName,

/**
* @NoAdminRequired
*
* @param int $textFileId
* @param string $link
* @return DataResponse
*/
public function insertImageLink(int $textFileId, string $link): DataResponse {
try {
Expand All @@ -73,10 +77,15 @@ public function insertImageLink(int $textFileId, string $link): DataResponse {
/**
* @NoAdminRequired
* @PublicPage
*
* @param int|null $textFileId can be null with public file share
* @param string $link
* @param string $shareToken
* @return DataResponse
*/
public function insertImageLinkPublic(?int $textFileId, string $link, string $token): DataResponse {
public function insertImageLinkPublic(?int $textFileId, string $link, string $shareToken): DataResponse {
try {
$downloadResult = $this->imageService->insertImageLinkPublic($textFileId, $link, $token);
$downloadResult = $this->imageService->insertImageLinkPublic($textFileId, $link, $shareToken);
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
Expand All @@ -89,6 +98,9 @@ public function insertImageLinkPublic(?int $textFileId, string $link, string $to

/**
* @NoAdminRequired
*
* @param int $textFileId
* @return DataResponse
*/
public function uploadImage(int $textFileId): DataResponse {
try {
Expand All @@ -109,14 +121,18 @@ public function uploadImage(int $textFileId): DataResponse {
/**
* @NoAdminRequired
* @PublicPage
*
* @param int|null $textFileId can be null with public file share
* @param string $shareToken
* @return DataResponse
*/
public function uploadImagePublic(?int $textFileId, string $token): DataResponse {
public function uploadImagePublic(?int $textFileId, string $shareToken): DataResponse {
try {
$file = $this->request->getUploadedFile('image');
if ($file !== null && isset($file['tmp_name'], $file['name'])) {
$newFileContent = file_get_contents($file['tmp_name']);
$newFileName = $file['name'];
$uploadResult = $this->imageService->uploadImagePublic($textFileId, $newFileName, $newFileContent, $token);
$uploadResult = $this->imageService->uploadImagePublic($textFileId, $newFileName, $newFileContent, $shareToken);
return new DataResponse($uploadResult);
} else {
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
Expand All @@ -129,6 +145,16 @@ public function uploadImagePublic(?int $textFileId, string $token): DataResponse
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Serve the images in the editor
* @param int $textFileId
* @param string $imageFileName
* @return DataDisplayResponse
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
* @throws \OC\User\NoUserException
*/
public function getImage(int $textFileId, string $imageFileName): DataDisplayResponse {
$imageContent = $this->imageService->getImage($textFileId, $imageFileName, $this->userId);
Expand All @@ -144,9 +170,14 @@ public function getImage(int $textFileId, string $imageFileName): DataDisplayRes
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*
* @param int $textFileId
* @param string $imageFileName
* @param string $shareToken
* @return DataDisplayResponse
*/
public function getImagePublic(int $textFileId, string $imageFileName, string $token): DataDisplayResponse {
$imageContent = $this->imageService->getImagePublic($textFileId, $imageFileName, $token);
public function getImagePublic(int $textFileId, string $imageFileName, string $shareToken): DataDisplayResponse {
$imageContent = $this->imageService->getImagePublic($textFileId, $imageFileName, $shareToken);
if ($imageContent !== null) {
return new DataDisplayResponse($imageContent);
} else {
Expand Down
127 changes: 107 additions & 20 deletions lib/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function __construct(IRootFolder $rootFolder,
}

/**
* Get image content from file name
*
* @param int $textFileId
* @param string $imageFileName
* @param string $userId
Expand All @@ -101,8 +103,18 @@ public function getImage(int $textFileId, string $imageFileName, string $userId)
return null;
}

public function getImagePublic(int $textFileId, string $imageFileName, string $token): ?string {
$textFile = $this->getTextFilePublic($textFileId, $token);
/**
* Get image content from file name in public context
*
* @param int $textFileId
* @param string $imageFileName
* @param string $shareToken
* @return string|null
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
*/
public function getImagePublic(int $textFileId, string $imageFileName, string $shareToken): ?string {
$textFile = $this->getTextFilePublic($textFileId, $shareToken);
$attachmentFolder = $this->getOrCreateAttachmentDirectoryForFile($textFile);
if ($attachmentFolder !== null) {
try {
Expand All @@ -118,6 +130,8 @@ public function getImagePublic(int $textFileId, string $imageFileName, string $t
}

/**
* Save an uploaded image in the attachment folder
*
* @param int $textFileId
* @param string $newFileName
* @param string $newFileContent
Expand Down Expand Up @@ -151,11 +165,23 @@ public function uploadImage(int $textFileId, string $newFileName, string $newFil
}
}

public function uploadImagePublic(?int $textFileId, string $newFileName, string $newFileContent, string $token): array {
if (!$this->hasUpdatePermissions($token)) {
/**
* Save an uploaded image in the attachment folder in a public context
*
* @param int|null $textFileId
* @param string $newFileName
* @param string $newFileContent
* @param string $shareToken
* @return array|string[]
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotPermittedException
*/
public function uploadImagePublic(?int $textFileId, string $newFileName, string $newFileContent, string $shareToken): array {
if (!$this->hasUpdatePermissions($shareToken)) {
throw new Exception('No write permissions');
}
$textFile = $this->getTextFilePublic($textFileId, $token);
$textFile = $this->getTextFilePublic($textFileId, $shareToken);
$saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
if ($saveDir !== null) {
$fileName = (string) time() . '-' . $newFileName;
Expand All @@ -175,14 +201,15 @@ public function uploadImagePublic(?int $textFileId, string $newFileName, string
}

/**
* @param string $textFilePath
* Download and save an image from a link in the attachment folder
*
* @param int $textFileId
* @param string $link
* @param string $userId
* @return array
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
* @throws \OC\User\NoUserException
*/
public function insertImageLink(int $textFileId, string $link, string $userId): array {
Expand All @@ -200,11 +227,20 @@ public function insertImageLink(int $textFileId, string $link, string $userId):
}
}

public function insertImageLinkPublic(?int $textFileId, string $link, string $token): array {
if (!$this->hasUpdatePermissions($token)) {
/**
* Download and save an image from a link in the attachment folder in a public context
*
* @param int|null $textFileId
* @param string $link
* @param string $shareToken
* @return array|string[]
* @throws Exception
*/
public function insertImageLinkPublic(?int $textFileId, string $link, string $shareToken): array {
if (!$this->hasUpdatePermissions($shareToken)) {
throw new Exception('No write permissions');
}
$textFile = $this->getTextFilePublic($textFileId, $token);
$textFile = $this->getTextFilePublic($textFileId, $shareToken);
$saveDir = $this->getOrCreateAttachmentDirectoryForFile($textFile);
if ($saveDir !== null) {
return $this->downloadLink($saveDir, $link, $textFile);
Expand All @@ -215,15 +251,33 @@ public function insertImageLinkPublic(?int $textFileId, string $link, string $to
}
}

private function hasUpdatePermissions(string $token): bool {
/**
* Check if the shared access has write permissions
*
* @param string $shareToken
* @return bool
*/
private function hasUpdatePermissions(string $shareToken): bool {
try {
$share = $this->shareManager->getShareByToken($token);
$share = $this->shareManager->getShareByToken($shareToken);
return ($share->getShareType() === IShare::TYPE_LINK && $share->getPermissions() & Constants::PERMISSION_UPDATE);
} catch (ShareNotFound $e) {
return false;
}
}

/**
* Download an image from a link and place it in a given folder
*
* @param Folder $saveDir
* @param string $link
* @param File $textFile
* @return array|string[]
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
*/
private function downloadLink(Folder $saveDir, string $link, File $textFile): array {
$fileName = (string) time();
$savedFile = $saveDir->newFile($fileName);
Expand Down Expand Up @@ -258,11 +312,19 @@ private function downloadLink(Folder $saveDir, string $link, File $textFile): ar
}
}

/**
* Get or create the user-specific attachment folder
*
* @param string $userId
* @return Folder|null
* @throws NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
private function getOrCreateTextDirectory(string $userId): ?Folder {
$userFolder = $this->rootFolder->getUserFolder($userId);
if ($userFolder->nodeExists('/Text')) {
$node = $userFolder->get('Text');
//if ($node->getType() === FileInfo::TYPE_FOLDER) {
if ($node instanceof Folder) {
return $node;
} else {
Expand All @@ -273,6 +335,16 @@ private function getOrCreateTextDirectory(string $userId): ?Folder {
}
}

/**
* Get or create file-specific attachment folder
*
* @param File $textFile
* @return Folder|null
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotPermittedException
* @throws \OC\User\NoUserException
*/
private function getOrCreateAttachmentDirectoryForFile(File $textFile): ?Folder {
$owner = $textFile->getOwner();
$ownerId = $owner->getUID();
Expand All @@ -295,6 +367,8 @@ private function getOrCreateAttachmentDirectoryForFile(File $textFile): ?Folder
}

/**
* Get a user file from file ID
*
* @param int $textFileId
* @param string $userId
* @return Folder|null
Expand All @@ -308,31 +382,35 @@ private function getTextFile(int $textFileId, string $userId): ?File {
$textFile = $userFolder->getById($textFileId);
if (count($textFile) > 0 && $textFile[0] instanceof File) {
return $textFile[0];
//return $this->getOrCreateAttachmentDirectoryForFile($textFile);
}
return null;
}

private function getTextFilePublic(?int $textFileId, string $token): ?File {
// $textFile = $this->rootFolder->getById($textFileId);
/**
* Get file from share token
*
* @param int|null $textFileId
* @param string $shareToken
* @return File|null
* @throws NotFoundException
*/
private function getTextFilePublic(?int $textFileId, string $shareToken): ?File {
// is the file shared with this token?
try {
$share = $this->shareManager->getShareByToken($token);
$share = $this->shareManager->getShareByToken($shareToken);
if ($share->getShareType() === IShare::TYPE_LINK) {
// shared file or folder?
if ($share->getNodeType() === 'file') {
$textFile = $share->getNode();
if ($textFile instanceof File) {
return $textFile;
//return $this->getOrCreateAttachmentDirectoryForFile($textFile);
}
} elseif ($share->getNodeType() === 'folder' && $textFileId !== null) {
$folder = $share->getNode();
if ($folder instanceof Folder) {
$textFile = $folder->getById($textFileId);
if (count($textFile) > 0 && $textFile[0] instanceof File) {
return $textFile[0];
//return $this->getOrCreateAttachmentDirectoryForFile($textFile);
}
}
}
Expand All @@ -343,6 +421,15 @@ private function getTextFilePublic(?int $textFileId, string $token): ?File {
return null;
}

/**
* Download a file and write it to a resource
*
* @param string $url
* @param $resource
* @param array $params
* @param string $method
* @return array|string[]
*/
private function simpleDownload(string $url, $resource, array $params = [], string $method = 'GET'): array {
$client = $this->clientService->newClient();
try {
Expand Down
4 changes: 2 additions & 2 deletions src/components/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export default {
formData.append('image', image)
formData.append('textFileId', this.fileId)
if (this.isPublic) {
formData.append('token', this.token)
formData.append('shareToken', this.token)
}
const url = this.isPublic
? generateUrl('/apps/text/public/image/upload')
Expand Down Expand Up @@ -376,7 +376,7 @@ export default {
link: this.imageLink,
}
if (this.isPublic) {
params.token = this.token
params.shareToken = this.token
}
const url = this.isPublic
? generateUrl('/apps/text/public/image/link')
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ImageView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default {
imageFileName,
})
} else {
return generateUrl('/apps/text/public/image?textFileId={textFileId}&imageFileName={imageFileName}&token={token}',
return generateUrl('/apps/text/public/image?textFileId={textFileId}&imageFileName={imageFileName}&shareToken={token}',
{
textFileId,
imageFileName,
Expand Down