Skip to content

Commit

Permalink
Spliting functions for getting files
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Jan 27, 2025
1 parent 41d7db6 commit 0e2a61c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
74 changes: 48 additions & 26 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,48 +1312,55 @@ private function handleFileProperty(ObjectEntity $objectEntity, array $object, s
/**
* Get files for object
*
* See https://nextcloud-server.netlify.app/classes/ocp-files-file for the Nextcloud documentation on the File class
* See https://nextcloud-server.netlify.app/classes/ocp-files-node for the Nextcloud documentation on the Node superclass
*
* @param ObjectEntity $object The object to fetch files for.
* @return Node[] The files found.
* @throws \OCP\Files\NotFoundException
* See https://nextcloud-server.netlify.app/classes/ocp-files-file for the Nextcloud documentation on the File class
* See https://nextcloud-server.netlify.app/classes/ocp-files-node for the Nextcloud documentation on the Node superclass
*
* @param ObjectEntity|string $object The object or object ID to fetch files for
* @return Node[] The files found
* @throws \OCP\Files\NotFoundException If the folder is not found
* @throws DoesNotExistException If the object ID is not found
*/
public function getFiles(ObjectEntity $object): array
{
$folder = $this->fileService->getObjectFolder(objectEntity: $object, register: $object->getRegister(), schema: $object->getSchema());
public function getFiles(ObjectEntity|string $object): array
{
// If string ID provided, try to find the object entity
if (is_string($object)) {
$object = $this->objectEntityMapper->find($object);
}

if($folder instanceof Folder === true) {
$files = $folder->getDirectoryListing();
}
$folder = $this->fileService->getObjectFolder(
objectEntity: $object,
register: $object->getRegister(),
schema: $object->getSchema()
);

if ($folder instanceof Folder === true) {
$files = $folder->getDirectoryListing();
}

return $files;
}
}

/**
* Hydrate files array with metadata.
* Formats an array of Node files into an array of metadata arrays.
*
* See https://nextcloud-server.netlify.app/classes/ocp-files-file for the Nextcloud documentation on the File class
* See https://nextcloud-server.netlify.app/classes/ocp-files-node for the Nextcloud documentation on the Node superclass
*
* @param ObjectEntity $object The object to hydrate the files array of.
* @param Node[] $files The files to hydrate the files array with.
*
* @return ObjectEntity The object with hydrated files array.
* See https://nextcloud-server.netlify.app/classes/ocp-files-file for the Nextcloud documentation on the File class
* See https://nextcloud-server.netlify.app/classes/ocp-files-node for the Nextcloud documentation on the Node superclass
*
* @param Node[] $files Array of Node files to format
* @return array Array of formatted file metadata arrays
*/
public function hydrateFiles(ObjectEntity $object, array $files): ObjectEntity
public function formatFiles(array $files): array
{
$formattedFiles = [];

foreach($files as $file) {

// IShare documentation see https://nextcloud-server.netlify.app/classes/ocp-share-ishare
// IShare documentation see https://nextcloud-server.netlify.app/classes/ocp-share-ishare
$shares = $this->fileService->findShares($file);

$formattedFile = [
'id' => $file->getId(),
'path' => $file->getPath(),
'title' => $file->getName(),
'title' => $file->getName(),
'accessUrl' => count($shares) > 0 ? $this->fileService->getShareLink($shares[0]) : null,
'downloadUrl' => count($shares) > 0 ? $this->fileService->getShareLink($shares[0]).'/download' : null,
'type' => $file->getMimetype(),
Expand All @@ -1367,8 +1374,23 @@ public function hydrateFiles(ObjectEntity $object, array $files): ObjectEntity
$formattedFiles[] = $formattedFile;
}

$object->setFiles($formattedFiles);
return $formattedFiles;
}

/**
* Hydrate files array with metadata.
*
* See https://nextcloud-server.netlify.app/classes/ocp-files-file for the Nextcloud documentation on the File class
* See https://nextcloud-server.netlify.app/classes/ocp-files-node for the Nextcloud documentation on the Node superclass
*
* @param ObjectEntity $object The object to hydrate the files array of.
* @param Node[] $files The files to hydrate the files array with.
* @return ObjectEntity The object with hydrated files array.
*/
public function hydrateFiles(ObjectEntity $object, array $files): ObjectEntity
{
$formattedFiles = $this->formatFiles($files);
$object->setFiles($formattedFiles);
return $object;
}

Expand Down
23 changes: 19 additions & 4 deletions src/views/object/ObjectDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ import { objectStore, navigationStore } from '../../store/store.js'
</div>
</BTab>
<BTab title="Files">
<NcButton @click="openFolder(objectStore.objectItem.folder)">
<template #icon>
<FolderOutline :size="20" />
</template>
Open folder
</NcButton>
<div v-if="objectStore.files.length">
<NcListItem v-for="(file, key) in objectStore.files"
:key="key"
Expand Down Expand Up @@ -210,6 +216,7 @@ import {
NcActionButton,
NcListItem,
NcNoteCard,
NcButton,
} from '@nextcloud/vue'
import { BTabs, BTab } from 'bootstrap-vue'
Expand All @@ -231,6 +238,7 @@ export default {
NcActionButton,
NcListItem,
NcNoteCard,
NcButton,
BTabs,
BTab,
DotsHorizontal,
Expand Down Expand Up @@ -287,15 +295,22 @@ export default {
})
},
/**
* Opens the folder URL in a new tab after parsing the encoded URL
* @param {string} url - The encoded folder URL to open
* Opens the folder URL in a new tab after parsing the encoded URL and converting to Nextcloud format
* @param {string} url - The encoded folder URL to open (e.g. "Open Registers\/Publicatie Register\/Publicatie\/123")
*/
openFolder(url) {
// Parse the encoded URL by replacing escaped characters
const decodedUrl = url.replace(/\\\//g, '/')
const decodedUrl = url.replace(/\\\//g, '/')
// Ensure URL starts with forward slash
const normalizedUrl = decodedUrl.startsWith('/') ? decodedUrl : '/' + decodedUrl
// Construct the proper Nextcloud Files app URL with the normalized path
// Use window.location.origin to get the current domain instead of hardcoding
const nextcloudUrl = `${window.location.origin}/index.php/apps/files/files?dir=${encodeURIComponent(normalizedUrl)}`
// Open URL in new tab
window.open(decodedUrl, '_blank')
window.open(nextcloudUrl, '_blank')
},
/**
* Opens a file in the Nextcloud Files app
Expand Down

0 comments on commit 0e2a61c

Please sign in to comment.