@@ -31,36 +31,67 @@ window.addEventListener('DOMContentLoaded', async function() {
3131 fileType,
3232 app,
3333 cryptPadUrl,
34+ isShared,
35+ fileName,
3436 } = window . OpenInCryptPadInfo
35- document . title = fileName ( filePath ) + ' - Nextcloud'
3637
37- const sessionKey = await getSessionForFile ( fileId )
38+ let blob
39+ let viewMode = ''
40+ document . title = fileName + ' - Nextcloud'
41+ // if opening file from a share link, we don't get access to the file path, but we can download it
42+ if ( isShared . startsWith ( 'true' ) ) {
43+ viewMode = 'view'
44+ }
45+
46+ if ( isShared . includes ( 'External' ) ) {
47+ blob = await loadFileContentShared ( filePath , mimeType )
48+ } else {
49+ blob = await loadFileContent ( filePath , mimeType )
50+ }
3851
39- const blob = await loadFileContent ( filePath , mimeType )
52+ let viewOnlyMode = false
53+ let sessionKey
54+ try {
55+ sessionKey = await getSessionForFile ( fileId )
56+ } catch ( e ) {
57+ viewOnlyMode = true
58+ }
4059
4160 const docUrl = URL . createObjectURL ( blob )
4261
43- CryptPadAPI ( cryptPadUrl , 'editor-content' , {
44- document : {
45- url : docUrl ,
46- key : sessionKey ,
47- fileType ,
48- } ,
49- documentType : app ,
50- events : {
51- onSave : ( data , cb ) => onSave ( filePath , data , cb ) ,
62+ const events = viewOnlyMode
63+ ? {
64+ onSave : ( data , cb ) => null ,
65+ onNewKey : ( data , cb ) => cb ( data . new ) , // Just accept and ignore any session key CryptPad wants to use
66+ onHasUnsavedChanges : ( unsavedChanges ) => null ,
67+ onInsertImage ,
68+ }
69+ : {
70+ onSave : ( data , cb ) => onSave ( filePath , data , cb , isShared ) ,
5271 onNewKey : ( data , cb ) => updateSessionForFile ( fileId , data , cb ) ,
5372 onHasUnsavedChanges : ( unsavedChanges ) => {
5473 const elem = document . querySelector ( '#unsaved-indicator' )
5574 elem . className = unsavedChanges ? 'visible' : ''
5675 } ,
5776 onInsertImage,
77+ }
78+
79+ CryptPadAPI ( cryptPadUrl , 'editor-content' , {
80+ document : {
81+ url : docUrl ,
82+ key : sessionKey ,
83+ fileType,
5884 } ,
85+ documentType : app ,
86+ mode : viewMode ,
87+ events,
5988 width : '100%' ,
6089 height : '100%' ,
6190 } )
6291
63- checkForPermissionChange ( filePath , ( ) => resetCryptPadSession ( fileId ) )
92+ if ( isShared !== 'trueExternal' ) {
93+ checkForPermissionChange ( filePath , ( ) => resetCryptPadSession ( fileId ) )
94+ }
6495 initBackButton ( )
6596
6697 } catch ( e ) {
@@ -159,18 +190,6 @@ async function getFilePermission(path) {
159190 . join ( )
160191}
161192
162- /**
163- * @param {string } filePath the file path
164- */
165- function fileName ( filePath ) {
166- if ( ! filePath ) {
167- return
168- }
169-
170- const parts = filePath . split ( '/' )
171- return parts [ parts . length - 1 ]
172- }
173-
174193/**
175194 *
176195 * @param {string } filePath the file path
@@ -190,16 +209,41 @@ async function loadFileContent(filePath, mimeType) {
190209 }
191210}
192211
212+ /**
213+ *
214+ * @param {string } downloadPath the download path for the file
215+ * @param {string } mimeType the mime type
216+ */
217+ async function loadFileContentShared ( downloadPath , mimeType ) {
218+ try {
219+ const response = await fetch ( downloadPath )
220+ if ( ! response . ok ) {
221+ throw new Error ( `Failed to fetch file: ${ response . statusText } ` )
222+ }
223+ const blob = await response . blob ( )
224+
225+ return blob
226+ } catch ( e ) {
227+ console . log ( 'MASSIVE ERROR' )
228+ console . log ( e )
229+ throw e [ 1 ]
230+ }
231+ }
232+
193233/**
194234 *
195235 * @param {string } filePath the file path
196236 * @param {Blob } data the data to dave
197237 * @param {Function } cb callback
238+ * @param {string } isShared if file is shared
198239 */
199- function onSave ( filePath , data , cb ) {
200- saveFileContent ( filePath , data )
201- . then ( ( ) => cb ( ) )
202- . catch ( cb )
240+ function onSave ( filePath , data , cb , isShared ) {
241+ if ( isShared === 'false' ) {
242+ saveFileContent ( filePath , data )
243+ . then ( ( ) => cb ( ) )
244+ . catch ( cb )
245+ }
246+ // if it's through a share link, we shouldn't save (read only)
203247}
204248
205249/**
@@ -218,8 +262,10 @@ async function getSessionForFile(fileId) {
218262 if ( response . ok ) {
219263 const body = await response . json ( )
220264 return body . sessionKey
221- } else {
265+ } else if ( response . status === 404 ) {
222266 return null
267+ } else {
268+ throw new Error ( 'no write permission' )
223269 }
224270}
225271
0 commit comments