Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions client-react/src/client/connectors/google_drive_v2/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function normalizeResource(resource) {
modifiedDate: Date.parse(resource.modifiedDate),
title: resource.title,
type: resource.mimeType === 'application/vnd.google-apps.folder' ? 'dir' : 'file',
size: typeof resource.fileSize === 'undefined' ? resource.fileSize : parseInt(resource.fileSize),
size: typeof resource.fileSize === 'undefined' ? resource.fileSize : parseInt(resource.fileSize, 10),
parents: resource.parents,
capabilities: resource.capabilities,
downloadUrl: resource.downloadUrl,
Expand Down Expand Up @@ -188,7 +188,11 @@ async function downloadResources({ resources, apiOptions, trackers: {
if (resources.length === 1) {
const downloadParams = getDownloadParams(resources[0]);
onStart({ name: `Downloading ${downloadParams.fileName}...`, quantity: 1 });
const result = await downloadResource({ resource: resources[0], params: downloadParams, onProgress, i: 0, l: 1 });
const result = await downloadResource({ resource: resources[0], params: downloadParams, onProgress, i: 0, l: 1 }).
catch(err => {
console.error('Error downloading resource: ', err);
onFail()
});
onSuccess();
return result;
}
Expand All @@ -207,6 +211,9 @@ async function downloadResources({ resources, apiOptions, trackers: {
direct: false
},
onProgress, i, l
}).catch(err => {
console.error('Error downloading archive: ', err);
onFail()
})
),
onProgress
Expand Down Expand Up @@ -251,7 +258,7 @@ async function uploadChunk({ sessionUrl, size, startByte, content }) {
set('Content-Encoding', 'base64').
send(btoa(content.slice(startByte, endByte))).
end((err, res) => {
if (err) { } // pass
if (err) { } // pass // TBD how to handle errors
resolve(res);
});
});
Expand Down Expand Up @@ -291,27 +298,50 @@ async function uploadFileToId(parentId, { onStart, onSuccess, onFail, onProgress
}
}

async function createFolder(apiOptions, parentId, folderName) {
return await window.gapi.client.drive.files.insert({
async function createFolder(apiOptions, parentId, folderName, { onFail }) {
return window.gapi.client.drive.files.insert({
title: folderName,
parents: [{ id: parentId }],
mimeType: 'application/vnd.google-apps.folder'
});
}).then(
res => res,
err => {
console.error(`Filemanager. createFolder(${id})`, err);
onFail()
}
);
}

async function renameResource(apiOptions, id, newName) {
return await window.gapi.client.drive.files.patch({
async function renameResource(apiOptions, id, newName, { onFail }) {
return window.gapi.client.drive.files.patch({
fileId: id,
title: newName
});
}).then(
res => res,
err => {
console.error('Failed to rename resource: ', err);
onFail()
}
)
}

function getResourceName(apiOptions, resource) {
return resource.title;
}

async function removeResources() {
async function removeResource(apiOptions, resource) {
return window.gapi.client.drive.files.delete({
fileId: resource.id,
});
}

async function removeResources(apiOptions, selectedResources, { onSuccess, onFail }) {
let success = await Promise.all(selectedResources.map(async (resource) => await removeResource(apiOptions, resource))).
catch((error) => {
console.error(`Filemanager. removeResources`, error);
onFail();
});
onSuccess();
}

async function signIn() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import api from '../api';
import sanitizeFilename from 'sanitize-filename';
import ContextMenuItem from '../../../components/ContextMenuItem';
import SetNameDialog from '../../../components/SetNameDialog';
import onFailError from '../../utils/onFailError';

let icon = require('@opuscapita/svg-icons/lib/create_new_folder.svg');
let label = 'Create folder';
Expand All @@ -19,6 +21,14 @@ function handler(apiOptions, {
getResourceLocation,
getNotifications
}) {
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'createFolder',
updateNotifications,
message
});

showDialog((
<SetNameDialog
onHide={hideDialog}
Expand All @@ -30,7 +40,7 @@ function handler(apiOptions, {
return `File or folder with name "${folderName}" already exists`;
} else {
hideDialog();
let { result } = await api.createFolder(apiOptions, resource.id, folderName);
let { result } = await api.createFolder(apiOptions, resource.id, folderName, { onFail });
navigateToDir(resource.id, result.id, false);
}
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';
import api from '../api';
import ContextMenuItem from '../../../components/ContextMenuItem';
import onFailError from '../../utils/onFailError';
import ConfirmDialog from '../../../components/ConfirmDialog';

let icon = require('@opuscapita/svg-icons/lib/delete.svg');
let label = 'Remove';
Expand All @@ -17,7 +20,37 @@ function handler(apiOptions, {
getResourceLocation,
getNotifications
}) {
let onSuccess = () => {
let resource = getResource();
navigateToDir(resource.id, null, false);
};

const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'delete',
updateNotifications,
message
});

let selectedResources = getSelectedResources();
let dialogNameText = `Do you really want to remove\n`;
let dialogFilesText = selectedResources.length > 1 ?
`${selectedResources.length} files ?` :
`"${selectedResources[0].name}" ?`;

return showDialog((
<ConfirmDialog
onHide={hideDialog}
onSubmit={async () => {
hideDialog();
api.removeResources(apiOptions, selectedResources, { onSuccess, onFail });
}}
headerText={dialogNameText + dialogFilesText}
cancelButtonText={'Cancel'}
submitButtonText={'Confirm'}
/>
));
}

export default (apiOptions, {
Expand All @@ -37,9 +70,14 @@ export default (apiOptions, {
label,
title: 'Remove',
shouldBeAvailable: (apiOptions) => {
// let selectedResources = getSelectedResources();
let selectedResources = getSelectedResources();
if (!selectedResources.length) {
return false;
}

// return selectedResources.every(resource => resource.capabilities.canDelete);
return false;
console.log(selectedResources) // returns ...capabilities: {canCopy: true, canEdit: true}, 'canDelete' missing
return selectedResources.every(resource => resource.capabilities.canEdit); // TBD is this correct?
},
availableInContexts: ['row', 'toolbar'],
handler: () => handler(apiOptions, {
Expand All @@ -55,7 +93,21 @@ export default (apiOptions, {
getNotifications
}),
contextMenuRenderer: (apiOptions) => (
<ContextMenuItem icon={{ svg: icon }}>
<ContextMenuItem
icon={{ svg: icon }}
onClick={() => handler(apiOptions, {
showDialog,
hideDialog,
navigateToDir,
updateNotifications,
getSelection,
getSelectedResources,
getResource,
getResourceChildren,
getResourceLocation,
getNotifications
})}
>
<span>{label}</span>
</ContextMenuItem>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ContextMenuItem from '../../../components/ContextMenuItem';
import NotificationProgressItem from '../../../components/NotificationProgressItem';
import notifUtils from '../../../components/Notifications/utils';
import { getIcon } from '../icons';
import onFailError from '../../utils/onFailError';

const icon = require('@opuscapita/svg-icons/lib/file_download.svg');
const label = 'Download';
Expand Down Expand Up @@ -66,7 +67,14 @@ function handler(apiOptions, {
updateNotifications(newNotifications);
};

const onFail = () => { };
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId,
updateNotifications,
message
});

const onProgress = (progress) => {
const notifications = getNotifications();
const notification = notifUtils.getNotification(notifications, notificationId);
Expand All @@ -93,7 +101,11 @@ function handler(apiOptions, {
...(mimeType === 'application/pdf' ? { target: '_blank' } : null)
}) :
promptToSaveBlob({ content: file, name: fileName })
).catch(err => console.log(err))
)
// .catch(err => {
// console.log('Failed to download resource: ', err);
// onFail()
// })
}

export default (apiOptions, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import api from '../api';
import sanitizeFilename from 'sanitize-filename';
import ContextMenuItem from '../../../components/ContextMenuItem';
import SetNameDialog from '../../../components/SetNameDialog';
import onFailError from '../../utils/onFailError';

let icon = `<svg class="a-s-fa-Ha-pa" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" focusable="false" fill="rgba(0, 0, 0, 0.72)"><path d="M0 0h24v24H0z" fill="none"></path><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM6 17v-2.47l7.88-7.88c.2-.2.51-.2.71 0l1.77 1.77c.2.2.2.51 0 .71L8.47 17H6zm12 0h-7.5l2-2H18v2z"></path></svg>`;
let label = 'Rename';
Expand All @@ -19,6 +21,14 @@ function handler(apiOptions, {
getResourceLocation,
getNotifications
}) {
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'rename',
updateNotifications,
message
});

showDialog((
<SetNameDialog
onHide={hideDialog}
Expand All @@ -30,7 +40,7 @@ function handler(apiOptions, {
return `File or folder with name "${name}" already exists`;
} else {
hideDialog();
let { result } = await api.renameResource(apiOptions, selectedResources[0].id, name);
let { result } = await api.renameResource(apiOptions, selectedResources[0].id, name, { onFail });
let resource = getResource();
navigateToDir(resource.id, result.id, false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import api from '../api';
import ContextMenuItem from '../../../components/ContextMenuItem';
import NotificationProgressItem from '../../../components/NotificationProgressItem';
import notifUtils from '../../../components/Notifications/utils';
import { getIcon } from '../icons';
import nanoid from 'nanoid';
import onFailError from '../../utils/onFailError';

let icon = require('@opuscapita/svg-icons/lib/file_upload.svg');
let label = 'Upload';
Expand Down Expand Up @@ -69,7 +71,14 @@ function handler(apiOptions, {
navigateToDir(resource.id, null, false);
};

let onFail = () => {};
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId,
updateNotifications,
message
});

let onProgress = (progress) => {
let notifications = getNotifications();
let notification = notifUtils.getNotification(notifications, notificationId);
Expand Down
2 changes: 1 addition & 1 deletion client-react/src/client/connectors/nodejs_v1/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function getChildrenForId(options, { id, sortBy = 'name', sortDirection =
let response = await request(method, route).catch((error) => {
console.error(`Filemanager. getChildrenForId(${id})`, error);
if (onFail) {
onFail({ message: 'Unable to read a directory.' }) // TODO doesn't intercept for some reason
onFail({ message: 'Unable to read a directory.' })
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ function handler(apiOptions, {
getResourceLocation,
getNotifications
}) {
const onFail = _ => onFailError({
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'createFolder',
updateNotifications
updateNotifications,
message
});

showDialog((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ function handler(apiOptions, {
navigateToDir(resource.id, null, false);
};

const onFail = _ => onFailError({
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'delete',
updateNotifications
updateNotifications,
message
});

let selectedResources = getSelectedResources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ function handler(apiOptions, {
updateNotifications(newNotifications);
};

const onFail = _ => onFailError({
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId,
updateNotifications
updateNotifications,
message
});

const onProgress = (progress) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function handler(apiOptions, {
getResourceLocation,
getNotifications
}) {
const onFail = ({ message }) => onFailError({
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'rename',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default (apiOptions, {
}) => ({
id: 'sort',
handler: async ({ sortBy, sortDirection }) => {
const onFail = ({ message }) => onFailError({
const onFail = ({ message } = {}) => onFailError({
getNotifications,
label,
notificationId: 'rename',
Expand Down
Loading