Skip to content
Snippets Groups Projects
Commit 563e8ed9 authored by vkovtun's avatar vkovtun
Browse files

BIS-753: Adding file download logic.

parent a909d039
No related branches found
No related tags found
1 merge request!40SSDM-13578 : 2PT : Database and V3 Implementation - include the new AFS "free"...
......@@ -17,6 +17,8 @@
import ComponentController from '@src/js/components/common/ComponentController.js'
import autoBind from 'auto-bind'
const MAX_READ_SIZE_IN_BYTES = 1024
export default class DataBrowserController extends ComponentController {
constructor() {
......@@ -62,8 +64,8 @@ export default class DataBrowserController extends ComponentController {
async createNewFolder(name) {
return new Promise((resolve, reject) => {
this.component.datastoreServer.create(this.owner, this.path + name, true, async (success) => {
if (success) {
this.component.datastoreServer.create(this.owner, this.path + name, true, async (response) => {
if (response) {
if (this.gridController) {
await this.gridController.load()
}
......@@ -77,8 +79,8 @@ export default class DataBrowserController extends ComponentController {
async rename(oldName, newName) {
return new Promise((resolve, reject) => {
this.component.datastoreServer.move(this.owner, this.path + oldName, this.owner, this.path + newName, async (success) => {
if (success) {
this.component.datastoreServer.move(this.owner, this.path + oldName, this.owner, this.path + newName, async (response) => {
if (response) {
if (this.gridController) {
await this.gridController.load()
}
......@@ -102,8 +104,8 @@ export default class DataBrowserController extends ComponentController {
async _delete(file) {
return new Promise((resolve, reject) => {
this.component.datastoreServer.delete(this.owner, file.path, async (success) => {
if (success) {
this.component.datastoreServer.delete(this.owner, file.path, async (response) => {
if (response) {
resolve()
} else {
reject()
......@@ -125,8 +127,8 @@ export default class DataBrowserController extends ComponentController {
async _copy(file, newLocation){
const cleanNewLocation = this._removeLeadingSlash(newLocation) + file.name
return new Promise((resolve, reject) => {
this.component.datastoreServer.copy(this.owner, file.path, this.owner, cleanNewLocation, async (success) => {
if (success) {
this.component.datastoreServer.copy(this.owner, file.path, this.owner, cleanNewLocation, async (response) => {
if (response) {
resolve()
} else {
reject()
......@@ -148,8 +150,8 @@ export default class DataBrowserController extends ComponentController {
async _move(file, newLocation){
const cleanNewLocation = this._removeLeadingSlash(newLocation) + file.name
return new Promise((resolve, reject) => {
this.component.datastoreServer.move(this.owner, file.path, this.owner, cleanNewLocation, async (success) => {
if (success) {
this.component.datastoreServer.move(this.owner, file.path, this.owner, cleanNewLocation, async (response) => {
if (response) {
resolve()
} else {
reject()
......@@ -158,8 +160,29 @@ export default class DataBrowserController extends ComponentController {
})
}
async download() {
async download(file) {
let offset = 0
const dataArray = []
while (offset < file.size) {
dataArray.push(await this._download(file, offset))
offset += MAX_READ_SIZE_IN_BYTES
}
return dataArray
}
async _download(file, offset) {
return new Promise((resolve, reject) => {
const limit = Math.min(MAX_READ_SIZE_IN_BYTES, file.size - offset)
this.component.datastoreServer.read(this.owner, file.path, offset, limit, async (responseData) => {
if (responseData) {
resolve(responseData)
} else {
reject()
}
})
})
}
_removeLeadingSlash(path) {
......
......@@ -184,6 +184,18 @@ class LeftToolbar extends React.Component {
this.closeDeleteDialog()
}
async handleDownload() {
const { multiselectedFiles } = this.props
const file = multiselectedFiles.values().next().value;
const dataArray = await this.controller.download(file)
const blob = new Blob(dataArray, { type: "application/octet-stream" })
const link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = file.name
link.click()
}
renderNoSelectionContextToolbar() {
const { classes, buttonSize } = this.props
return ([
......@@ -242,6 +254,7 @@ class LeftToolbar extends React.Component {
variant='outlined'
disabled={multiselectedFiles.size !== 1 || multiselectedFiles.values().next().value.directory}
startIcon={<DownloadIcon />}
onClick={this.handleDownload}
>
{messages.get(messages.DOWNLOAD)}
</Button>,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment