Skip to content

Commit

Permalink
feat(pci-object-storage): download object implementation
Browse files Browse the repository at this point in the history
ref: DTCORE-2889
Signed-off-by: LIDRISSI Hamid <[email protected]>
  • Loading branch information
seven-amid committed Dec 13, 2024
1 parent 2d3fb76 commit a8f44e2
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/manager/apps/pci-object-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"react-i18next": "^14.0.5",
"react-router-dom": "^6.24.1",
"react-use": "^17.5.0",
"zustand": "^4.5.5"
"zustand": "^4.5.5",
"date-fns": "^3.6.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Objektcontainer erstellen",
"pci_projects_project_storages_containers_my_containers_label": "Meine Container",
"pci_projects_project_storages_containers_s3_users_label": "S3-Nutzer",
"pci_projects_project_storages_containers_container_object_error_download": "Beim Download des Objekts ist ein Fehler aufgetreten: {{ message }}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Hosten Sie Ihren Datenkatalog mit unserer Object-Storage-Lösung. Verwalten Sie diese Daten mit Ihrer Anwendung und nutzen Sie dafür S3-APIs."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Create an object container ",
"pci_projects_project_storages_containers_my_containers_label": "My containers",
"pci_projects_project_storages_containers_s3_users_label": "S3 users",
"pci_projects_project_storages_containers_container_object_error_download": "An error has occurred downloading the object: {{message}}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Host your data catalogue in our Object Storage solution. You can manage this data from your application using the S3 APIs."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Crear un contenedor de objetos",
"pci_projects_project_storages_containers_my_containers_label": "Mis contenedores",
"pci_projects_project_storages_containers_s3_users_label": "Usuarios S3",
"pci_projects_project_storages_containers_container_object_error_download": "Se ha producido un error al descargar el objeto: {{ message }}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Aloje su catálogo de datos en nuestra solución Object Storage. Puede administrar estos datos desde su aplicación gracias a las API S3."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Créer un conteneur d'objets",
"pci_projects_project_storages_containers_my_containers_label": "Mes conteneurs",
"pci_projects_project_storages_containers_s3_users_label": "Utilisateurs S3",

"pci_projects_project_storages_containers_container_object_error_download": "Une erreur est survenue lors du téléchargement de l'objet : {{ message }}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Hébergez votre catalogue de données dans notre solution Object Storage. Vous pouvez administrer ces données depuis votre application grâce aux API S3."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Créer un conteneur d'objets",
"pci_projects_project_storages_containers_my_containers_label": "Mes conteneurs",
"pci_projects_project_storages_containers_s3_users_label": "Utilisateurs S3",

"pci_projects_project_storages_containers_container_object_error_download": "Une erreur est survenue lors du téléchargement de l'objet : {{ message }}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Hébergez votre catalogue de données dans notre solution Object Storage. Vous pouvez administrer ces données depuis votre application grâce aux API S3."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Crea un container di oggetti",
"pci_projects_project_storages_containers_my_containers_label": "I tuoi container",
"pci_projects_project_storages_containers_s3_users_label": "Utenti S3",
"pci_projects_project_storages_containers_container_object_error_download": "Si è verificato un errore durante il download dell'oggetto: {{message}}",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Ospita il tuo catalogo di dati nella nostra soluzione Object Storage. Puoi gestire questi dati dalla tua applicazione con le API S3."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Utwórz kontener obiektów",
"pci_projects_project_storages_containers_my_containers_label": "Twoje kontenery",
"pci_projects_project_storages_containers_s3_users_label": "Użytkownicy S3",
"pci_projects_project_storages_containers_container_object_error_download": "Wystąpił błąd podczas pobierania obiektu: {{message}}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Hostuj katalog danych w naszym rozwiązaniu Object Storage. Danymi możesz zarządzać z poziomu swojej aplikacji za pomocą API S3."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"pci_projects_project_storages_blocks_add_object_label": "Criar um container de objetos",
"pci_projects_project_storages_containers_my_containers_label": "Os meus containers",
"pci_projects_project_storages_containers_s3_users_label": "Utilizadores S3",
"pci_projects_project_storages_containers_container_object_error_download": "Ocorreu um erro durante a transferência do objeto: {{ message }}.",
"pci_projects_project_storages_containers_object_title": "Object Storage",
"pci_projects_project_storages_containers_object_description": "Aloje o seu catálogo de dados na nossa solução Object Storage. Pode administrar estes dados a partir da sua aplicação graças às API S3."
}
38 changes: 37 additions & 1 deletion packages/manager/apps/pci-object-storage/src/api/data/objects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v6 } from '@ovh-ux/manager-core-api';
import { OPENIO_DEFAULT_REGION } from '@/constants';
import { addMonths } from 'date-fns';
import { OPENIO_DEFAULT_REGION, OPENIO_PRESIGN_EXPIRE } from '@/constants';

export type TAccess = {
token: string;
Expand Down Expand Up @@ -65,3 +66,38 @@ export const addUser = async (
);
return data;
};

export const downloadStandardS3Object = async (
projectId: string,
regionName: string,
storageId: string,
object,
) => {
const { data } = await v6.post(
`/cloud/project/${projectId}/region/${regionName}/${object.s3StorageType}/${storageId}/presign`,
{
expire: OPENIO_PRESIGN_EXPIRE,
method: 'GET',
object: object.key,
},
);
return data;
};

export const downloadObject = async (
projectId: string,
storageId: string,
object,
) => {
const expirationDate = addMonths(new Date(), 1).toISOString();

const { data } = await v6.post(
`/cloud/project/${projectId}/storage/${storageId}/publicUrl`,
{
expirationDate,
objectName: object?.name,
},
);

return data;
};
1 change: 1 addition & 0 deletions packages/manager/apps/pci-object-storage/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,4 @@ export const OBJECT_CONTAINER_OFFERS_LABELS = {
price: null,
},
};
export const OPENIO_PRESIGN_EXPIRE = 3600;
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ApiError } from '@ovh-ux/manager-core-api';
import { useNotifications } from '@ovh-ux/manager-react-components';
import { ShellContext } from '@ovh-ux/manager-react-shell-client';
import { useContext } from 'react';
import { Translation } from 'react-i18next';
import { useParams, useSearchParams } from 'react-router-dom';
import { PAGE_PREFIX } from '@/tracking.constants';
import { downloadObject, downloadStandardS3Object } from '@/api/data/objects';

export default function DownloadObjectPage() {
const { projectId, storageId } = useParams();
const [searchParams] = useSearchParams();
const { tracking } = useContext(ShellContext).shell;
const { addError } = useNotifications();

/**
* TODO
* Waiting for the objet type,
* and this function should be implemented on action menu of object datagrid
*/
const handleObjectDownload = (object) => {
tracking.trackClick({
name: `${PAGE_PREFIX}object::download-file`,
type: 'action',
});

let downloadPromise = null;

if (object.s3StorageType) {
downloadPromise = downloadStandardS3Object(
projectId,
searchParams.get('region'),
storageId,
object,
);
} else {
downloadPromise = downloadObject(projectId, storageId, object);
}

return downloadPromise
.then((url: string) => {
window.top.location = url;
})
.catch((error: ApiError) =>
addError(
<Translation ns="objects">
{(_t) =>
_t(
`pci_projects_project_storages_containers_container_object_error_download`,
{
message:
error?.response?.data?.message || error?.message || null,
},
)
}
</Translation>,
true,
),
);
};

return <></>;
}

0 comments on commit a8f44e2

Please sign in to comment.