1
- import signInGoDAM from "./signInGoDAM" ;
2
-
3
- // GoDAM OAuth configuration
4
- const clientId = process . env . GODAM_OAUTH_CLIENT_ID ;
1
+ import getGoDAMAuthToken from "./getGoDAMAuthToken" ;
5
2
6
3
// Function to upload a video to GoDAM
7
4
const saveToGoDAM = async ( videoBlob , fileName , sendResponse ) => {
8
- // Function to get an access token from Chrome storage
9
- async function getGoDAMAuthToken ( ) {
10
- return new Promise ( ( resolve , reject ) => {
11
- chrome . storage . local . get ( [ "godamToken" , "godamRefreshToken" , "godamTokenExpiration" ] , async ( result ) => {
12
- if ( chrome . runtime . lastError ) {
13
- reject ( new Error ( chrome . runtime . lastError ) ) ;
14
- } else {
15
- const { godamToken, godamRefreshToken, godamTokenExpiration } = result ;
16
-
17
- if ( ! godamToken ) {
18
- // Token is not set, trigger sign-in
19
- const newToken = await signInGoDAM ( ) ;
20
- if ( ! newToken ) {
21
- // Sign-in failed, throw an error
22
- reject ( new Error ( "GoDAM sign-in failed" ) ) ;
5
+ try {
6
+ const token = await getGoDAMAuthToken ( ) ;
7
+ const formData = new FormData ( ) ;
8
+ formData . append ( 'file' , videoBlob , fileName ) ;
9
+
10
+ const uploadUrl = process . env . GODAM_UPLOAD_URL || 'https://upload.godam.io' ; // Todo: Replace the option with production URL
11
+
12
+ const url = uploadUrl + '/upload-file' ;
13
+
14
+ const uploadResponse = await fetch (
15
+ url ,
16
+ {
17
+ method : "POST" ,
18
+ headers : {
19
+ Authorization : `Bearer ${ token } ` ,
20
+ } ,
21
+ body : formData ,
23
22
}
24
- resolve ( newToken ) ;
25
- } else {
26
- // Token is set, check if it has expired
27
- const currentTime = Date . now ( ) ;
28
- if ( currentTime >= godamTokenExpiration ) {
29
- // Token has expired, refresh it
30
- const baseUrl = process . env . GODAM_BASE_URL || 'https://app.godam.io' ;
31
- try {
32
- const refreshResponse = await fetch ( `${ baseUrl } /api/method/frappe.integrations.oauth2.get_token` , {
33
- method : 'POST' ,
34
- headers : {
35
- 'Content-Type' : 'application/x-www-form-urlencoded' ,
36
- 'Accept' : 'application/json' ,
37
- } ,
38
- credentials : 'omit' ,
39
- body : new URLSearchParams ( {
40
- grant_type : 'authorization_code' ,
41
- refresh_token : godamRefreshToken ,
42
- client_id : clientId ,
43
- } ) ,
44
- } ) ;
45
-
46
- if ( ! refreshResponse . ok ) {
47
- // Refresh token failed, try to sign in again
48
- const newToken = await signInGoDAM ( ) ;
49
- if ( ! newToken ) {
50
- reject ( new Error ( "GoDAM token refresh and sign-in failed" ) ) ;
51
- }
52
- resolve ( newToken ) ;
53
- return ;
54
- }
55
-
56
- const tokenData = await refreshResponse . json ( ) ;
57
- const newToken = tokenData . access_token ;
58
- const expiresIn = tokenData . expires_in || 3600 ;
59
- const expirationTime = Date . now ( ) + expiresIn * 1000 ;
23
+ ) ;
60
24
61
- // Save new token to storage
62
- await new Promise ( ( resolve ) =>
63
- chrome . storage . local . set ( {
64
- godamToken : newToken ,
65
- godamRefreshToken : tokenData . refresh_token || godamRefreshToken ,
66
- godamTokenExpiration : expirationTime
67
- } , ( ) => resolve ( ) )
68
- ) ;
69
-
70
- resolve ( newToken ) ;
71
- } catch ( error ) {
72
- // If refresh fails, try to sign in again
73
- const newToken = await signInGoDAM ( ) ;
74
- if ( ! newToken ) {
75
- reject ( new Error ( "GoDAM token refresh and sign-in failed" ) ) ;
76
- }
77
- resolve ( newToken ) ;
78
- }
25
+ let message = 'An error occurred while saving to GoDAM!' ;
26
+ if ( ! uploadResponse . ok ) {
27
+ // error message
28
+ if ( uploadResponse . status === 400 ) {
29
+ message = 'An error occurred while saving to GoDAM! <br> Looks like you are not logged in to GoDAM. Please log in again.' ;
79
30
} else {
80
- // Token is still valid
81
- resolve ( godamToken ) ;
31
+ message = 'An error occurred while saving to GoDAM! <br> Please try again later, if the problem persists, please contact <a href="https://app.godam.io/helpdesk/my-tickets" target="blank">support team</a>.' ;
82
32
}
83
- }
33
+ throw new Error ( message ) ;
84
34
}
85
- } ) ;
86
- } ) ;
87
- }
88
-
89
- return new Promise ( async ( resolve , reject ) => {
90
- try {
91
- // Get the access token from Chrome storage
92
- let token = await getGoDAMAuthToken ( ) ;
93
35
94
- if ( ! token ) {
95
- throw new Error ( "GoDAM sign-in failed" ) ;
96
- }
97
-
98
- const formData = new FormData ( ) ;
99
- formData . append ( 'file' , videoBlob , fileName ) ;
36
+ const responseData = await uploadResponse . json ( ) ;
37
+ const videoName = responseData ?. file_informations ?. name ;
100
38
101
- const uploadUrl = process . env . GODAM_UPLOAD_URL || 'https://upload .godam.io' ; // Todo: Replace the option with production URL
39
+ const baseURL = process . env . GODAM_BASE_URL || 'https://app .godam.io' ;
102
40
103
- const url = uploadUrl + '/upload-file' ;
41
+ sendResponse ( { status : "ok" , url : ` ${ baseURL } /web/video/ ${ videoName } ` } ) ;
104
42
105
- const uploadResponse = await fetch (
106
- url ,
107
- {
108
- method : "POST" ,
109
- headers : {
110
- Authorization : `Bearer ${ token } ` ,
111
- } ,
112
- body : formData ,
113
- }
114
- ) ;
43
+ } catch ( error ) {
44
+ sendResponse ( { status : "error" , url : null , message : error . message } ) ;
115
45
116
- let message = 'An error occurred while saving to GoDAM!' ;
117
- if ( ! uploadResponse . ok ) {
118
- // error message
119
- if ( uploadResponse . status === 400 ) {
120
- message = 'An error occurred while saving to GoDAM! <br> Looks like you are not logged in to GoDAM. Please log in again.' ;
121
- } else {
122
- message = 'An error occurred while saving to GoDAM! <br> Please try again later, if the problem persists, please contact <a href="https://app.godam.io/helpdesk/my-tickets" target="blank">support team</a>.' ;
123
- }
124
- throw new Error ( message ) ;
125
- }
46
+ }
126
47
127
- const responseData = await uploadResponse . json ( ) ;
128
- const filename = responseData . filename ;
129
- const location = responseData . location ;
130
- const videoName = responseData ?. file_informations ?. name ;
131
48
132
- const baseURL = process . env . GODAM_BASE_URL || 'https://app.godam.io' ;
133
-
134
- sendResponse ( { status : "ok" , url : `${ baseURL } /web/video/${ videoName } ` } ) ;
135
49
136
- resolve ( `${ baseURL } /${ location } ` ) ;
137
- } catch ( error ) {
138
- sendResponse ( { status : "error" , url : null , message : error . message } ) ;
139
- reject ( error ) ;
140
- }
141
- } ) ;
142
50
} ;
143
51
144
52
export default saveToGoDAM ;
0 commit comments