diff --git a/b0esche_cloud/lib/widgets/share_file_dialog.dart b/b0esche_cloud/lib/widgets/share_file_dialog.dart index 5d774e7..6bb21bb 100644 --- a/b0esche_cloud/lib/widgets/share_file_dialog.dart +++ b/b0esche_cloud/lib/widgets/share_file_dialog.dart @@ -51,12 +51,30 @@ class _ShareFileDialogState extends State { _isLoading = false; }); } catch (e) { - // If 404 -> no share link yet. Otherwise surface error. if (e is ApiError && e.status == 404) { + // No link exists, create one automatically setState(() { - _shareUrl = null; - _isLoading = false; + _isLoading = true; // Keep loading for creation }); + try { + final apiClient = getIt(); + final path = widget.orgId.isEmpty || widget.orgId == 'personal' + ? '/orgs/files/${widget.fileId}/share' + : '/orgs/${widget.orgId}/files/${widget.fileId}/share'; + final response = await apiClient.postRaw(path, data: {}); + + setState(() { + _shareUrl = response['shareUrl']; + _isLoading = false; + }); + } catch (createError) { + setState(() { + _error = createError is ApiError + ? createError.message + : 'Failed to create share link'; + _isLoading = false; + }); + } return; } setState(() { diff --git a/go_cloud/internal/http/routes.go b/go_cloud/internal/http/routes.go index 7b89ead..faaae21 100644 --- a/go_cloud/internal/http/routes.go +++ b/go_cloud/internal/http/routes.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "io" + "log" "mime/multipart" "net/http" "net/url" @@ -2822,6 +2823,8 @@ func createFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *data return } + log.Printf("Share link created: user_id=%s, file_id=%s, org_id=%v", userID, fileUUID, link.OrgID) + // Build full URL scheme := "https" if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" { @@ -3154,6 +3157,8 @@ func createUserFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db * return } + log.Printf("Share link created: user_id=%s, file_id=%s, org_id=%v", userID, fileUUID, link.OrgID) + // Build full URL scheme := "https" if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" { diff --git a/go_cloud/migrations/0009_file_share_links_org_id_nullable.sql b/go_cloud/migrations/0009_file_share_links_org_id_nullable.sql new file mode 100644 index 0000000..b8504f2 --- /dev/null +++ b/go_cloud/migrations/0009_file_share_links_org_id_nullable.sql @@ -0,0 +1,3 @@ +-- Make org_id nullable in file_share_links for personal file sharing + +ALTER TABLE file_share_links ALTER COLUMN org_id DROP NOT NULL; \ No newline at end of file diff --git a/go_cloud/migrations/0009_file_share_links_org_id_nullable_down.sql b/go_cloud/migrations/0009_file_share_links_org_id_nullable_down.sql new file mode 100644 index 0000000..3ed8bd3 --- /dev/null +++ b/go_cloud/migrations/0009_file_share_links_org_id_nullable_down.sql @@ -0,0 +1,3 @@ +-- Revert: Make org_id not nullable in file_share_links + +ALTER TABLE file_share_links ALTER COLUMN org_id SET NOT NULL; \ No newline at end of file