@@ -11,6 +11,7 @@ import json from 'jonny';
1111import * as ponse from 'ponse' ;
1212import { copymitter } from 'copymitter' ;
1313import { moveFiles as _moveFiles } from '@cloudcmd/move-files' ;
14+ import { tryCatch } from 'try-catch' ;
1415import * as CloudFunc from '#common/cloudfunc' ;
1516import root from '../root.js' ;
1617import markdown from '../markdown/index.js' ;
@@ -216,8 +217,15 @@ function onPUT({name, fs, moveFiles, config, body}, callback) {
216217 const msg = getMoveMsg ( names ) ;
217218 const fn = swap ( callback , msg ) ;
218219
219- const fromRooted = root ( from , rootDir ) ;
220- const toRooted = root ( to , rootDir ) ;
220+ const [ errorFrom , fromRooted ] = tryCatch ( root . resolve , from , rootDir ) ;
221+
222+ if ( errorFrom )
223+ return callback ( errorFrom ) ;
224+
225+ const [ errorTo , toRooted ] = tryCatch ( root . resolve , to , rootDir ) ;
226+
227+ if ( errorTo )
228+ return callback ( errorTo ) ;
221229
222230 return moveFiles ( fromRooted , toRooted , names )
223231 . on ( 'error' , fn )
@@ -227,21 +235,32 @@ function onPUT({name, fs, moveFiles, config, body}, callback) {
227235 case 'rename' :
228236 return rename ( rootDir , files . from , files . to , fs , callback ) ;
229237
230- case 'copy' :
238+ case 'copy' : {
231239 if ( ! files . from || ! files . names || ! files . to )
232240 return callback ( body ) ;
233241
234242 if ( isRootAll ( rootDir , [ files . to , files . from ] ) )
235243 return callback ( getWin32RootMsg ( ) ) ;
236244
237- files . from = root ( files . from , rootDir ) ;
238- files . to = root ( files . to , rootDir ) ;
245+ const [ errorFrom , resolvedFrom ] = tryCatch ( root . resolve , files . from , rootDir ) ;
246+
247+ if ( errorFrom )
248+ return callback ( errorFrom ) ;
249+
250+ const [ errorTo , resolvedTo ] = tryCatch ( root . resolve , files . to , rootDir ) ;
251+
252+ if ( errorTo )
253+ return callback ( errorTo ) ;
254+
255+ files . from = resolvedFrom ;
256+ files . to = resolvedTo ;
239257
240258 copy ( files . from , files . to , files . names , ( error ) => {
241259 const msg = formatMsg ( 'copy' , files . names ) ;
242260 callback ( error , msg ) ;
243261 } ) ;
244262 break ;
263+ }
245264
246265 case 'pack' :
247266 if ( ! files . from )
@@ -274,8 +293,15 @@ function rename(rootDir, from, to, fs, callback) {
274293 const msg = getRenameMsg ( from , to ) ;
275294 const fn = swap ( callback , msg ) ;
276295
277- const fromRooted = root ( from , rootDir ) ;
278- const toRooted = root ( to , rootDir ) ;
296+ const [ errorFrom , fromRooted ] = tryCatch ( root . resolve , from , rootDir ) ;
297+
298+ if ( errorFrom )
299+ return callback ( errorFrom ) ;
300+
301+ const [ errorTo , toRooted ] = tryCatch ( root . resolve , to , rootDir ) ;
302+
303+ if ( errorTo )
304+ return callback ( errorTo ) ;
279305
280306 return fs . rename ( fromRooted , toRooted , fn ) ;
281307}
@@ -286,8 +312,18 @@ function pack(from, to, names, config, fn) {
286312 const rootDir = config ( 'root' ) ;
287313 const packer = config ( 'packer' ) ;
288314
289- from = root ( from , rootDir ) ;
290- to = root ( to , rootDir ) ;
315+ const [ errorFrom , resolvedFrom ] = tryCatch ( root . resolve , from , rootDir ) ;
316+
317+ if ( errorFrom )
318+ return fn ( errorFrom ) ;
319+
320+ const [ errorTo , resolvedTo ] = tryCatch ( root . resolve , to , rootDir ) ;
321+
322+ if ( errorTo )
323+ return fn ( errorTo ) ;
324+
325+ from = resolvedFrom ;
326+ to = resolvedTo ;
291327
292328 if ( ! names ) {
293329 names = [
@@ -305,12 +341,23 @@ export const _pack = pack;
305341function extract ( from , to , config , fn ) {
306342 const rootDir = config ( 'root' ) ;
307343
308- from = root ( from , rootDir ) ;
344+ const [ errorFrom , resolvedFrom ] = tryCatch ( root . resolve , from , rootDir ) ;
309345
310- if ( to )
311- to = root ( to , rootDir ) ;
312- else
346+ if ( errorFrom )
347+ return fn ( errorFrom ) ;
348+
349+ from = resolvedFrom ;
350+
351+ if ( to ) {
352+ const [ errorTo , resolvedTo ] = tryCatch ( root . resolve , to , rootDir ) ;
353+
354+ if ( errorTo )
355+ return fn ( errorTo ) ;
356+
357+ to = resolvedTo ;
358+ } else {
313359 to = from . replace ( / \. t a r \. g z $ / , '' ) ;
360+ }
314361
315362 operation ( 'extract' , config ( 'packer' ) , from , to , fn ) ;
316363}
0 commit comments