Enhance file sharing functionality: infer org_id when not provided, update share link responses to include shareUrl
This commit is contained in:
@@ -1157,6 +1157,18 @@ func (db *DB) CreateFileShareLink(ctx context.Context, token string, fileID uuid
|
||||
var link models.FileShareLink
|
||||
var expiresAtNull sql.NullTime
|
||||
var orgIDNull sql.NullString
|
||||
// If caller didn't provide an orgID, try to infer it from the file record
|
||||
if orgID == nil {
|
||||
var fileOrgNull sql.NullString
|
||||
fileErr := db.QueryRowContext(ctx, `SELECT org_id::text FROM files WHERE id = $1`, fileID).Scan(&fileOrgNull)
|
||||
if fileErr == nil && fileOrgNull.Valid {
|
||||
if parsed, perr := uuid.Parse(fileOrgNull.String); perr == nil {
|
||||
orgID = &parsed
|
||||
}
|
||||
}
|
||||
// If the file lookup failed or org_id is not set, orgID remains nil
|
||||
}
|
||||
|
||||
err := db.QueryRowContext(ctx, `
|
||||
INSERT INTO file_share_links (token, file_id, org_id, created_by_user_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
|
||||
@@ -2748,10 +2748,7 @@ func getFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *databas
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
// No share link exists
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"exists": false,
|
||||
})
|
||||
errors.WriteError(w, errors.CodeNotFound, "Share link not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
errors.LogError(r, err, "Failed to get share link")
|
||||
@@ -2774,9 +2771,8 @@ func getFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *databas
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"exists": true,
|
||||
"url": fullURL,
|
||||
"token": link.Token,
|
||||
"shareUrl": fullURL,
|
||||
"token": link.Token,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2812,7 +2808,7 @@ func createFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *data
|
||||
db.RevokeFileShareLink(r.Context(), fileUUID) // Ignore error
|
||||
|
||||
// Generate token
|
||||
token, err := storage.GenerateSecurePassword(32)
|
||||
token, err := storage.GenerateSecurePassword(48)
|
||||
if err != nil {
|
||||
errors.LogError(r, err, "Failed to generate token")
|
||||
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
|
||||
@@ -2841,8 +2837,8 @@ func createFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *data
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"url": fullURL,
|
||||
"token": link.Token,
|
||||
"shareUrl": fullURL,
|
||||
"token": link.Token,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3087,10 +3083,7 @@ func getUserFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *dat
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
// No share link exists
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"exists": false,
|
||||
})
|
||||
errors.WriteError(w, errors.CodeNotFound, "Share link not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
errors.LogError(r, err, "Failed to get share link")
|
||||
@@ -3113,9 +3106,8 @@ func getUserFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *dat
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"exists": true,
|
||||
"url": fullURL,
|
||||
"token": link.Token,
|
||||
"shareUrl": fullURL,
|
||||
"token": link.Token,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3146,13 +3138,15 @@ func createUserFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *
|
||||
db.RevokeFileShareLink(r.Context(), fileUUID) // Ignore error
|
||||
|
||||
// Generate token
|
||||
token, err := storage.GenerateSecurePassword(32)
|
||||
token, err := storage.GenerateSecurePassword(48)
|
||||
if err != nil {
|
||||
errors.LogError(r, err, "Failed to generate token")
|
||||
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// If the file belongs to an org, prefer binding the share link to that org.
|
||||
// db.CreateFileShareLink will attempt to infer org_id from the file if nil is passed.
|
||||
link, err := db.CreateFileShareLink(r.Context(), token, fileUUID, nil, userID)
|
||||
if err != nil {
|
||||
errors.LogError(r, err, "Failed to create share link")
|
||||
@@ -3175,8 +3169,8 @@ func createUserFileShareLinkHandler(w http.ResponseWriter, r *http.Request, db *
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"url": fullURL,
|
||||
"token": link.Token,
|
||||
"shareUrl": fullURL,
|
||||
"token": link.Token,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user