Skip to content

Commit

Permalink
chore: disallow and skip download for drive_document
Browse files Browse the repository at this point in the history
  • Loading branch information
uhrjun committed Aug 16, 2023
1 parent b095cd5 commit 2ce2c6d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 57 deletions.
6 changes: 6 additions & 0 deletions drive/api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ def get_file_content(entity_name, trigger_download=0):
# status_code = status.HTTP_206_PARTIAL_CONTENT
# Figure out a sane way to handle blob range streaming requests

try:
file = open(drive_entity.path, "rb")
except TypeError:
response = Response(frappe.request.environ)
response.status_code = 204
return response

response = Response(
wrap_file(frappe.request.environ, file), direct_passthrough=True
)
Expand Down
1 change: 1 addition & 0 deletions drive/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def get_shared_with_me(get_all=False, order_by="modified"):
DriveEntity.parent_drive_entity,
DriveEntity.allow_comments,
DriveEntity.color,
DriveEntity.document,
DriveEntity.allow_download,
DocShare.read,
DocShare.write,
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/pages/Favourites.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ export default {
window.location.href = `/api/method/drive.api.files.get_file_content?entity_name=${this.selectedEntities[0].name}&trigger_download=1`;
},
isEnabled: () => {
return (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
);
if (this.selectedEntities.length === 1) {
if (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
) {
return !this.selectedEntities[0].document;
}
}
},
},
// {
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/pages/Folder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ export default {
window.location.href = `/api/method/drive.api.files.get_file_content?entity_name=${this.selectedEntities[0].name}&trigger_download=1`;
},
isEnabled: () => {
return (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
);
if (this.selectedEntities.length === 1) {
if (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
) {
return !this.selectedEntities[0].document;
}
}
},
},
{
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/pages/FolderShare.vue
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ export default {
window.location.href = `/api/method/drive.api.files.get_file_content?entity_name=${this.selectedEntities[0].name}&trigger_download=1`;
},
isEnabled: () => {
return (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
);
if (this.selectedEntities.length === 1) {
if (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
) {
return !this.selectedEntities[0].document;
}
}
},
},
{
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,14 @@ export default {
window.location.href = `/api/method/drive.api.files.get_file_content?entity_name=${this.selectedEntities[0].name}&trigger_download=1`;
},
isEnabled: () => {
return (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
);
if (this.selectedEntities.length === 1) {
if (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group
) {
return !this.selectedEntities[0].document;
}
}
},
},
{
Expand Down
17 changes: 11 additions & 6 deletions frontend/src/pages/Shared.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,17 @@ export default {
window.location.href = `/api/method/drive.api.files.get_file_content?entity_name=${this.selectedEntities[0].name}&trigger_download=1`;
},
isEnabled: () => {
return (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group &&
(this.selectedEntities[0].allow_download ||
this.selectedEntities[0].write)
);
if (this.selectedEntities.length === 1) {
if (
this.selectedEntities.length === 1 &&
!this.selectedEntities[0].is_group &&
(this.selectedEntities[0].allow_download ||
this.selectedEntities[0].write)
) {
console.log(this.selectedEntities[0]);
return !this.selectedEntities[0].document;
}
}
},
},
// {
Expand Down
77 changes: 42 additions & 35 deletions frontend/src/utils/folderDownload.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
import JSZip from 'jszip';
import JSZip from "jszip";

export function selectedEntitiesDownload(selected_entities) {
const generateRandomString = () => [...Array(5)].map(() => Math.random().toString(36)[2]).join('');
const generateRandomString = () =>
[...Array(5)].map(() => Math.random().toString(36)[2]).join("");
const randomString = generateRandomString();
const folderName = "FDrive_" + randomString;

const zip = new JSZip();

const processEntity = (entity, parentFolder) => {
console.log(entity.title);
if (entity.is_group) {
const folder = parentFolder.folder(entity.title);
return get_children(entity.name)
.then((children) => {
const promises = children.map((childEntity) => processEntity(childEntity, folder));
return Promise.all(promises);
});
return get_children(entity.name).then((children) => {
const promises = children.map((childEntity) =>
processEntity(childEntity, folder)
);
return Promise.all(promises);
});
} else if (entity.document) {
return;
} else {
return get_file_content(entity.name)
.then((fileContent) => {
parentFolder.file(entity.title, fileContent);
});
return get_file_content(entity.name).then((fileContent) => {
parentFolder.file(entity.title, fileContent);
});
}
};

const promises = selected_entities.map((entity) => processEntity(entity, zip));
const promises = selected_entities.map((entity) =>
processEntity(entity, zip)
);

Promise.all(promises)
.then(() => {
return zip.generateAsync({ type: 'blob', streamFiles: true });
return zip.generateAsync({ type: "blob", streamFiles: true });
})
.then(function (content) {
var downloadLink = document.createElement('a');
var downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(content);
downloadLink.download = folderName + '.zip';
downloadLink.download = folderName + ".zip";

document.body.appendChild(downloadLink);
downloadLink.click();
Expand All @@ -50,12 +54,12 @@ export function folderDownload(root_entity) {

temp(root_entity.name, zip)
.then(() => {
return zip.generateAsync({ type: 'blob', streamFiles: true });
return zip.generateAsync({ type: "blob", streamFiles: true });
})
.then((content) => {
const downloadLink = document.createElement('a');
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(content);
downloadLink.download = folderName + '.zip';
downloadLink.download = folderName + ".zip";

document.body.appendChild(downloadLink);
downloadLink.click();
Expand All @@ -75,10 +79,9 @@ function temp(entity_name, parentZip) {
const folder = parentZip.folder(entity.title);
return temp(entity.name, folder);
} else {
return get_file_content(entity.name)
.then((fileContent) => {
parentZip.file(entity.title, fileContent);
});
return get_file_content(entity.name).then((fileContent) => {
parentZip.file(entity.title, fileContent);
});
}
});

Expand All @@ -97,22 +100,26 @@ function temp(entity_name, parentZip) {
}

function get_file_content(entity_name) {
const fileUrl = "/api/method/" + `drive.api.files.get_file_content?entity_name=${entity_name}`;

return fetch(fileUrl)
.then(response => {
if (response.ok) {
return response.blob();
} else {
throw new Error(`Request failed with status ${response.status}`);
}
});
const fileUrl =
"/api/method/" +
`drive.api.files.get_file_content?entity_name=${entity_name}`;

return fetch(fileUrl).then((response) => {
if (response.ok) {
return response.blob();
} else if (response.status === 204) {
console.log(response);
} else {
throw new Error(`Request failed with status ${response.status}`);
}
});
}


function get_children(entity_name) {
return new Promise((resolve, reject) => {
const url = "/api/method/" + `drive.api.nested_folder.folder_contents?entity_name=${entity_name}`;
const url =
"/api/method/" +
`drive.api.nested_folder.folder_contents?entity_name=${entity_name}`;

const xhr = new XMLHttpRequest();
xhr.open("GET", url);
Expand Down

0 comments on commit 2ce2c6d

Please sign in to comment.