Skip to content

Commit

Permalink
fix: update metadata endpoint response and add remote subfolder to pa…
Browse files Browse the repository at this point in the history
…rams
  • Loading branch information
FlorianRuen committed Jul 5, 2024
1 parent 39e9efe commit b47c279
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
8 changes: 6 additions & 2 deletions lib/Controller/ExternalStorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ public function getMetadataForSpecificFile(int $fileId): DataResponse {
$fileMetadata = $this->externalStorageService->getMetadataForSpecificFile($user, $fileId);

if (!isset($fileMetadata['error'])) {
return new DataResponse(['success' => true, 'metadata' => $fileMetadata['metadata']], Http::STATUS_OK);
if ($fileMetadata['result']['success']) {
return new DataResponse(['success' => true, 'metadata' => $fileMetadata['result']], Http::STATUS_OK);
} else {
return new DataResponse(['success' => false, 'error' => $fileMetadata['result']['error']], Http::STATUS_OK);
}
}

return new DataResponse([
'success' => false,
'error' => $fileMetadata['error'],
'errorMessage' => $fileMetadata['metadata']
'errorMessage' => $fileMetadata['result']
], Http::STATUS_INTERNAL_SERVER_ERROR);

} catch (Exception $e) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Service/BackendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public function __construct(IL10N $l, Password $legacyAuth) {
->setText($l->t('CIDgravity'))
->addParameters([
new DefinitionParameter('host', $l->t('URL')),
new DefinitionParameter('metadata_endpoint', $l->t('Metadata endpoint')),
(new DefinitionParameter('root', $l->t('Remote subfolder')))
->setFlag(DefinitionParameter::FLAG_OPTIONAL),
(new DefinitionParameter('secure', $l->t('Secure https://')))
->setType(DefinitionParameter::VALUE_BOOLEAN)
->setDefaultValue(true),
new DefinitionParameter('metadata_endpoint', $l->t('Metadata endpoint')),
(new DefinitionParameter('default_ipfs_gateway', $l->t('Default IPFS gateway')))
->setType(DefinitionParameter::VALUE_TEXT)
->setDefaultValue('https://ipfs.io/ipfs')
->setTooltip('You can also use your custom gateway or public gateway such as https://dweb.link'),
->setType(DefinitionParameter::VALUE_TEXT)
->setDefaultValue('https://ipfs.io/ipfs')
->setTooltip('You can also use your custom gateway or public gateway such as https://dweb.link'),
])
->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
->setLegacyAuthMechanism($legacyAuth);
Expand Down
18 changes: 13 additions & 5 deletions lib/Service/ExternalStorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function getMetadataForSpecificFile(IUser $nextcloudUser, int $fileId): a

if (!isset($externalStorageConfiguration['error'])) {
$requestBody = [
"fileOwner" => $nextcloudUser->getUID(),
"filePath" => $externalStorageConfiguration['filepath'],
"user" => $nextcloudUser->getUID(),
"filepath" => $externalStorageConfiguration['filepath'],
];

$response = $this->httpClient->post(
Expand All @@ -63,7 +63,7 @@ public function getMetadataForSpecificFile(IUser $nextcloudUser, int $fileId): a
$externalStorageConfiguration['password'],
);

return ['metadata' => $response];
return ['result' => $response];

} else {
return $externalStorageConfiguration;
Expand Down Expand Up @@ -125,8 +125,16 @@ private function buildExternalStorageConfiguration(string $fileInternalPath, Sto
$configuration['metadata_endpoint'] = $externalStorage->getBackendOption('metadata_endpoint');
$configuration['default_ipfs_gateway'] = $externalStorage->getBackendOption('default_ipfs_gateway');

// add file internal path (without the mount point folder, only the path after)
$configuration['filepath'] = $fileInternalPath;
// add filepath (with the mount point folder, must begin with a /)
// if the mountpoint is not empty, prepend a slash
$mountpoint = trim($externalStorage->getBackendOption('root'), '/');
$filename = ltrim($fileInternalPath, '/');

if ($mountpoint !== '') {
$configuration['filepath'] = '/' . $mountpoint . '/' . $filename;
} else {
$configuration['filepath'] = '/' . $filename;
}

// check if we need to include auth settings (for metadata call only, not exposed to frontend)
if ($includeAuthSettings) {
Expand Down
33 changes: 21 additions & 12 deletions src/views/GatewayTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<!-- Error loading storage type -->
<NcEmptyContent v-else-if="isErrorLocal"
class="emptyContent"
:name="t('cidgravitygateway', 'Something wrong while loading metadata')">
:name="t('cidgravitygateway', 'Something wrong while loading metadata')"
:description="errorMessage">
<template #icon>
<AlertCircleOutlineIcon />
</template>
Expand Down Expand Up @@ -138,7 +139,8 @@ export default {
externalStorageConfiguration: {},
ipfsGateway: {},
isErrorLocal: this.isError,
isCidgravityStorageLocal: this.isCidgravityStorage
isErrorMessageLocal: null,
isCidgravityStorageLocal: this.isCidgravityStorage,
}
},
Expand All @@ -159,6 +161,9 @@ export default {
return this.t('cidgravitygateway', 'Not on an CIDgravity storage')
},
errorMessage() {
return this.isErrorMessageLocal
},
getMetadataSectionTitle() {
if (this.fileInfo.type === 'dir') {
return this.t('cidgravitygateway', 'Directory metadata')
Expand All @@ -176,22 +181,22 @@ export default {
return this.t('cidgravitygateway', 'This {contentType} is not on an CIDgravity storage type. To display metadata, you must browse files on external storage', { contentType })
},
shortenedCid() {
if (this.fileMetadata.cid !== null && this.fileMetadata.cid !== '' && this.fileMetadata.cid !== undefined) {
if (this.fileMetadata.cid.length > 15) {
if (this.fileMetadata.result.file.cid !== null && this.fileMetadata.result.file.cid !== '' && this.fileMetadata.result.file.cid !== undefined) {
if (this.fileMetadata.result.file.cid.length > 15) {
return (
this.fileMetadata.cid.substring(0, 5)
this.fileMetadata.result.file.cid.substring(0, 5)
+ ' [...] '
+ this.fileMetadata.cid.substring(this.fileMetadata.cid.length - 5, this.fileMetadata.cid.length)
+ this.fileMetadata.result.file.cid.substring(this.fileMetadata.result.file.cid.length - 5, this.fileMetadata.result.file.cid.length)
)
} else {
return this.fileMetadata.cid
return this.fileMetadata.result.file.cid
}
} else {
return this.fileMetadata.cid
return this.fileMetadata.result.file.cid
}
},
ipfsPublicLink() {
return this.ipfsGateway.link + '/' + this.fileMetadata.cid
return this.ipfsGateway.link + '/' + this.fileMetadata.result.file.cid
},
isCustomIpfsGateway() {
return this.ipfsGateway.isCustom
Expand All @@ -215,7 +220,7 @@ export default {
},
async copyCid() {
try {
await navigator.clipboard.writeText(this.fileMetadata.cid)
await navigator.clipboard.writeText(this.fileMetadata.result.file.cid)
showSuccess(t('cidgravitygateway', 'CID copied'))
} catch (error) {
showError(t('cidgravitygateway', 'Unable to copy the CID'))
Expand All @@ -224,7 +229,7 @@ export default {
},
async copyIpfsPublicLink() {
try {
const publicLink = this.ipfsGateway.link + '/' + this.fileMetadata.cid
const publicLink = this.ipfsGateway.link + '/' + this.fileMetadata.result.file.cid
await navigator.clipboard.writeText(publicLink)
showSuccess(t('cidgravitygateway', 'Public link copied'))
} catch (error) {
Expand All @@ -238,8 +243,9 @@ export default {
setIsCidgravityStorage(isCidgravityStorage) {
this.isCidgravityStorageLocal = isCidgravityStorage
},
setIsError(isError) {
setIsError(isError, errorMessage) {
this.isErrorLocal = isError
this.isErrorMessageLocal = errorMessage
},
setExternalStorageConfiguration(config) {
this.externalStorageConfiguration = config
Expand Down Expand Up @@ -270,16 +276,19 @@ export default {
this.isCidgravityStorageLocal = true
this.isErrorLocal = false
this.loading = false
this.isErrorMessageLocal = null
} else {
console.error('unable to load file metadata')
this.fileMetadata = {}
this.loading = false
this.isErrorLocal = true
this.isErrorMessageLocal = res.data.metadata.error
}
}).catch((error) => {
console.error(error)
this.loading = false
this.isErrorLocal = true
this.isErrorMessageLocal = null
})
},
},
Expand Down

0 comments on commit b47c279

Please sign in to comment.