-
Notifications
You must be signed in to change notification settings - Fork 112
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
Layout template #476
Merged
Merged
Layout template #476
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
70d0830
first attempt at templates in go
madsi1m 5d78398
simplify code
madsi1m cfa1923
created a storage helper so the template work lives in one spot for a…
madsi1m 53a686a
added layout to storage-home.toml
madsi1m 2d7d7c5
add UsernameLower
madsi1m 9a6209f
better comment for exported function
madsi1m f040dee
Merge branch 'master' into layout
2798d56
fix merge
madsi1m 03bbeb6
Merge branch 'layout' of https://github.com/madsi1m/reva into layout
madsi1m 70960ea
add UsernamePrefixCount
madsi1m 80a9519
remove pw as it is not needed anymore
madsi1m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2018-2019 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package helper | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/cs3org/reva/pkg/errtypes" | ||
"github.com/pkg/errors" | ||
|
||
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
) | ||
|
||
type layoutTemplate struct { | ||
Username string //the username | ||
UsernameLower string //the username in lowercase | ||
FirstLetter string //first letter of username in lowercase | ||
Provider string //Provider/domain of user in lowercase | ||
} | ||
|
||
// GetUserHomePath converts username into user's home path according to layout | ||
func GetUserHomePath(u *userpb.User, layout string) (string, error) { | ||
if u.Username == "" { | ||
return "", errors.Wrap(errtypes.UserRequired("userrequired"), "user has no username") | ||
} | ||
|
||
usernameSplit := strings.Split(u.Username, "@") | ||
if len(usernameSplit) == 1 { | ||
usernameSplit = append(usernameSplit, "_unknown") | ||
} | ||
if usernameSplit[1] == "" { | ||
usernameSplit[1] = "_unknown" | ||
} | ||
|
||
pathTemplate := layoutTemplate{ | ||
Username: u.Username, | ||
UsernameLower: strings.ToLower(u.Username), | ||
FirstLetter: strings.ToLower(string([]rune(usernameSplit[0])[0])), | ||
Provider: strings.ToLower(usernameSplit[1]), | ||
} | ||
tmpl, err := template.New("userhomepath").Parse(layout) | ||
if err != nil { | ||
return "", errors.Wrap(errtypes.UserRequired("userrequired"), fmt.Sprintf("template parse error: %s", err.Error())) | ||
} | ||
buf := new(bytes.Buffer) | ||
err = tmpl.Execute(buf, pathTemplate) | ||
if err != nil { | ||
return "", errors.Wrap(errtypes.UserRequired("userrequired"), fmt.Sprintf("template execute error: %s", err.Error())) | ||
} | ||
|
||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently,
FirstLetter
limits that dir to a single rune. I'd like to see a variable length to use as the name for the parent dir. I lack the words for a better name of theParent
directory. Prefix does not cut it. Maybebucket
. For s3 storages we were considering the first n chars of an md5 / sha1 of the username to spread users among multiple buckets. But never followed up on it because randomly putting users into buckets seems nice, but in practice you'll end up with unevenly filled buckets when a few powerusers land in the same bucket. Which is why we stored a users home in the account table. That allows moving a single user to a different storage ...This is better than the current code so still a +1 from me ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, added
{{.UsernamePrefixCount.x}}
which at least solves the ability to set the x to 3 so you can make valid s3 buckets. I'm not sure how to spread users across "buckets" evenly as we cant dictate how a user will use the storage.