Skip to content

Commit 2ee5eb9

Browse files
andrehoffmann30daniellienert
authored andcommitted
TASK: adjust upload files hook to use tus client
1 parent c3dc792 commit 2ee5eb9

12 files changed

Lines changed: 64720 additions & 70 deletions

File tree

Classes/Controller/MediaController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function indexAction(): void
7979
* @throws Exception
8080
* @throws FilesException
8181
* @throws ReflectionException
82+
* @Flow\SkipCsrfProtection
8283
*/
8384
public function uploadAction(): void
8485
{
@@ -87,7 +88,7 @@ public function uploadAction(): void
8788
Files::createDirectoryRecursively($uploadDirectory);
8889
}
8990

90-
$server = new Server($this->partialUploadFileCacheAdapater);
91+
$server = new Server();
9192
$server->setApiPath($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath())
9293
->setUploadDir($uploadDirectory);
9394
// @todo: Set upload dir to data/temporary

Classes/Tus/PartialUploadFileCacheAdapter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function deleteAll(array $keys): bool
8686
*/
8787
public function getTtl(): int
8888
{
89+
return 60*60*24;
8990
return (int)ObjectAccess::getProperty($this->partialUploadFileCache->getBackend(), 'defaultLifetime', true);
9091
}
9192

Resources/Private/JavaScript/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"license": "GNU GPLv3",
55
"private": true,
66
"dependencies": {
7-
"pubsub-js": "^1.9.3"
7+
"pubsub-js": "^1.9.3",
8+
"tus-js-client": "^2.3.0"
89
},
910
"devDependencies": {
1011
"@types/pubsub-js": "^1.8.2"
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import { useMutation } from '@apollo/client';
1+
import * as tus from 'tus-js-client';
2+
import { UploadOptions } from 'tus-js-client';
23

3-
import { UPLOAD_FILES } from '../mutations';
4-
import { FileUploadResult } from '../interfaces';
4+
export default function useUploadFiles(options: UploadOptions) {
5+
console.log('HI');
6+
console.log(options.endpoint);
7+
const uploadFiles = (files: File[]) => {
8+
files.forEach((file) => {
9+
options.metadata = {
10+
filename: file.name,
11+
filetype: file.type,
12+
};
13+
console.log(options);
14+
const upload = new tus.Upload(file, options);
515

6-
export default function useUploadFiles() {
7-
const [action, { error, data, loading }] = useMutation<{ uploadFiles: FileUploadResult[] }>(UPLOAD_FILES);
8-
9-
const uploadFiles = (files: File[]) =>
10-
action({
11-
variables: {
12-
files,
13-
},
16+
upload.findPreviousUploads().then((previousUploads) => {
17+
if(previousUploads.length) {
18+
upload.resumeFromPreviousUpload(previousUploads[0]);
19+
}
20+
upload.start();
21+
});
1422
});
23+
};
1524

16-
return { uploadFiles, uploadState: data?.uploadFiles || [], error, loading };
25+
return { uploadFiles };
1726
}

Resources/Private/JavaScript/media-module/src/components/Dialogs/UploadDialog.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,29 @@ interface UploadedFile extends File {
124124
}
125125

126126
const UploadDialog: React.FC = () => {
127+
const [loading, setLoading] = useState(false);
128+
const [uploadState, setUploadState] = useState([]);
127129
const { translate } = useIntl();
128130
const Notify = useNotify();
129131
const [dialogVisible, setDialogVisible] = useRecoilState(uploadDialogVisibleState);
130132
const { dummyImage } = useMediaUi();
131133
const { config } = useConfigQuery();
132-
const { uploadFiles, uploadState, loading } = useUploadFiles();
134+
const { uploadFiles } = useUploadFiles({
135+
endpoint: 'http://localhost:8081/neos/management/mediaui/upload',
136+
onError: (error) => {
137+
Notify.error(translate('fileUpload.error', 'Upload failed'), error.message);
138+
},
139+
onSuccess: () => {
140+
Notify.ok(translate('uploadDialog.uploadFinished', 'Upload finished'));
141+
setLoading(false);
142+
refetchAssets();
143+
},
144+
onProgress: (bytesUploaded, bytesTotal) => {
145+
setLoading(true);
146+
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
147+
console.log(percentage);
148+
},
149+
});
133150
const { refetchAssets } = useMediaUi();
134151
const [files, setFiles] = useState<UploadedFile[]>([]);
135152
const uploadPossible = !loading && files.length > 0;
@@ -170,14 +187,7 @@ const UploadDialog: React.FC = () => {
170187

171188
const handleUpload = useCallback(() => {
172189
uploadFiles(files)
173-
.then(() => {
174-
Notify.ok(translate('uploadDialog.uploadFinished', 'Upload finished'));
175-
refetchAssets();
176-
})
177-
.catch((error) => {
178-
Notify.error(translate('fileUpload.error', 'Upload failed'), error);
179-
});
180-
}, [uploadFiles, Notify, translate, files, refetchAssets]);
190+
}, [uploadFiles, files]);
181191

182192
const handleRequestClose = useCallback(() => {
183193
setFiles([]);

Resources/Public/AssetEditor/Plugin.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/Public/AssetEditor/Plugin.js

Lines changed: 64496 additions & 2 deletions
Large diffs are not rendered by default.

Resources/Public/AssetEditor/Plugin.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/Public/Assets/main.bundle.css

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Resources/Public/Assets/main.bundle.js

Lines changed: 83 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)