From ec25f06ea3babd824b17615fc83be40878b2f8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6sche?= Date: Mon, 12 Jan 2026 01:18:08 +0100 Subject: [PATCH] Fix: Refactor code for improved readability and consistency in document viewer and signup form --- b0esche_cloud/lib/pages/document_viewer.dart | 74 +++++++++----------- b0esche_cloud/lib/pages/signup_form.dart | 1 - go_cloud/internal/database/db.go | 1 + go_cloud/internal/http/wopi_handlers.go | 50 ++++++------- 4 files changed, 59 insertions(+), 67 deletions(-) diff --git a/b0esche_cloud/lib/pages/document_viewer.dart b/b0esche_cloud/lib/pages/document_viewer.dart index 4ccc8ae..b3c6307 100644 --- a/b0esche_cloud/lib/pages/document_viewer.dart +++ b/b0esche_cloud/lib/pages/document_viewer.dart @@ -363,11 +363,7 @@ class _DocumentViewerModalState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Icon( - Icons.error_outline, - size: 64, - color: Colors.red[400], - ), + Icon(Icons.error_outline, size: 64, color: Colors.red[400]), const SizedBox(height: 16), Text( 'Failed to load document', @@ -404,10 +400,11 @@ class _DocumentViewerModalState extends State { } final wopiSession = snapshot.data!; - + // Build Collabora Online viewer URL with WOPISrc - final collaboraUrl = 'https://of.b0esche.cloud/loleaflet/dist/loleaflet.html?WOPISrc=${Uri.encodeComponent(wopiSession.wopisrc)}'; - + final collaboraUrl = + 'https://of.b0esche.cloud/loleaflet/dist/loleaflet.html?WOPISrc=${Uri.encodeComponent(wopiSession.wopisrc)}'; + // Use WebView to display Collabora Online return _buildWebView(collaboraUrl); }, @@ -418,7 +415,7 @@ class _DocumentViewerModalState extends State { try { // Use default base URL from backend String baseUrl = 'https://go.b0esche.cloud'; - + // Determine endpoint based on whether we're in org or user workspace String endpoint; if (widget.orgId.isNotEmpty && widget.orgId != 'personal') { @@ -426,14 +423,16 @@ class _DocumentViewerModalState extends State { } else { endpoint = '/user/files/${widget.fileId}/wopi-session'; } - - final response = await http.post( - Uri.parse('$baseUrl$endpoint'), - headers: { - 'Authorization': 'Bearer $token', - 'Content-Type': 'application/json', - }, - ).timeout(const Duration(seconds: 10)); + + final response = await http + .post( + Uri.parse('$baseUrl$endpoint'), + headers: { + 'Authorization': 'Bearer $token', + 'Content-Type': 'application/json', + }, + ) + .timeout(const Duration(seconds: 10)); if (response.statusCode == 200) { final json = jsonDecode(response.body) as Map; @@ -442,7 +441,9 @@ class _DocumentViewerModalState extends State { accessToken: json['access_token'] as String, ); } else { - throw Exception('Failed to create WOPI session: ${response.statusCode}'); + throw Exception( + 'Failed to create WOPI session: ${response.statusCode}', + ); } } catch (e) { throw Exception('Error creating WOPI session: $e'); @@ -459,7 +460,11 @@ class _DocumentViewerModalState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - const Icon(Icons.description, size: 64, color: AppTheme.accentColor), + const Icon( + Icons.description, + size: 64, + color: AppTheme.accentColor, + ), const SizedBox(height: 16), Text( 'Collabora Online Viewer', @@ -476,7 +481,9 @@ class _DocumentViewerModalState extends State { onPressed: () { // You can implement opening in browser or using webview here ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('Collabora viewer loading...')), + const SnackBar( + content: Text('Collabora viewer loading...'), + ), ); }, child: const Text('Open Document'), @@ -500,10 +507,7 @@ class WOPISession { final String wopisrc; final String accessToken; - WOPISession({ - required this.wopisrc, - required this.accessToken, - }); + WOPISession({required this.wopisrc, required this.accessToken}); factory WOPISession.fromJson(Map json) { return WOPISession( @@ -792,18 +796,14 @@ class _DocumentViewerState extends State { // Build HTML to embed Collabora Online viewer // For now, we'll show the document download option with a link to open in Collabora // A proper implementation would require WOPI protocol support - + return Container( color: AppTheme.primaryBackground, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Icon( - Icons.description, - size: 64, - color: AppTheme.accentColor, - ), + Icon(Icons.description, size: 64, color: AppTheme.accentColor), const SizedBox(height: 16), Text( 'Office Document', @@ -816,24 +816,16 @@ class _DocumentViewerState extends State { const SizedBox(height: 8), Text( 'Collabora Online Viewer', - style: TextStyle( - color: AppTheme.secondaryText, - fontSize: 14, - ), + style: TextStyle(color: AppTheme.secondaryText, fontSize: 14), ), const SizedBox(height: 16), Text( 'Opening document in Collabora...', - style: TextStyle( - color: AppTheme.secondaryText, - fontSize: 12, - ), + style: TextStyle(color: AppTheme.secondaryText, fontSize: 12), ), const SizedBox(height: 24), CircularProgressIndicator( - valueColor: AlwaysStoppedAnimation( - AppTheme.accentColor, - ), + valueColor: AlwaysStoppedAnimation(AppTheme.accentColor), ), const SizedBox(height: 24), ElevatedButton.icon( diff --git a/b0esche_cloud/lib/pages/signup_form.dart b/b0esche_cloud/lib/pages/signup_form.dart index 4f3cfbe..b153a6a 100644 --- a/b0esche_cloud/lib/pages/signup_form.dart +++ b/b0esche_cloud/lib/pages/signup_form.dart @@ -29,7 +29,6 @@ class _SignupFormState extends State { super.dispose(); } - String _generateRandomBase64(int bytes) { final random = Random(); final values = List.generate(bytes, (i) => random.nextInt(256)); diff --git a/go_cloud/internal/database/db.go b/go_cloud/internal/database/db.go index 0b0ce39..2ed8330 100644 --- a/go_cloud/internal/database/db.go +++ b/go_cloud/internal/database/db.go @@ -674,4 +674,5 @@ func (db *DB) MarkChallengeUsed(ctx context.Context, challenge []byte) error { `, challenge) return err } + // UpdateFileSize updates the size and last_modified timestamp of a file diff --git a/go_cloud/internal/http/wopi_handlers.go b/go_cloud/internal/http/wopi_handlers.go index 1094202..d722a83 100644 --- a/go_cloud/internal/http/wopi_handlers.go +++ b/go_cloud/internal/http/wopi_handlers.go @@ -183,32 +183,32 @@ func wopiCheckFileInfoHandler(w http.ResponseWriter, r *http.Request, db *databa // Build response response := models.WOPICheckFileInfoResponse{ - BaseFileName: file.Name, - Size: file.Size, - Version: file.ID.String(), - OwnerId: ownerID, - UserId: userID.String(), - UserFriendlyName: "", // Could be populated from user info - UserCanWrite: true, - UserCanRename: false, - UserCanNotWriteRelative: false, - ReadOnly: false, - RestrictedWebViewOnly: false, - UserCanCreateRelativeToFolder: false, - EnableOwnerTermination: false, - SupportsUpdate: true, - SupportsCobalt: false, - SupportsLocks: true, - SupportsExtendedLockLength: false, - SupportsGetLock: true, - SupportsDelete: false, - SupportsRename: false, + BaseFileName: file.Name, + Size: file.Size, + Version: file.ID.String(), + OwnerId: ownerID, + UserId: userID.String(), + UserFriendlyName: "", // Could be populated from user info + UserCanWrite: true, + UserCanRename: false, + UserCanNotWriteRelative: false, + ReadOnly: false, + RestrictedWebViewOnly: false, + UserCanCreateRelativeToFolder: false, + EnableOwnerTermination: false, + SupportsUpdate: true, + SupportsCobalt: false, + SupportsLocks: true, + SupportsExtendedLockLength: false, + SupportsGetLock: true, + SupportsDelete: false, + SupportsRename: false, SupportsRenameRelativeToFolder: false, - SupportsFolders: false, - SupportsScenarios: []string{"default"}, - LastModifiedTime: file.LastModified.UTC().Format(time.RFC3339), - IsAnonymousUser: false, - TimeZone: "UTC", + SupportsFolders: false, + SupportsScenarios: []string{"default"}, + LastModifiedTime: file.LastModified.UTC().Format(time.RFC3339), + IsAnonymousUser: false, + TimeZone: "UTC", } fmt.Printf("[WOPI-REQUEST] CheckFileInfo: file=%s user=%s size=%d\n", fileID, userID.String(), file.Size)