@@ -53,7 +53,7 @@ import { UploadStatus } from '@nextcloud/upload'
5353import TrayArrowDownIcon from ' vue-material-design-icons/TrayArrowDown.vue'
5454
5555import logger from ' ../logger.js'
56- import { handleDrop } from ' ../services/DropService'
56+ import { dataTransferToFileTree , handleDrop , onDropExternalFiles } from ' ../services/DropService'
5757
5858export default defineComponent ({
5959 name: ' DragAndDropNotice' ,
@@ -76,6 +76,10 @@ export default defineComponent({
7676 },
7777
7878 computed: {
79+ currentView() {
80+ return this .$navigation .active
81+ },
82+
7983 /**
8084 * Check if the current folder has create permissions
8185 */
@@ -146,8 +150,6 @@ export default defineComponent({
146150 },
147151
148152 async onDrop(event : DragEvent ) {
149- logger .debug (' Dropped on DragAndDropNotice' , { event })
150-
151153 // cantUploadLabel is null if we can upload
152154 if (this .cantUploadLabel ) {
153155 showError (this .cantUploadLabel )
@@ -161,38 +163,50 @@ export default defineComponent({
161163 event .preventDefault ()
162164 event .stopPropagation ()
163165
164- if (event .dataTransfer && event .dataTransfer .items .length > 0 ) {
165- // Start upload
166- logger .debug (` Uploading files to ${this .currentFolder .path } ` )
167- // Process finished uploads
168- const uploads = await handleDrop (event .dataTransfer )
169- logger .debug (' Upload terminated' , { uploads })
170-
171- if (uploads .some ((upload ) => upload .status === UploadStatus .FAILED )) {
172- showError (t (' files' , ' Some files could not be uploaded' ))
173- const failedUploads = uploads .filter ((upload ) => upload .status === UploadStatus .FAILED )
174- logger .debug (' Some files could not be uploaded' , { failedUploads })
175- } else {
176- showSuccess (t (' files' , ' Files uploaded successfully' ))
177- }
178-
179- // Scroll to last successful upload in current directory if terminated
180- const lastUpload = uploads .findLast ((upload ) => upload .status !== UploadStatus .FAILED
181- && ! upload .file .webkitRelativePath .includes (' /' )
182- && upload .response ?.headers ?.[' oc-fileid' ])
183-
184- if (lastUpload !== undefined ) {
185- this .$router .push ({
186- ... this .$route ,
187- params: {
188- view: this .$route .params ?.view ?? ' files' ,
189- fileid: parseInt (lastUpload .response ! .headers [' oc-fileid' ]),
190- },
191- })
192- }
166+ // Caching the selection
167+ const items = [... event .dataTransfer ?.items || []] as DataTransferItem []
168+
169+ // We need to process the dataTransfer ASAP before the
170+ // browser clears it. This is why we cache the items too.
171+ const fileTree = await dataTransferToFileTree (items )
172+
173+ // We might not have the target directory fetched yet
174+ const contents = await this .currentView ?.getContents (this .currentFolder .path )
175+ const folder = contents ?.folder
176+ if (! folder ) {
177+ showError (this .t (' files' , ' Target folder does not exist any more' ))
178+ return
179+ }
180+
181+ // If another button is pressed, cancel it. This
182+ // allows cancelling the drag with the right click.
183+ if (event .button !== 0 ) {
184+ return
185+ }
186+
187+ logger .debug (' Dropped' , { event , folder , fileTree })
188+
189+ // Check whether we're uploading files
190+ const uploads = await onDropExternalFiles (fileTree , folder , contents .contents )
191+
192+ // Scroll to last successful upload in current directory if terminated
193+ const lastUpload = uploads .findLast ((upload ) => upload .status !== UploadStatus .FAILED
194+ && ! upload .file .webkitRelativePath .includes (' /' )
195+ && upload .response ?.headers ?.[' oc-fileid' ])
196+
197+ if (lastUpload !== undefined ) {
198+ this .$router .push ({
199+ ... this .$route ,
200+ params: {
201+ view: this .$route .params ?.view ?? ' files' ,
202+ fileid: parseInt (lastUpload .response ! .headers [' oc-fileid' ]),
203+ },
204+ })
193205 }
206+
194207 this .dragover = false
195208 },
209+
196210 t ,
197211 },
198212})
0 commit comments