Skip to content

Commit

Permalink
Get proper url
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Jan 27, 2025
2 parents 0e2a61c + 30dcdfd commit e1cb264
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 23 deletions.
53 changes: 53 additions & 0 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
Expand All @@ -36,6 +37,7 @@
class FileService
{
const ROOT_FOLDER = 'Open Registers';
const APP_GROUP = 'openregister';

/**
* Constructor for FileService
Expand All @@ -54,6 +56,7 @@ public function __construct(
private readonly IConfig $config,
private readonly RegisterMapper $registerMapper,
private readonly SchemaMapper $schemaMapper,
private readonly IGroupManager $groupManager,
)
{
}
Expand Down Expand Up @@ -408,6 +411,36 @@ public function findShare(string $path, ?int $shareType = 3): ?IShare
return null;
}

/**
* Share a file or folder with a user group in Nextcloud.
*
* @param int $nodeId The file or folder to share.
* @param string $nodeType 'file' or 'folder', the type of node.
* @param string $target The target folder to share the node in.
* @param int $permissions Permissions the group members will have in the folder.
* @param string $groupId The id of the group to share the folder with.
*
* @return IShare The resulting share
* @throws Exception
*/
private function shareWithGroup(int $nodeId, string $nodeType, string $target, int $permissions, string $groupId): IShare
{
$share = $this->shareManager->newShare();
$share->setTarget(target: '/'. $target);
$share->setNodeId(fileId:$nodeId);
$share->setNodeType(type:$nodeType);

$share->setShareType(shareType: 1);
$share->setPermissions(permissions: $permissions);
$share->setSharedBy(sharedBy:$this->userSession->getUser()->getUID());
$share->setShareOwner(shareOwner:$this->userSession->getUser()->getUID());
$share->setShareTime(shareTime: new DateTime());
$share->setSharedWith(sharedWith: $groupId);
$share->setStatus(status: $share::STATUS_ACCEPTED);

return $this->shareManager->createShare($share);
}

/**
* Creates a IShare object using the $shareData array data.
*
Expand Down Expand Up @@ -503,6 +536,7 @@ public function createShareLink(string $path, ?int $shareType = 3, ?int $permiss
*/
public function createFolder(string $folderPath): bool
{

$folderPath = trim(string: $folderPath, characters: '/');

// Get the current user.
Expand All @@ -511,6 +545,25 @@ public function createFolder(string $folderPath): bool

// Check if folder exists and if not create it.
try {
// First, check if the root folder exists, and if not, create it and share it with the openregister group.
try {
$userFolder->get(self::ROOT_FOLDER);
} catch(NotFoundException $exception) {
$rootFolder = $userFolder->newFolder(self::ROOT_FOLDER);

if($this->groupManager->groupExists(self::APP_GROUP) === false) {
$this->groupManager->createGroup(self::APP_GROUP);
}

$this->shareWithGroup(
nodeId: $rootFolder->getId(),
nodeType: $rootFolder->getType() === 'file' ? $rootFolder->getType() : 'folder',
target: self::ROOT_FOLDER,
permissions: 31,
groupId: self::APP_GROUP
);
}

try {
$userFolder->get(path: $folderPath);
} catch (NotFoundException $e) {
Expand Down
11 changes: 0 additions & 11 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,17 +1146,6 @@ private function writeFile(string $fileContent, string $propertyName, ObjectEnti
return $file;
}

public function nodeCreatedEventFunction(NodeCreatedEvent $event): void
{
$node = $event->getNode();
var_dump($node->getMetadata());
if ($node instanceof \OC\Files\Node\File === true) {
$path = $node->getFileInfo()->getPath();
$metadata = $node->getFileInfo()->getMetadata();
var_dump($metadata);
}
}

/**
* @todo
*
Expand Down
2 changes: 1 addition & 1 deletion src/entities/object/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ObjectEntity implements TObject {

/**
* Validates the object against a schema
* @returns {SafeParseReturnType<TObject, unknown>} Object containing validation result with success/error status
* @return {SafeParseReturnType<TObject, unknown>} Object containing validation result with success/error status
*/
public validate(): SafeParseReturnType<TObject, unknown> {
const schema = z.object({
Expand Down
6 changes: 3 additions & 3 deletions src/modals/object/LockObject.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { objectStore, navigationStore } from '../../store/store.js'
</template>
{{ success ? 'Close' : 'Cancel' }}
</NcButton>
<NcButton v-if="success === null"
:disabled="loading"
<NcButton
:disabled="loading || success"
type="primary"
@click="lockObject()">
<template #icon>
Expand Down Expand Up @@ -103,9 +103,9 @@ export default {
this.duration || undefined,
)
this.success = true
this.error = null
this.closeModalTimeout = setTimeout(this.closeModal, 2000)
} catch (error) {
this.success = false
this.error = error.message || 'Failed to lock object'
} finally {
this.loading = false
Expand Down
2 changes: 1 addition & 1 deletion src/store/modules/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const useObjectStore = defineStore('object', {
// FILES
/**
* Get files for an object
*
*
* @param {number} id Object ID
* @return {Promise} Promise that resolves with the object's files
*/
Expand Down
10 changes: 5 additions & 5 deletions src/views/object/ObjectDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { objectStore, navigationStore } from '../../store/store.js'
</template>
Delete
</NcActionButton>
<NcActionButton
<NcActionButton
:disabled="!objectStore.objectItem.folder"
@click="openFolder(objectStore.objectItem.folder)">
<template #icon>
Expand Down Expand Up @@ -314,18 +314,18 @@ export default {
},
/**
* Opens a file in the Nextcloud Files app
* @param {Object} file - The file object containing id, path, and other metadata
* @param {object} file - The file object containing id, path, and other metadata
*/
openFile(file) {
// Extract the directory path without the filename
const dirPath = file.path.substring(0, file.path.lastIndexOf('/'))
// Remove the '/admin/files/' prefix if it exists
const cleanPath = dirPath.replace(/^\/admin\/files\//, '/')
// Construct the proper Nextcloud Files app URL with file ID and openfile parameter
const filesAppUrl = `/index.php/apps/files/files/${file.id}?dir=${encodeURIComponent(cleanPath)}&openfile=true`
// Open URL in new tab
window.open(filesAppUrl, '_blank')
},
Expand Down
3 changes: 1 addition & 2 deletions src/views/object/ObjectsList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ import TrashCanOutline from 'vue-material-design-icons/TrashCanOutline.vue'
import Upload from 'vue-material-design-icons/Upload.vue'
import LockOutline from 'vue-material-design-icons/LockOutline.vue'
import LockOpenOutline from 'vue-material-design-icons/LockOpenOutline.vue'
import { showSuccess, showError } from '@nextcloud/dialogs'
export default {
name: 'ObjectsList',
Expand All @@ -128,7 +127,7 @@ export default {
},
mounted() {
objectStore.refreshObjectList()
}
},
}
</script>

Expand Down

0 comments on commit e1cb264

Please sign in to comment.