Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow date-only expiration for public shares #918

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,12 @@ func (h *Handler) createPublicLinkShare(w http.ResponseWriter, r *http.Request)
},
}

var expireTime time.Time
expireDate := r.FormValue("expireDate")
if expireDate != "" {
expireTime, err = time.Parse("2006-01-02T15:04:05Z0700", expireDate)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "invalid date format", err)
return
}
req.Grant.Expiration = &types.Timestamp{
Nanos: uint32(expireTime.UnixNano()),
Seconds: uint64(expireTime.Unix()),
}
expireTime, err := expirationTimestampFromRequest(r, h)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "invalid date format", err)
}
if expireTime != nil {
req.Grant.Expiration = expireTime
}

// set displayname and password protected as arbitrary metadata
Expand Down Expand Up @@ -1455,11 +1449,14 @@ func (h *Handler) updatePublicShare(w http.ResponseWriter, r *http.Request, toke
}

// ExpireDate
newExpiration := expirationTimestampFromRequest(r, h)
newExpiration, err := expirationTimestampFromRequest(r, h)
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "invalid date format", err)
}
beforeExpiration, _ := json.Marshal(before.Share.Expiration)
afterExpiration, _ := json.Marshal(newExpiration)
if newExpiration != nil || (string(afterExpiration) != string(beforeExpiration)) {
logger.Info().Str("shares", "update").Msgf("updating expire date from %v to: %v", string(beforeExpiration), string(afterExpiration))
logger.Debug().Str("shares", "update").Msgf("updating expire date from %v to: %v", string(beforeExpiration), string(afterExpiration))
updates = append(updates, &link.UpdatePublicShareRequest_Update{
Type: link.UpdatePublicShareRequest_Update_TYPE_EXPIRATION,
Grant: &link.Grant{
Expand Down Expand Up @@ -1529,25 +1526,28 @@ func (h *Handler) updatePublicShare(w http.ResponseWriter, r *http.Request, toke
response.WriteOCSSuccess(w, r, shares)
}

func expirationTimestampFromRequest(r *http.Request, h *Handler) *types.Timestamp {
func expirationTimestampFromRequest(r *http.Request, h *Handler) (*types.Timestamp, error) {
var expireTime time.Time
var err error

expireDate := r.FormValue("expireDate")
if expireDate != "" {
expireTime, err = time.Parse("2006-01-02T15:04:05Z0700", expireDate)
if err != nil {
log.Error().Str("expiration", "create public share").Msgf("date format invalid: %v", expireDate)
expireTime, err = time.Parse("2006-01-02", expireDate)
}
if err != nil {
return nil, fmt.Errorf("date format invalid: %v", expireDate)
}
final := expireTime.UnixNano()

return &types.Timestamp{
Seconds: uint64(final / 1000000000),
Nanos: uint32(final % 1000000000),
}
}, nil
}

return nil
return nil, nil
}

func permissionFromRequest(r *http.Request, h *Handler) *provider.ResourcePermissions {
Expand Down