1
+ import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js' ;
2
+ import pathLib from '../../../lib/path.js' ;
3
+
4
+ // This only works for absolute symlinks for now
5
+ const symlink = async function ( target , linkPath ) {
6
+
7
+
8
+ // If auth token is not provided and we are in the web environment,
9
+ // try to authenticate with Puter
10
+ if ( ! puter . authToken && puter . env === 'web' ) {
11
+ try {
12
+ await puter . ui . authenticateWithPuter ( ) ;
13
+ } catch ( e ) {
14
+ // if authentication fails, throw an error
15
+ throw 'Authentication failed.' ;
16
+ }
17
+ }
18
+
19
+ // convert path to absolute path
20
+ linkPath = getAbsolutePathForApp ( linkPath ) ;
21
+ target = getAbsolutePathForApp ( target ) ;
22
+ const name = pathLib . basename ( linkPath ) ;
23
+ const linkDir = pathLib . dirname ( linkPath )
24
+
25
+ const op =
26
+ {
27
+ op : 'symlink' ,
28
+ path : linkDir ,
29
+ name : name ,
30
+ target : target
31
+ } ;
32
+
33
+ const formData = new FormData ( ) ;
34
+ formData . append ( 'operation' , JSON . stringify ( op ) ) ;
35
+
36
+ try {
37
+ const response = await fetch ( this . APIOrigin + "/batch" , {
38
+ method : 'POST' ,
39
+ headers : { 'Authorization' : `Bearer ${ puter . authToken } ` } ,
40
+ body : formData
41
+ } ) ;
42
+ if ( response . status !== 200 ) {
43
+ const error = await response . text ( ) ;
44
+ console . error ( "[symlink] fetch error: " , error ) ;
45
+ throw error ;
46
+ }
47
+ } catch ( e ) {
48
+ console . error ( "[symlink] fetch error: " , e ) ;
49
+ throw e ;
50
+ }
51
+
52
+
53
+ }
54
+
55
+ export default symlink ;
0 commit comments