-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathFileComponent.js
More file actions
377 lines (333 loc) · 12.7 KB
/
FileComponent.js
File metadata and controls
377 lines (333 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import React from "react";
import ReactDOM from "react-dom";
import {
Button,
Card,
CardBody,
CardFooter,
DropdownItem,
DropdownMenu,
DropdownToggle,
Modal,
ModalHeader,
ModalBody,
ModalFooter,
UncontrolledButtonDropdown
} from "reactstrap";
import {ItemTypes} from './Constants'
import {DragSource} from 'react-dnd'
import {formatBytes} from "../../../utils/Tools";
import {performCopyFile, performMoveFile} from "../../../utils/API/API";
import {toast} from "react-toastify";
import * as PropTypes from "prop-types";
import handleViewport from 'react-in-viewport';
import MediaWidget, {isMedia} from "../../Base/MediaWidget/MediaWidget";
import {PROP_ITEM} from "../../../utils/RclonePropTypes";
import ErrorBoundary from "../../../ErrorHandling/ErrorBoundary";
async function performCopyMoveOperation(params) {
const {srcRemoteName, srcRemotePath, destRemoteName, destRemotePath, Name, IsDir, dropEffect, updateHandler} = params;
if (dropEffect === "copy") { /*Default operation without holding alt is copy, named as move in react-dnd*/
// if (component.props.canCopy) {
await performCopyFile(srcRemoteName, srcRemotePath, destRemoteName, destRemotePath, Name, IsDir);
updateHandler();
if (IsDir) {
toast.info(`Directory copying started in background: ${Name}`);
} else {
toast.info(`File copying started in background: ${Name}`);
}
// } else {
// toast.error("This remote does not support copying");
// }
} else if (dropEffect === "move") {
// if (component.props.canMove) {
await performMoveFile(srcRemoteName, srcRemotePath, destRemoteName, destRemotePath, Name, IsDir);
updateHandler();
if (IsDir) {
toast.info(`Directory moving started in background: ${Name}`);
} else {
toast.info(`Directory moving started in background: ${Name}`);
}
// } else {
// toast.error("This remote does not support moving");
// }
}
}
let modalbox = true;
let operation = "";
const modalClose = () => {
const modalNode = document.getElementById("mainModalElement");
ReactDOM.unmountComponentAtNode(modalNode);
ReactDOM.render(emptyElement, document.getElementById("modalElement"));
}
const modalMove = () => {
const modalNode = document.getElementById("mainModalElement");
ReactDOM.unmountComponentAtNode(modalNode);
operation = "move";
ReactDOM.render(emptyElement, document.getElementById("modalElement"));
}
const modalCopy = () => {
const modalNode = document.getElementById("mainModalElement");
ReactDOM.unmountComponentAtNode(modalNode);
operation = "copy";
ReactDOM.render(emptyElement, document.getElementById("modalElement"));
}
let emptyElement = (<div></div>);
let modalElement = (
<Modal id="mainModalElement" isOpen={modalbox} toggle={modalClose}>
<ModalHeader>Confirm Operation</ModalHeader>
<ModalBody> Confirm operation: Move or Copy </ModalBody>
<ModalFooter>
<Button color="primary" onClick={modalMove}>Move</Button>{' '}
<Button color="primary" onClick={modalCopy}>Copy</Button>
</ModalFooter>
</Modal>
);
const fileComponentSource = {
canDrag(props) {
// You can disallow drag based on props
return true;
},
beginDrag(props) {
// console.log("props", props, props.remoteName);
const {Name, Path, IsDir} = props.item;
return {
Name: Name, Path: Path, IsDir: IsDir, remoteName: props.remoteName, remotePath: props.remotePath
}
},
endDrag(props, monitor, component) {
// console.log("EndDrag", monitor.getDropResult());
console.log(props, "Component:", component);
ReactDOM.render(modalElement, document.getElementById("modalElement"));
console.log(operation);
try {
if (monitor.getDropResult() && component) {
performCopyMoveOperation(monitor.getDropResult());
}
} catch (e) {
const error = e.response ? e.response : e;
console.log(JSON.stringify(error));
toast.error(`Error copying file(s). ${error}`, {
autoClose: false
});
}
}
};
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
}
}
function FileIcon({IsDir, MimeType}, ...props) {
let className = "fa-file";
if (IsDir) {
className = "fa-folder";
} else if (MimeType === "application/pdf") {
className = "fa-file-pdf-o";
} else if (MimeType === "image/jpeg") {
className = "fa-file-image-o";
} else if (MimeType === "application/rar" || MimeType === "application/x-rar-compressed" || MimeType === " application/zip") {
className = "fa-file-archive-o";
} else if (MimeType === "text/plain") {
className = "fa-file-text-o";
} else if (MimeType === "text/x-vcard") {
className = "fa-address-card-o";
}
return <i className={className + " fa fa-lg"}/>;
}
function confirmDelete(deleteHandle, item) {
if (window.confirm(`Are you sure you want to delete ${item.Name}`)) {
deleteHandle(item);
}
}
function Actions({downloadHandle, deleteHandle, item, linkShareHandle}) {
const {IsDir} = item;
// let {ID, Name} = item;
// // Using fallback as fileName when the ID is not available (for local file system)
// if (ID === undefined) {
// ID = Name;
// }
if (!IsDir) {
return (
<React.Fragment>
<Button color="link" onClick={() => downloadHandle(item)}>
<i className={"fa fa-cloud-download fa-lg d-inline"}/>
</Button>
<Button color="link">
<i className="fa fa-info-circle"/>
</Button>
<UncontrolledButtonDropdown>
<DropdownToggle color="link">
<i className="fa fa-ellipsis-v"/>
</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Actions</DropdownItem>
<DropdownItem onClick={() => linkShareHandle(item)}><i
className="fa fa-share fa-lg d-inline"/> Share with link</DropdownItem>
<DropdownItem divider/>
<DropdownItem onClick={() => confirmDelete(deleteHandle, item)}><i
className="fa fa-remove fa-lg d-inline text-danger"/> Delete </DropdownItem>
</DropdownMenu>
</UncontrolledButtonDropdown>
</React.Fragment>
);
} else {
return (
<React.Fragment>
<UncontrolledButtonDropdown>
<DropdownToggle color="link">
<i className="fa fa-ellipsis-v"/>
</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Actions</DropdownItem>
<DropdownItem onClick={() => linkShareHandle(item)}><i
className="fa fa-share fa-lg d-inline"/> Share with link</DropdownItem>
<DropdownItem divider/>
<DropdownItem onClick={() => confirmDelete(deleteHandle, item)}><i
className="fa fa-remove fa-lg d-inline text-danger"/> Delete </DropdownItem>
</DropdownMenu>
</UncontrolledButtonDropdown>
</React.Fragment>
)
}
}
/**
* Main class for individual render of file/directory in the files view.
*/
// Non used props are required for drag-and-drop functionality
class FileComponent extends React.Component {
/*
MimeTypes: https://www.freeformatter.com/mime-types-list.html
* {
* For Directory
"ID": "18DsZ4ne6XV3qwDZQCBj2nAEwouFMxudB",
"IsDir": true,
"MimeType": "inode/directory",
"ModTime": "2019-02-12T14:23:33.440Z",
"Name": "two pass 28-1-19",
"Path": "two pass 28-1-19",
"Size": -1
},
*
* // For non-directory
* {
"ID": "1u4D6-UdxhJYY8AVd8FcTN2Tl73W1RXsk",
"IsDir": false,
"MimeType": "application/octet-stream",
"ModTime": "2018-11-18T13:14:54.068Z",
"Name": "streamlined-gdoc.gdoc",
"Path": "streamlined-gdoc.gdoc",
"Size": 173
},
* */
handleClick(IsDir, clickHandler, e, item) {
if (IsDir) {
clickHandler(e, item)
}
}
render() {
const {containerID, inViewport, item, loadImages, clickHandler, downloadHandle, linkShareHandle, deleteHandle, connectDragSource, gridMode, itemIdx /*isDragging, remoteName*/} = this.props;
const {IsDir, MimeType, ModTime, Name, Size} = item;
let modTime = new Date(Date.parse(ModTime));
let element;
if (gridMode === "card") {
element = connectDragSource(
<div className={IsDir ? "" : "col-md-4"}>
<Card>
<CardBody onClick={(e) => this.handleClick(IsDir, clickHandler, e, item)}>
{loadImages && isMedia(MimeType) ?
<MediaWidget containerID={containerID} item={item} inViewport={inViewport}/> :
<FileIcon IsDir={IsDir} MimeType={MimeType}/>
}
{Name}
</CardBody>
<CardFooter>
<Actions downloadHandle={downloadHandle} linkShareHandle={linkShareHandle}
deleteHandle={deleteHandle} item={item}/>
</CardFooter>
</Card>
</div>
)
} else {
element = connectDragSource(
<tr className={"pointer-cursor"}>
<td className="d-none d-md-table-cell"><input type="checkbox"/></td>
<td onClick={(e) => clickHandler(e, item)} id={"file" + itemIdx}>
<FileIcon IsDir={IsDir} MimeType={MimeType}/> {Name}
</td>
<td>{Size === -1 ? "-" : formatBytes(Size, 2)}</td>
<td className="d-none d-md-table-cell">{modTime.toLocaleDateString()}</td>
<td><Actions downloadHandle={downloadHandle} linkShareHandle={linkShareHandle}
deleteHandle={deleteHandle} item={item}/></td>
</tr>
)
}
return <ErrorBoundary>
{element}
<button onClick={this.modalOpen}>Open Modal</button>
<div id="modalElement">
</div>
</ErrorBoundary>;
}
}
FileComponent.propTypes = {
/**
* Item as returned from the rclone backend
*/
item: PROP_ITEM.isRequired,
/**
* Function which handles the clicks on the current item.
*/
clickHandler: PropTypes.func.isRequired,
/**
* Function to handle the download of the current file
*/
downloadHandle: PropTypes.func.isRequired,
/**
* Function to delete a file.
*/
deleteHandle: PropTypes.func.isRequired,
/**
* Function to share the link of a file.
*/
linkShareHandle: PropTypes.func.isRequired,
/**
* Name of the remote containing the {item}.
*/
remoteName: PropTypes.string.isRequired,
/**
* Remote path of the current item. remoteName + remotePath gives the full path.
*/
remotePath: PropTypes.string.isRequired,
/**
* Denotes the current grid mode:
* card: Card mode with Media support.
*/
gridMode: PropTypes.string,
/**
* Container ID of the FilesExplorer this component is contained in.
*/
containerID: PropTypes.string.isRequired,
/**
* Boolean value to represent if the current item can be moved to another destination.
*/
canMove: PropTypes.bool.isRequired,
/**
* Boolean value to represent if the current item can be copied to another destination.
*/
canCopy: PropTypes.bool.isRequired,
/**
* Boolean value to represent if loading media files is enabled by the user.
*/
loadImages: PropTypes.bool.isRequired,
/**
* Boolean value to represent if the current remote is bucketbased, the url of a bucket based remote is different.
*/
isBucketBased: PropTypes.bool.isRequired
};
/**
* Handles view port to check if the current item is visible on the screen.
*/
const MyViewPort = handleViewport(FileComponent, {rootMargin: '-1.0px'});
export default DragSource(ItemTypes.FILECOMPONENT, fileComponentSource, collect)(MyViewPort);