From 6b66e538da2d627c206c049345bc856644c6dfa2 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 20 Apr 2021 13:23:51 +0900 Subject: [PATCH] Add repositories for downloading dataset --- .../download/apiDownloadFormatRepository.ts | 16 ++++++++++ .../download/apiDownloadRepository.ts | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 frontend/repositories/download/apiDownloadFormatRepository.ts create mode 100644 frontend/repositories/download/apiDownloadRepository.ts diff --git a/frontend/repositories/download/apiDownloadFormatRepository.ts b/frontend/repositories/download/apiDownloadFormatRepository.ts new file mode 100644 index 00000000..50c79626 --- /dev/null +++ b/frontend/repositories/download/apiDownloadFormatRepository.ts @@ -0,0 +1,16 @@ +import ApiService from '@/services/api.service' +import { DownloadFormatRepository } from '@/domain/models/download/downloadFormatRepository' +import { Format } from '~/domain/models/download/format' + +export class APIDownloadFormatRepository implements DownloadFormatRepository { + constructor( + private readonly request = ApiService + ) {} + + async list(projectId: string): Promise { + const url = `/projects/${projectId}/download-format` + const response = await this.request.get(url) + const responseItems: Format[] = response.data + return responseItems.map(item => Format.valueOf(item)) + } +} diff --git a/frontend/repositories/download/apiDownloadRepository.ts b/frontend/repositories/download/apiDownloadRepository.ts new file mode 100644 index 00000000..be249b21 --- /dev/null +++ b/frontend/repositories/download/apiDownloadRepository.ts @@ -0,0 +1,31 @@ +import ApiService from '@/services/api.service' +import { DownloadRepository } from '@/domain/models/download/downloadRepository' + +export class APIDownloadRepository implements DownloadRepository { + constructor( + private readonly request = ApiService + ) {} + + async prepare(projectId: string, format: string): Promise { + const url = `/projects/${projectId}/download` + const data = { + format, + } + const response = await this.request.post(url, data) + return response.data.task_id + } + + async download(projectId: string, taskId: string): Promise { + const url = `/projects/${projectId}/download?taskId=${taskId}` + const config = { + responseType: 'blob', + } + const response = await this.request.get(url, config) + const downloadUrl = window.URL.createObjectURL(new Blob([response.data])) + const link = document.createElement('a') + link.href = downloadUrl + link.setAttribute('download', `${taskId}.zip`) + document.body.appendChild(link) + link.click() + } +}