File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed
blocks/browse/da-list/helpers
test/unit/blocks/browse/helpers Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -81,18 +81,25 @@ export async function getFullEntryList(entries) {
81
81
return files . filter ( ( file ) => file ) ;
82
82
}
83
83
84
+ export function sanitizePath ( path ) {
85
+ const pathArray = path . split ( '/' ) ;
86
+ const sanitizedArray = pathArray . map ( ( element ) => element . replaceAll ( / [ ^ a - z A - Z 0 - 9 . ] / g, '-' ) . toLowerCase ( ) ) ;
87
+ return [ ...sanitizedArray ] . join ( '/' ) ;
88
+ }
89
+
84
90
export async function handleUpload ( list , fullpath , file ) {
85
91
const { data, path } = file ;
86
92
const formData = new FormData ( ) ;
87
93
formData . append ( 'data' , data ) ;
88
94
const opts = { method : 'POST' , body : formData } ;
89
- const postpath = `${ fullpath } ${ path } ` ;
95
+ const sanitizedPath = sanitizePath ( path ) ;
96
+ const postpath = `${ fullpath } ${ sanitizedPath } ` ;
90
97
91
98
try {
92
99
await daFetch ( `${ DA_ORIGIN } /source${ postpath } ` , opts ) ;
93
100
file . imported = true ;
94
101
95
- const [ displayName ] = path . split ( '/' ) . slice ( 1 ) ;
102
+ const [ displayName ] = sanitizedPath . split ( '/' ) . slice ( 1 ) ;
96
103
const [ filename , ...rest ] = displayName . split ( '.' ) ;
97
104
const ext = rest . pop ( ) ;
98
105
const rejoined = [ filename , ...rest ] . join ( '.' ) ;
Original file line number Diff line number Diff line change 1
1
import { expect } from '@esm-bundle/chai' ;
2
2
import { stub } from 'sinon' ;
3
- import { getFullEntryList , handleUpload } from '../../../../../blocks/browse/da-list/helpers/drag-n-drop.js' ;
3
+ import { getFullEntryList , handleUpload , sanitizePath } from '../../../../../blocks/browse/da-list/helpers/drag-n-drop.js' ;
4
4
5
5
const goodEntry = {
6
6
isDirectory : false ,
@@ -81,4 +81,11 @@ describe('Upload and format', () => {
81
81
const item = await handleUpload ( list , fullpath , packagedFile ) ;
82
82
expect ( item ) . to . exist ;
83
83
} ) ;
84
+
85
+ it ( 'Returns sanitize file path' , async ( ) => {
86
+ const path = '/new folder/geo_metrixx.jpg' ;
87
+ const item = sanitizePath ( path ) ;
88
+ console . log ( 'item' , item ) ;
89
+ expect ( item ) . to . equal ( '/new-folder/geo-metrixx.jpg' ) ;
90
+ } ) ;
84
91
} ) ;
You can’t perform that action at this time.
0 commit comments