chore: import upstream snapshot with attribution
CI / Check and lint (push) Failing after 1s
Lint / Lint (ubuntu-latest) (push) Failing after 1s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:22:32 +08:00
commit 39dbe3a57d
1131 changed files with 272709 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import "unicode"
func IsChinese(str string) bool {
for _, v := range str {
if unicode.Is(unicode.Han, v) {
return true
}
}
return false
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import "strings"
func EmailInAllowEmailDomain(email string, allowEmailDomains []string) bool {
if len(allowEmailDomains) == 0 {
return true
}
for _, domain := range allowEmailDomains {
if strings.HasSuffix(email, domain) {
return true
}
}
return false
}
+152
View File
@@ -0,0 +1,152 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"path/filepath"
"slices"
"strings"
"github.com/segmentfault/pacman/log"
"golang.org/x/image/webp"
)
// IsUnAuthorizedExtension check whether the file extension is not in the allowedExtensions
// WANING Only checks the file extension is not reliable, but `http.DetectContentType` and `mimetype` are not reliable for all file types.
func IsUnAuthorizedExtension(fileName string, allowedExtensions []string) bool {
ext := strings.ToLower(strings.Trim(filepath.Ext(fileName), "."))
return !slices.Contains(allowedExtensions, ext)
}
// DecodeAndCheckImageFile currently answers support image type is
// `image/jpeg, image/jpg, image/png, image/gif, image/webp`
func DecodeAndCheckImageFile(localFilePath string, maxImageMegapixel int) bool {
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(localFilePath), "."))
switch ext {
case "jpg", "jpeg", "png", "gif":
if !decodeAndCheckImageFile(localFilePath, maxImageMegapixel, ext, formatSpecificConfigCheck) {
return false
}
if !decodeAndCheckImageFile(localFilePath, maxImageMegapixel, ext, formatSpecificImageCheck) {
return false
}
case "webp":
if !decodeAndCheckImageFile(localFilePath, maxImageMegapixel, ext, webpImageConfigCheck) {
return false
}
if !decodeAndCheckImageFile(localFilePath, maxImageMegapixel, ext, webpImageCheck) {
return false
}
}
return true
}
func decodeAndCheckImageFile(localFilePath string, maxImageMegapixel int, ext string,
checker func(file io.Reader, ext string, maxImageMegapixel int) error) bool {
file, err := os.Open(localFilePath)
if err != nil {
log.Errorf("open file error: %v", err)
return false
}
defer func() {
_ = file.Close()
}()
if err = checker(file, ext, maxImageMegapixel); err != nil {
log.Errorf("check image format error: %v", err)
return false
}
return true
}
// formatSpecificConfigCheck decodes image config using a format-specific decoder
// based on the file extension. This avoids calling image.DecodeConfig() which
// dispatches by magic bytes and can invoke unintended decoders (e.g., TIFF)
// registered by transitive dependencies.
func formatSpecificConfigCheck(file io.Reader, ext string, maxImageMegapixel int) error {
var config image.Config
var err error
switch ext {
case "jpg", "jpeg":
config, err = jpeg.DecodeConfig(file)
case "png":
config, err = png.DecodeConfig(file)
case "gif":
config, err = gif.DecodeConfig(file)
default:
return fmt.Errorf("unsupported image format: %s", ext)
}
if err != nil {
return fmt.Errorf("decode image config error: %v", err)
}
if imageSizeTooLarge(config, maxImageMegapixel) {
return fmt.Errorf("image size too large")
}
return nil
}
// formatSpecificImageCheck fully decodes the image using a format-specific decoder.
func formatSpecificImageCheck(file io.Reader, ext string, _ int) error {
var err error
switch ext {
case "jpg", "jpeg":
_, err = jpeg.Decode(file)
case "png":
_, err = png.Decode(file)
case "gif":
_, err = gif.Decode(file)
default:
return fmt.Errorf("unsupported image format: %s", ext)
}
if err != nil {
return fmt.Errorf("decode image error: %v", err)
}
return nil
}
func webpImageConfigCheck(file io.Reader, _ string, maxImageMegapixel int) error {
config, err := webp.DecodeConfig(file)
if err != nil {
return fmt.Errorf("decode webp image config error: %v", err)
}
if imageSizeTooLarge(config, maxImageMegapixel) {
return fmt.Errorf("image size too large")
}
return nil
}
func webpImageCheck(file io.Reader, _ string, _ int) error {
_, err := webp.Decode(file)
if err != nil {
return fmt.Errorf("decode webp image error: %v", err)
}
return nil
}
func imageSizeTooLarge(config image.Config, maxImageMegapixel int) bool {
return config.Width*config.Height > maxImageMegapixel
}
+67
View File
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import (
"errors"
"fmt"
"regexp"
"strings"
)
const (
levelD = iota
LevelC
LevelB
LevelA
LevelS
)
const (
PasswordCannotContainSpaces = "error.password.space_invalid"
)
// CheckPassword checks the password strength
func CheckPassword(password string) error {
if strings.Contains(password, " ") {
return errors.New(PasswordCannotContainSpaces)
}
// TODO Currently there is no requirement for password strength
minLevel := 0
// The password strength level is initialized to D.
// The regular is used to verify the password strength.
// If the matching is successful, the password strength increases by 1
level := levelD
patternList := []string{`[0-9]+`, `[a-z]+`, `[A-Z]+`, `[~!@#$%^&*?_-]+`}
for _, pattern := range patternList {
match, _ := regexp.MatchString(pattern, password)
if match {
level++
}
}
// If the final password strength falls below the required minimum strength, return with an error
if level < minLevel {
return fmt.Errorf("the password does not satisfy the current policy requirements")
}
return nil
}
+57
View File
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import (
"slices"
"sync"
"github.com/apache/answer/configs"
"github.com/segmentfault/pacman/log"
"gopkg.in/yaml.v3"
)
type PathIgnore struct {
Users []string `yaml:"users"`
Questions []string `yaml:"questions"`
}
var (
ignorePathInit sync.Once
pathIgnore = &PathIgnore{}
)
func initPathIgnore() {
if err := yaml.Unmarshal(configs.PathIgnore, pathIgnore); err != nil {
log.Error(err)
}
}
// IsUsersIgnorePath checks whether the username is in ignore path
func IsUsersIgnorePath(username string) bool {
ignorePathInit.Do(initPathIgnore)
return slices.Contains(pathIgnore.Users, username)
}
// IsQuestionsIgnorePath checks whether the questionID is in ignore path
func IsQuestionsIgnorePath(questionID string) bool {
ignorePathInit.Do(initPathIgnore)
return slices.Contains(pathIgnore.Questions, questionID)
}
+131
View File
@@ -0,0 +1,131 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import (
"github.com/apache/answer/internal/base/constant"
"github.com/apache/answer/pkg/obj"
"github.com/apache/answer/pkg/uid"
)
const (
QuestionLinkTypeURL = 1
QuestionLinkTypeID = 2
)
type QuestionLink struct {
LinkType int
QuestionID string
AnswerID string
}
func GetQuestionLink(content string) []QuestionLink {
uniqueIDs := make(map[string]struct{})
var questionLinks []QuestionLink
// use two pointer to find the link
left, right := 0, 0
for right < len(content) {
// find "/questions/" or "#"
switch {
case right+11 < len(content) && content[right:right+11] == "/questions/":
left = right
right += 11
processURL(content, &left, &right, uniqueIDs, &questionLinks)
case content[right] == '#':
left = right + 1
right = left
processID(content, &left, &right, uniqueIDs, &questionLinks)
default:
right++
}
}
return questionLinks
}
func processURL(content string, left, right *int, uniqueIDs map[string]struct{}, questionLinks *[]QuestionLink) {
for *right < len(content) && (isDigit(content[*right]) || isLetter(content[*right])) {
*right++
}
questionID := content[*left+len("/questions/") : *right]
answerID := ""
if *right < len(content) && content[*right] == '/' {
*left = *right + 1
*right = *left
for *right < len(content) && (isDigit(content[*right]) || isLetter(content[*right])) {
*right++
}
answerID = content[*left:*right]
}
addUniqueID(questionID, answerID, QuestionLinkTypeURL, uniqueIDs, questionLinks)
}
func processID(content string, left, right *int, uniqueIDs map[string]struct{}, questionLinks *[]QuestionLink) {
for *right < len(content) && (isDigit(content[*right]) || isLetter(content[*right])) {
*right++
}
id := content[*left:*right]
addUniqueID(id, "", QuestionLinkTypeID, uniqueIDs, questionLinks)
}
func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}
func isLetter(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func addUniqueID(questionID, answerID string, linkType int, uniqueIDs map[string]struct{}, questionLinks *[]QuestionLink) {
isAdd := false
if answerID != "" {
objectType, err := obj.GetObjectTypeStrByObjectID(uid.DeShortID(answerID))
if err != nil {
answerID = ""
} else if objectType == constant.AnswerObjectType {
if _, ok := uniqueIDs[answerID]; !ok {
uniqueIDs[answerID] = struct{}{}
isAdd = true
}
}
}
if objectType, err := obj.GetObjectTypeStrByObjectID(uid.DeShortID(questionID)); err == nil {
if _, ok := uniqueIDs[questionID]; !ok {
uniqueIDs[questionID] = struct{}{}
isAdd = true
if objectType == constant.AnswerObjectType {
answerID = questionID
questionID = ""
}
}
}
if isAdd {
*questionLinks = append(*questionLinks, QuestionLink{
LinkType: linkType,
QuestionID: questionID,
AnswerID: answerID,
})
}
}
+184
View File
@@ -0,0 +1,184 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker_test
import (
"testing"
"github.com/apache/answer/pkg/checker"
"github.com/stretchr/testify/assert"
)
func TestGetQuestionLink(t *testing.T) {
// Step 1: Test empty content
t.Run("Empty content", func(t *testing.T) {
links := checker.GetQuestionLink("")
assert.Empty(t, links)
})
// Step 2: Test content without link or ID
t.Run("Content without link or ID", func(t *testing.T) {
links := checker.GetQuestionLink("This is a random text")
assert.Empty(t, links)
})
// Step 3: Test content with valid question link
t.Run("Valid question link", func(t *testing.T) {
links := checker.GetQuestionLink("Check this question: https://example.com/questions/10010000000000060")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000060",
AnswerID: "",
},
}, links)
})
// Step 4: Test content with valid question and answer link
t.Run("Valid question and answer link", func(t *testing.T) {
links := checker.GetQuestionLink("Check this answer: https://example.com/questions/10010000000000060/10020000000000060?from=copy")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000060",
AnswerID: "10020000000000060",
},
}, links)
})
// Step 5: Test content with #questionID
t.Run("Content with #questionID", func(t *testing.T) {
links := checker.GetQuestionLink("This is question #10010000000000060")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeID,
QuestionID: "10010000000000060",
AnswerID: "",
},
}, links)
})
// Step 6: Test content with #answerID
t.Run("Content with #answerID", func(t *testing.T) {
links := checker.GetQuestionLink("This is answer #10020000000000060")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeID,
QuestionID: "",
AnswerID: "10020000000000060",
},
}, links)
})
// Step 7: Test invalid question ID
t.Run("Invalid question ID", func(t *testing.T) {
links := checker.GetQuestionLink("https://example.com/questions/invalid")
assert.Empty(t, links)
})
// Step 8: Test invalid answer ID
t.Run("Invalid answer ID", func(t *testing.T) {
links := checker.GetQuestionLink("https://example.com/questions/10010000000000060/invalid")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000060",
AnswerID: "",
},
}, links)
})
// Step 9: Test content with multiple links and IDs
t.Run("Multiple links and IDs", func(t *testing.T) {
content := "Question #10010000000000060 and https://example.com/questions/10010000000000060/10020000000000061 and https://example.com/questions/10010000000000065/10020000000000066 and another #10020000000000066"
links := checker.GetQuestionLink(content)
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeID,
QuestionID: "10010000000000060",
AnswerID: "",
},
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000060",
AnswerID: "10020000000000061",
},
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000065",
AnswerID: "10020000000000066",
},
}, links)
})
// Step 11: Test URL with www prefix
t.Run("URL with www prefix", func(t *testing.T) {
links := checker.GetQuestionLink("Check this question: https://www.example.com/questions/10010000000000060")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000060",
AnswerID: "",
},
}, links)
})
// Step 12: Test URL without protocol
t.Run("URL without protocol", func(t *testing.T) {
links := checker.GetQuestionLink("Check this question: example.com/questions/10010000000000060")
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000060",
AnswerID: "",
},
}, links)
})
// Step 14: Test error id
t.Run("Error id", func(t *testing.T) {
links := checker.GetQuestionLink("https://example.com/questions/10110000000000060")
assert.Empty(t, links)
})
// step 15: SEO options
t.Run("SEO options", func(t *testing.T) {
content := `
URL1: http://localhost:3000/questions/D1I2
URL2: http://localhost:3000/questions/D1I2/hello
URL3: http://localhost:3000/questions/10010000000000068
URL4: http://localhost:3000/questions/10010000000000068/hello
ERROR URL: http://localhost:3000/questions/AAAA/BBBB
`
links := checker.GetQuestionLink(content)
assert.Equal(t, []checker.QuestionLink{
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "D1I2",
AnswerID: "",
},
{
LinkType: checker.QuestionLinkTypeURL,
QuestionID: "10010000000000068",
AnswerID: "",
},
}, links)
})
}
+58
View File
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import (
"encoding/json"
"os"
"path/filepath"
"sync"
"github.com/apache/answer/configs"
"github.com/apache/answer/internal/base/path"
"github.com/apache/answer/pkg/dir"
)
var (
reservedUsernameMapping = make(map[string]bool)
reservedUsernameInit sync.Once
)
func initReservedUsername() {
reservedUsernamesJsonFilePath := filepath.Join(path.ConfigFileDir, path.DefaultReservedUsernamesConfigFileName)
if dir.CheckFileExist(reservedUsernamesJsonFilePath) {
// if reserved username file exists, read it and replace configuration
reservedUsernamesJsonFile, err := os.ReadFile(reservedUsernamesJsonFilePath)
if err == nil {
configs.ReservedUsernames = reservedUsernamesJsonFile
}
}
var usernames []string
_ = json.Unmarshal(configs.ReservedUsernames, &usernames)
for _, username := range usernames {
reservedUsernameMapping[username] = true
}
}
// IsReservedUsername checks whether the username is reserved
func IsReservedUsername(username string) bool {
reservedUsernameInit.Do(initReservedUsername)
return reservedUsernameMapping[username]
}
+43
View File
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import (
"net/url"
"strings"
)
func IsURL(str string) bool {
s := strings.ToLower(str)
if len(s) == 0 {
return false
}
u, err := url.Parse(s)
if err != nil || u.Scheme == "" {
return false
}
if u.Host == "" && u.Fragment == "" && u.Opaque == "" {
return false
}
return u.Scheme == "http" || u.Scheme == "https"
}
+30
View File
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
import "regexp"
var (
usernameReg = regexp.MustCompile(`^[\w.\- ]{2,30}$`)
)
func IsInvalidUsername(username string) bool {
return !usernameReg.MatchString(username)
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package checker
// IsNotZeroString check s is not empty string and is not "0"
func IsNotZeroString(s string) bool {
return len(s) > 0 && s != "0"
}
// FilterEmptyString filter empty string from string slice
func FilterEmptyString(strs []string) []string {
var result []string
for _, str := range strs {
if IsNotZeroString(str) {
result = append(result, str)
}
}
return result
}
+46
View File
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package converter
func ArrayNotInArray(original []string, search []string) []string {
var result []string
originalMap := make(map[string]bool)
for _, v := range original {
originalMap[v] = true
}
for _, v := range search {
if _, ok := originalMap[v]; !ok {
result = append(result, v)
}
}
return result
}
func UniqueArray[T comparable](input []T) []T {
result := make([]T, 0, len(input))
seen := make(map[T]bool, len(input))
for _, element := range input {
if !seen[element] {
result = append(result, element)
seen[element] = true
}
}
return result
}
+189
View File
@@ -0,0 +1,189 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package converter
import (
"bytes"
"regexp"
"strings"
"github.com/asaskevich/govalidator"
"github.com/microcosm-cc/bluemonday"
"github.com/segmentfault/pacman/log"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
goldmarkHTML "github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
)
// Markdown2HTML convert markdown to html
func Markdown2HTML(source string) string {
mdConverter := goldmark.New(
goldmark.WithExtensions(&DangerousHTMLFilterExtension{}, extension.GFM, extension.Footnote),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
goldmarkHTML.WithHardWraps(),
),
)
var buf bytes.Buffer
if err := mdConverter.Convert([]byte(source), &buf); err != nil {
log.Error(err)
return source
}
html := buf.String()
filter := bluemonday.UGCPolicy()
filter.AllowStyling()
filter.RequireNoFollowOnLinks(false)
filter.RequireParseableURLs(false)
filter.RequireNoFollowOnFullyQualifiedLinks(false)
filter.AllowElements("kbd")
filter.AllowAttrs("title").Matching(regexp.MustCompile(`^[\p{L}\p{N}\s\-_',\[\]!\./\\\(\)]*$|^@embed?$`)).Globally()
filter.AllowAttrs("start").OnElements("ol")
html = strings.TrimSpace(filter.Sanitize(html))
return html
}
// Markdown2BasicHTML convert markdown to html, Only basic syntax can be used
func Markdown2BasicHTML(source string) string {
content := Markdown2HTML(source)
filter := bluemonday.NewPolicy()
filter.AllowElements("p", "b", "br", "strong", "em")
filter.AllowAttrs("src").OnElements("img")
filter.AddSpaceWhenStrippingTag(true)
content = filter.Sanitize(content)
return content
}
type DangerousHTMLFilterExtension struct {
}
func (e *DangerousHTMLFilterExtension) Extend(m goldmark.Markdown) {
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(&DangerousHTMLRenderer{
Config: goldmarkHTML.NewConfig(),
Filter: bluemonday.UGCPolicy(),
}, 1),
))
}
type DangerousHTMLRenderer struct {
goldmarkHTML.Config
Filter *bluemonday.Policy
}
// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
func (r *DangerousHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(ast.KindHTMLBlock, r.renderHTMLBlock)
reg.Register(ast.KindRawHTML, r.renderRawHTML)
reg.Register(ast.KindLink, r.renderLink)
reg.Register(ast.KindAutoLink, r.renderAutoLink)
}
func (r *DangerousHTMLRenderer) renderRawHTML(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkSkipChildren, nil
}
n := node.(*ast.RawHTML)
l := n.Segments.Len()
for i := range l {
segment := n.Segments.At(i)
if string(source[segment.Start:segment.Stop]) == "<kbd>" || string(source[segment.Start:segment.Stop]) == "</kbd>" {
_, _ = w.Write(segment.Value(source))
} else {
_, _ = w.Write(r.Filter.SanitizeBytes(segment.Value(source)))
}
}
return ast.WalkSkipChildren, nil
}
func (r *DangerousHTMLRenderer) renderHTMLBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.HTMLBlock)
if entering {
l := n.Lines().Len()
for i := range l {
line := n.Lines().At(i)
r.Writer.SecureWrite(w, line.Value(source))
}
} else if n.HasClosure() {
closure := n.ClosureLine
r.Writer.SecureWrite(w, closure.Value(source))
}
return ast.WalkContinue, nil
}
func (r *DangerousHTMLRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.Link)
if entering && r.renderLinkIsUrl(string(n.Destination)) {
_, _ = w.WriteString("<a href=\"")
// _, _ = w.WriteString("<a test=\"1\" rel=\"nofollow\" href=\"")
if r.Unsafe || !goldmarkHTML.IsDangerousURL(n.Destination) {
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
}
_ = w.WriteByte('"')
if n.Title != nil {
_, _ = w.WriteString(` title="`)
r.Writer.Write(w, n.Title)
_ = w.WriteByte('"')
}
if n.Attributes() != nil {
goldmarkHTML.RenderAttributes(w, n, goldmarkHTML.LinkAttributeFilter)
}
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("</a>")
}
return ast.WalkContinue, nil
}
func (r *DangerousHTMLRenderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ast.AutoLink)
if !entering || !r.renderLinkIsUrl(string(n.URL(source))) {
return ast.WalkContinue, nil
}
_, _ = w.WriteString(`<a href="`)
url := n.URL(source)
label := n.Label(source)
if n.AutoLinkType == ast.AutoLinkEmail && !bytes.HasPrefix(bytes.ToLower(url), []byte("mailto:")) {
_, _ = w.WriteString("mailto:")
}
_, _ = w.Write(util.EscapeHTML(util.URLEscape(url, false)))
if n.Attributes() != nil {
_ = w.WriteByte('"')
goldmarkHTML.RenderAttributes(w, n, goldmarkHTML.LinkAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString(`">`)
}
_, _ = w.Write(util.EscapeHTML(label))
_, _ = w.WriteString(`</a>`)
return ast.WalkContinue, nil
}
func (r *DangerousHTMLRenderer) renderLinkIsUrl(verifyURL string) bool {
isURL := govalidator.IsURL(verifyURL)
isPath, _ := regexp.MatchString(`^/`, verifyURL)
return isURL || isPath
}
+74
View File
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package converter
import (
"fmt"
"strconv"
"github.com/segmentfault/pacman/log"
)
func StringToInt64(str string) int64 {
num, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0
}
return num
}
func StringToInt(str string) int {
num, err := strconv.Atoi(str)
if err != nil {
return 0
}
return num
}
func IntToString(data int64) string {
return fmt.Sprintf("%d", data)
}
// InterfaceToString converts data to string
// It will be used in template render
func InterfaceToString(data any) string {
switch t := data.(type) {
case int:
i := data.(int)
return strconv.Itoa(i)
case int8:
i := data.(int8)
return strconv.Itoa(int(i))
case int16:
i := data.(int16)
return strconv.Itoa(int(i))
case int32:
i := data.(int32)
return string(i)
case int64:
i := data.(int64)
return strconv.FormatInt(i, 10)
case string:
return data.(string)
default:
log.Warn("can't convert type:", t)
}
return ""
}
+43
View File
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package converter
import (
"regexp"
"github.com/segmentfault/pacman/utils"
)
func DeleteUserDisplay(userID string) string {
return utils.EnShortID(StringToInt64(userID), 100)
}
func GetMentionUsernameList(text string) []string {
re := regexp.MustCompile(`\[@([^\]]+)\]\(/users/[^\)]+\)`)
matches := re.FindAllStringSubmatch(text, -1)
var usernames []string
for _, match := range matches {
if len(match) > 1 {
usernames = append(usernames, match[1])
}
}
return usernames
}
+210
View File
@@ -0,0 +1,210 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package day
import (
"strings"
"time"
)
var placeholder = map[string]string{
"YY": "06", // 06 year
"YYYY": "2006", // 2006 full year
"M": "1", // 1-12 month
"MM": "01", // 01-12 month
"MMM": "Jan", // Jan-Dec month
"MMMM": "January", // January-December month
"D": "2", // 1-31 date
"DD": "02", // 01-31 date preset 0
"H": "15", // 00-23 hour 24
"HH": "15", // 00-23 hour 24
"h": "3", // 1-12 hour 12
"hh": "03", // 01-12 hour 12
"m": "4", // 0-59 minute
"mm": "04", // 00-59 minute
"s": "5", // 0-59 second
"ss": "05", // 00-59 second
"A": "PM", // AM / PM
"a": "pm", // am / pm
"[at]": "at", // at string
}
func Format(unix int64, format, tz string) (formatted string) {
/*l := len(placeholders) - 1
for i := l; i >= 0; i-- {
format = strings.ReplaceAll(format, placeholders[i].old, placeholders[i].new)
}*/
var toFormat strings.Builder
from := []rune(format)
for len(from) > 0 {
to, suffix := nextStdChunk(from)
toFormat.WriteString(string(to))
from = suffix
}
_, _ = time.LoadLocation(tz)
formatted = time.Unix(unix, 0).Format(toFormat.String())
return
}
func nextStdChunk(from []rune) (to, suffix []rune) {
if len(from) == 0 {
to = []rune{}
suffix = []rune{}
return
}
s := string(from[0])
old := ""
switch s {
case "Y":
if len(from) >= 4 && string(from[:4]) == "YYYY" {
old = "YYYY"
} else if len(from) >= 2 && string(from[:2]) == "YY" {
old = "YY"
}
case "M":
for i := 4; i > 0; i-- {
if len(from) >= i {
switch string(from[:i]) {
case "MMMM":
old = "MMMM"
case "MMM":
old = "MMM"
case "MM":
old = "MM"
case "M":
old = "M"
}
}
if old != "" {
break
}
}
case "D":
for i := 2; i >= 0; i-- {
if len(from) >= i {
switch string(from[:i]) {
case "DD":
old = "DD"
case "D":
old = "D"
}
}
if old != "" {
break
}
}
case "H":
for i := 2; i >= 0; i-- {
if len(from) >= i {
switch string(from[:i]) {
case "HH":
old = "HH"
case "H":
old = "H"
}
}
if old != "" {
break
}
}
case "h":
for i := 2; i >= 0; i-- {
if len(from) >= i {
switch string(from[:i]) {
case "hh":
old = "hh"
case "h":
old = "h"
}
}
if old != "" {
break
}
}
case "m":
for i := 2; i >= 0; i-- {
if len(from) >= i {
switch string(from[:i]) {
case "mm":
old = "mm"
case "m":
old = "m"
}
}
if old != "" {
break
}
}
case "s":
for i := 2; i >= 0; i-- {
if len(from) >= i {
switch string(from[:i]) {
case "ss":
old = "ss"
case "s":
old = "s"
}
}
if old != "" {
break
}
}
case "A":
old = "A"
case "a":
old = "a"
case "[":
// treat anything inside [] as literal text, e.g. [at], [a las], [o]
end := -1
for i := 1; i < len(from); i++ {
if from[i] == ']' {
end = i
break
}
}
if end != -1 {
to = []rune(string(from[1:end]))
suffix = from[end+1:]
return
}
// no closing bracket, fall back to literal "["
old = "["
default:
old = s
}
if old == "" {
// safety: always consume at least one rune
old = s
}
tos, ok := placeholder[old]
if !ok {
to = []rune(old)
} else {
to = []rune(tos)
}
suffix = from[len([]rune(old)):]
return
}
+104
View File
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package day
import (
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestFormat(t *testing.T) {
sec := int64(1700000000)
tz := "UTC"
actual := Format(sec, "MMM D, YYYY [a las] HH:mm", tz)
expected := time.Unix(sec, 0).Format("Jan 2, 2006") + " a las " + time.Unix(sec, 0).Format("15:04")
assert.Equal(t, expected, actual)
}
func TestFormat_AllLanguagesNoHang(t *testing.T) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatalf("unable to determine test file path")
}
i18nDir := filepath.Clean(filepath.Join(filepath.Dir(currentFile), "..", "..", "i18n"))
entries, err := os.ReadDir(i18nDir)
if err != nil {
t.Fatalf("read i18n dir: %v", err)
}
type datesConfig struct {
LongDate string `yaml:"long_date"`
LongDateWithYear string `yaml:"long_date_with_year"`
LongDateWithTime string `yaml:"long_date_with_time"`
}
type uiConfig struct {
Dates datesConfig `yaml:"dates"`
}
type fileConfig struct {
UI uiConfig `yaml:"ui"`
}
sec := int64(1700000000)
tz := "UTC"
for _, entry := range entries {
name := entry.Name()
if entry.IsDir() || filepath.Ext(name) != ".yaml" || name == "i18n.yaml" {
continue
}
data, err := os.ReadFile(filepath.Join(i18nDir, name))
if err != nil {
t.Fatalf("read %s: %v", name, err)
}
var cfg fileConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
t.Fatalf("parse %s: %v", name, err)
}
formats := []string{
cfg.UI.Dates.LongDate,
cfg.UI.Dates.LongDateWithYear,
cfg.UI.Dates.LongDateWithTime,
}
for _, format := range formats {
if format == "" {
continue
}
done := make(chan struct{})
go func(f string) {
_ = Format(sec, f, tz)
close(done)
}(format)
select {
case <-done:
case <-time.After(200 * time.Millisecond):
t.Fatalf("format hang in %s: %q", name, format)
}
}
}
}
+69
View File
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package dir
import (
"fmt"
"os"
"path/filepath"
)
func CreateDirIfNotExist(path string) error {
return os.MkdirAll(path, os.ModePerm)
}
func CheckDirExist(path string) bool {
f, err := os.Stat(path)
return err == nil && f.IsDir()
}
func CheckFileExist(path string) bool {
f, err := os.Stat(path)
return err == nil && !f.IsDir()
}
func DirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func FormatFileSize(fileSize int64) (size string) {
switch {
case fileSize < 1024:
// return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2f B", float64(fileSize)/float64(1))
case fileSize < (1024 * 1024):
return fmt.Sprintf("%.2f KB", float64(fileSize)/float64(1024))
case fileSize < (1024 * 1024 * 1024):
return fmt.Sprintf("%.2f MB", float64(fileSize)/float64(1024*1024))
case fileSize < (1024 * 1024 * 1024 * 1024):
return fmt.Sprintf("%.2f GB", float64(fileSize)/float64(1024*1024*1024))
case fileSize < (1024 * 1024 * 1024 * 1024 * 1024):
return fmt.Sprintf("%.2f TB", float64(fileSize)/float64(1024*1024*1024*1024))
default: // if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2f EB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}
+65
View File
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package display
import (
"github.com/apache/answer/internal/base/constant"
"github.com/apache/answer/pkg/htmltext"
"github.com/apache/answer/pkg/uid"
)
// QuestionURL get question url
func QuestionURL(permalink int, siteUrl, questionID, title string) string {
u := siteUrl + "/questions"
if permalink == constant.PermalinkQuestionIDAndTitle || permalink == constant.PermalinkQuestionID {
questionID = uid.DeShortID(questionID)
} else {
questionID = uid.EnShortID(questionID)
}
u += "/" + questionID
if permalink == constant.PermalinkQuestionIDAndTitle || permalink == constant.PermalinkQuestionIDAndTitleByShortID {
u += "/" + htmltext.UrlTitle(title)
}
return u
}
// AnswerURL get answer url
func AnswerURL(permalink int, siteUrl, questionID, title, answerID string) string {
if permalink == constant.PermalinkQuestionIDAndTitle ||
permalink == constant.PermalinkQuestionID {
answerID = uid.DeShortID(answerID)
} else {
answerID = uid.EnShortID(answerID)
}
return QuestionURL(permalink, siteUrl, questionID, title) + "/" + answerID
}
// CommentURL get comment url
func CommentURL(permalink int, siteUrl, questionID, title, answerID, commentID string) string {
if len(answerID) > 0 {
return AnswerURL(permalink, siteUrl, questionID, answerID, title) + "?commentId=" + commentID
}
return QuestionURL(permalink, siteUrl, questionID, title) + "?commentId=" + commentID
}
// UserURL get user url
func UserURL(siteUrl, username string) string {
return siteUrl + "/users/" + username
}
+32
View File
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package encryption
import (
"crypto/md5"
"encoding/hex"
)
// MD5 return md5 hash
func MD5(data string) string {
h := md5.New()
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
+50
View File
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package gravatar
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/url"
"strings"
)
// GetAvatarURL get avatar url from gravatar by email
func GetAvatarURL(baseURL, email string) string {
hasher := sha256.Sum256([]byte(strings.TrimSpace(email)))
hash := hex.EncodeToString(hasher[:])
return baseURL + hash
}
// Resize resize avatar by pixel
func Resize(originalAvatarURL string, sizePixel int) (resizedAvatarURL string) {
if len(originalAvatarURL) == 0 {
return
}
originalURL, err := url.Parse(originalAvatarURL)
if err != nil {
return originalAvatarURL
}
query := originalURL.Query()
query.Set("s", fmt.Sprintf("%d", sizePixel))
originalURL.RawQuery = query.Encode()
return originalURL.String()
}
+92
View File
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package gravatar
import (
"testing"
"github.com/apache/answer/internal/base/constant"
"github.com/stretchr/testify/assert"
)
func TestGetAvatarURL(t *testing.T) {
type args struct {
email string
}
tests := []struct {
name string
args args
want string
}{
{
name: "answer@answer.com",
args: args{email: "answer@answer.com"},
want: "https://www.gravatar.com/avatar/7296942c1f63d97f6c124705142009867638f7b3dbcdadd0cb1bcb40e427eb8e",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, GetAvatarURL(constant.DefaultGravatarBaseURL, tt.args.email))
})
}
}
func TestResize(t *testing.T) {
type args struct {
originalAvatarURL string
sizePixel int
}
tests := []struct {
name string
args args
wantResizedAvatarURL string
}{
{
name: "original url",
args: args{
originalAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7",
sizePixel: 128,
},
wantResizedAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7?s=128",
},
{
name: "already resized url",
args: args{
originalAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7?s=128",
sizePixel: 64,
},
wantResizedAvatarURL: "https://www.gravatar.com/avatar/b2be4e4438f08a5e885be8de5f41fdd7?s=64",
},
{
name: "empty url",
args: args{
originalAvatarURL: "",
sizePixel: 64,
},
wantResizedAvatarURL: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.wantResizedAvatarURL, Resize(tt.args.originalAvatarURL, tt.args.sizePixel), "Resize(%v, %v)", tt.args.originalAvatarURL, tt.args.sizePixel)
})
}
}
+254
View File
@@ -0,0 +1,254 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package htmltext
import (
"io"
"net/http"
"net/url"
"regexp"
"strings"
"sync/atomic"
"unicode"
"unicode/utf8"
"github.com/Machiel/slugify"
"github.com/apache/answer/pkg/checker"
"github.com/apache/answer/pkg/converter"
strip "github.com/grokify/html-strip-tags-go"
"github.com/mozillazg/go-pinyin"
"github.com/mozillazg/go-unidecode"
)
var (
reCode = regexp.MustCompile(`(?ism)<(pre)>.*<\/pre>`)
reCodeReplace = "{code...}"
reLink = regexp.MustCompile(`(?ism)<a.*?[^<]>(.*)?<\/a>`)
reLinkReplace = " [$1] "
reSpace = regexp.MustCompile(` +`)
reSpaceReplace = " "
spaceReplacer = strings.NewReplacer(
"\n", " ",
"\r", " ",
"\t", " ",
)
// Without this, pure non-Latin titles (Arabic, Cyrillic, Hebrew, ...) get
// stripped by slugify and collapse to the "topic" fallback. Chinese is
// handled separately by convertChinese.
transliterateNonLatin atomic.Bool
)
func init() {
transliterateNonLatin.Store(true)
}
// SetTransliterateNonLatin toggles non-Latin script transliteration for URL slugs.
func SetTransliterateNonLatin(enabled bool) {
transliterateNonLatin.Store(enabled)
}
// IsTransliterateNonLatinEnabled reports whether non-Latin transliteration is on.
func IsTransliterateNonLatinEnabled() bool {
return transliterateNonLatin.Load()
}
// ClearText clear HTML, get the clear text
func ClearText(html string) string {
if html == "" {
return html
}
html = reCode.ReplaceAllString(html, reCodeReplace)
html = reLink.ReplaceAllString(html, reLinkReplace)
text := spaceReplacer.Replace(strip.StripTags(html))
// replace multiple spaces to one space
return strings.TrimSpace(reSpace.ReplaceAllString(text, reSpaceReplace))
}
func UrlTitle(title string) (text string) {
title = convertChinese(title)
if transliterateNonLatin.Load() {
title = convertNonLatin(title)
}
title = clearEmoji(title)
title = slugify.Slugify(title)
title = url.QueryEscape(title)
title = cutLongTitle(title)
if len(title) == 0 {
title = "topic"
}
return title
}
func clearEmoji(s string) string {
var ret strings.Builder
rs := []rune(s)
for i := range rs {
if len(string(rs[i])) != 4 {
ret.WriteString(string(rs[i]))
}
}
return ret.String()
}
func convertChinese(content string) string {
has := checker.IsChinese(content)
if !has {
return content
}
return strings.Join(pinyin.LazyConvert(content, nil), "-")
}
// Short-circuits on Latin-only / Chinese-only input so existing slugs stay byte-identical.
func convertNonLatin(content string) string {
if !containsNonLatin(content) {
return content
}
return unidecode.Unidecode(content)
}
func containsNonLatin(content string) bool {
for _, r := range content {
switch {
case r < 0x0080: // ASCII
continue
case r >= 0x0080 && r <= 0x024F: // Latin-1 Supplement, Latin Extended-A/B
continue
case unicode.Is(unicode.Han, r): // handled by convertChinese
continue
case unicode.IsLetter(r):
return true
}
}
return false
}
func cutLongTitle(title string) string {
maxBytes := 150
if len(title) <= maxBytes {
return title
}
truncated := title[:maxBytes]
for len(truncated) > 0 && !utf8.ValidString(truncated) {
truncated = truncated[:len(truncated)-1]
}
return truncated
}
// FetchExcerpt return the excerpt from the HTML string
func FetchExcerpt(html, trimMarker string, limit int) (text string) {
return FetchRangedExcerpt(html, trimMarker, 0, limit)
}
// findFirstMatchedWord returns the first matched word and its index
func findFirstMatchedWord(text string, words []string) (string, int) {
if len(text) == 0 || len(words) == 0 {
return "", 0
}
words = converter.UniqueArray(words)
firstWord := ""
firstIndex := len(text)
for _, word := range words {
if idx := strings.Index(text, word); idx != -1 && idx < firstIndex {
firstIndex = idx
firstWord = word
}
}
if firstIndex != len(text) {
return firstWord, firstIndex
}
return "", 0
}
// getRuneRange returns the valid begin and end indexes of the runeText
func getRuneRange(runeText []rune, offset, limit int) (begin, end int) {
runeLen := len(runeText)
limit = min(runeLen, max(0, limit))
begin = min(runeLen, max(0, offset))
end = min(runeLen, begin+limit)
return
}
// FetchRangedExcerpt returns a ranged excerpt from the HTML string.
// Note: offset is a rune index, not a byte index
func FetchRangedExcerpt(html, trimMarker string, offset int, limit int) (text string) {
if len(html) == 0 {
text = html
return
}
runeText := []rune(ClearText(html))
begin, end := getRuneRange(runeText, offset, limit)
text = string(runeText[begin:end])
if begin > 0 {
text = trimMarker + text
}
if end < len(runeText) {
text += trimMarker
}
return
}
// FetchMatchedExcerpt returns the matched excerpt according to the words
func FetchMatchedExcerpt(html string, words []string, trimMarker string, trimLength int) string {
text := ClearText(html)
matchedWord, matchedIndex := findFirstMatchedWord(text, words)
runeIndex := utf8.RuneCountInString(text[0:matchedIndex])
trimLength = max(0, trimLength)
runeOffset := runeIndex - trimLength
runeLimit := trimLength + trimLength + utf8.RuneCountInString(matchedWord)
textRuneCount := utf8.RuneCountInString(text)
if runeOffset+runeLimit > textRuneCount {
// Reserved extra chars before the matched word
runeOffset = textRuneCount - runeLimit
}
return FetchRangedExcerpt(html, trimMarker, runeOffset, runeLimit)
}
func GetPicByUrl(url string) string {
res, err := http.Get(url)
if err != nil {
return ""
}
defer func() {
_ = res.Body.Close()
}()
pix, err := io.ReadAll(res.Body)
if err != nil {
return ""
}
return string(pix)
}
+333
View File
@@ -0,0 +1,333 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package htmltext
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClearText(t *testing.T) {
var (
expected,
clearedText string
)
// test code clear text
expected = "hello{code...}"
clearedText = ClearText("<p>hello<pre>var a = \"good\"</pre></p>")
assert.Equal(t, expected, clearedText)
// test link clear text
expected = "hello [example.com]"
clearedText = ClearText("<p>hello <a href=\"http://example.com/\">example.com</a></p>")
assert.Equal(t, expected, clearedText)
clearedText = ClearText("<p>hello<a href=\"https://example.com/\">example.com</a></p>")
assert.Equal(t, expected, clearedText)
expected = "hello world"
clearedText = ClearText("<div> hello</div>\n<div>world</div>")
assert.Equal(t, expected, clearedText)
}
func TestFetchExcerpt(t *testing.T) {
var (
expected,
text string
)
// test english string
expected = "hello..."
text = FetchExcerpt("<p>hello world</p>", "...", 5)
assert.Equal(t, expected, text)
// test mixed string
expected = "hello你好..."
text = FetchExcerpt("<p>hello你好world</p>", "...", 7)
assert.Equal(t, expected, text)
// test mixed string with emoticon
expected = "hello你好😂..."
text = FetchExcerpt("<p>hello你好😂world</p>", "...", 8)
assert.Equal(t, expected, text)
expected = "hello你好"
text = FetchExcerpt("<p>hello你好</p>", "...", 8)
assert.Equal(t, expected, text)
}
func TestUrlTitle(t *testing.T) {
list := []string{
"hello你好😂...",
"这是一个,标题,title",
}
for _, title := range list {
formatTitle := UrlTitle(title)
fmt.Println(formatTitle)
}
}
func TestUrlTitleTable(t *testing.T) {
// Long pure-Arabic title: 50 copies of the same Arabic word, joined by spaces.
// Unidecode of "كيف" is "kyf", so the slug becomes "kyf-" repeated and
// exceeds cutLongTitle's 150-byte cap.
longArabic := strings.Repeat("كيف ", 50)
wantLongArabic := strings.Repeat("kyf-", 37) + "ky" // 37*4 + 2 = 150 bytes
cases := []struct {
name string
title string
want string
}{
{
name: "empty",
title: "",
want: "topic",
},
{
name: "pure latin unchanged",
title: "hello world",
want: "hello-world",
},
{
// Pinyin conversion drops Latin runes by design — matches pre-fix behavior.
name: "pure chinese unchanged",
title: "这是一个,标题,title",
want: "zhe-shi-yi-ge-biao-ti",
},
{
// The fix: previously collapsed to "topic" for all of these scripts.
// Outputs are an ASCII approximation, not linguistically correct
// romanization — see PR description.
name: "arabic transliterated",
title: "كيف حالك",
want: "kyf-hlk",
},
{
name: "mixed latin and arabic",
title: "مرحبا hello",
want: "mrhb-hello",
},
{
name: "thai transliterated",
title: "ไทย ไทย",
want: "aithy-aithy",
},
{
name: "japanese hiragana transliterated",
title: "こんにちは",
want: "konnichiha",
},
{
// Japanese with Han-block kanji is caught by the pre-existing pinyin
// pre-step (Chinese reading, not Japanese), so this path is unchanged
// by this PR. Pinning to document the existing behavior.
name: "japanese kanji goes through pinyin path unchanged",
title: "日本",
want: "ri-ben",
},
{
name: "korean transliterated",
title: "안녕하세요",
want: "annyeonghaseyo",
},
{
name: "hebrew transliterated",
title: "שלום עולם",
want: "shlvm-vlm",
},
{
name: "cyrillic transliterated",
title: "Привет мир",
want: "privet-mir",
},
{
name: "emoji only falls back to topic",
title: "😂😂😂",
want: "topic",
},
{
name: "long arabic truncates at cutLongTitle boundary",
title: longArabic,
want: wantLongArabic,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := UrlTitle(tc.title)
assert.Equal(t, tc.want, got)
})
}
}
func TestUrlTitleTransliterationToggle(t *testing.T) {
defer SetTransliterateNonLatin(true)
SetTransliterateNonLatin(false)
// With transliteration off, pure-Arabic titles collapse to the existing
// "topic" fallback (the pre-fix behavior).
assert.Equal(t, "topic", UrlTitle("كيف حالك"))
SetTransliterateNonLatin(true)
assert.Equal(t, "kyf-hlk", UrlTitle("كيف حالك"))
}
func TestFindFirstMatchedWord(t *testing.T) {
var (
expectedWord,
actualWord string
expectedIndex,
actualIndex int
)
text := "Hello, I have 中文 and 😂 and I am supposed to work fine."
// test find nothing
expectedWord, expectedIndex = "", 0
actualWord, actualIndex = findFirstMatchedWord(text, []string{"youcantfindme"})
assert.Equal(t, expectedWord, actualWord)
assert.Equal(t, expectedIndex, actualIndex)
// test find one word
expectedWord, expectedIndex = "文", 17
actualWord, actualIndex = findFirstMatchedWord(text, []string{"文"})
assert.Equal(t, expectedWord, actualWord)
assert.Equal(t, expectedIndex, actualIndex)
// test find multiple matched words
expectedWord, expectedIndex = "Hello", 0
actualWord, actualIndex = findFirstMatchedWord(text, []string{"Hello", "文"})
assert.Equal(t, expectedWord, actualWord)
assert.Equal(t, expectedIndex, actualIndex)
}
func TestGetRuneRange(t *testing.T) {
var (
expectedBegin,
expectedEnd,
actualBegin,
actualEnd int
)
runeText := []rune("Hello, I have 中文 and 😂.")
runeLen := len(runeText)
// test get range of negative offset and negative limit
expectedBegin, expectedEnd = 0, 0
actualBegin, actualEnd = getRuneRange(runeText, -1, -1)
assert.Equal(t, expectedBegin, actualBegin)
assert.Equal(t, expectedEnd, actualEnd)
// test get range of exceeding offset and exceeding limit
expectedBegin, expectedEnd = runeLen, runeLen
actualBegin, actualEnd = getRuneRange(runeText, runeLen+1, runeLen+1)
assert.Equal(t, expectedBegin, actualBegin)
assert.Equal(t, expectedEnd, actualEnd)
// test get range of normal offset and exceeding limit
expectedBegin, expectedEnd = 3, runeLen
actualBegin, actualEnd = getRuneRange(runeText, 3, runeLen)
assert.Equal(t, expectedBegin, actualBegin)
assert.Equal(t, expectedEnd, actualEnd)
// test get range of normal offset and normal limit
expectedBegin, expectedEnd = 3, 10
actualBegin, actualEnd = getRuneRange(runeText, 3, 7)
assert.Equal(t, expectedBegin, actualBegin)
assert.Equal(t, expectedEnd, actualEnd)
}
func TestFetchRangedExcerpt(t *testing.T) {
var (
expected,
actual string
)
// test english string
expected = "hello..."
actual = FetchRangedExcerpt("<p>hello world</p>", "...", 0, 5)
assert.Equal(t, expected, actual)
// test string with offset
expected = "...llo你好..."
actual = FetchRangedExcerpt("<p>hello你好world</p>", "...", 2, 5)
assert.Equal(t, expected, actual)
// test mixed string with emoticon with offset
expected = "...你好😂..."
actual = FetchRangedExcerpt("<p>hello你好😂world</p>", "...", 5, 3)
assert.Equal(t, expected, actual)
// test mixed string with offset and exceeding limit
expected = "...你好😂world"
actual = FetchRangedExcerpt("<p>hello你好😂world</p>", "...", 5, 100)
assert.Equal(t, expected, actual)
}
func TestCutLongTitle(t *testing.T) {
// Short title, no cutting needed
short := "hello"
assert.Equal(t, short, cutLongTitle(short))
// Exactly max bytes, no cutting needed
exact150 := strings.Repeat("a", 150)
assert.Len(t, cutLongTitle(exact150), 150)
// Just over max bytes, should be cut
exact151 := strings.Repeat("a", 151)
assert.Len(t, cutLongTitle(exact151), 150)
// Multi-byte rune at boundary gets removed properly
asciiPart := strings.Repeat("a", 149) // 149 bytes
multiByteChar := "中" // 3 bytes - will span bytes 149-151
title := asciiPart + multiByteChar // 152 bytes total
assert.Equal(t, asciiPart, cutLongTitle(title))
}
func TestFetchMatchedExcerpt(t *testing.T) {
var (
expected,
actual string
)
html := "<p>Hello, I have 中文 and 😂 and I am supposed to work fine</p>"
// test find nothing
// it should return from the begin with double trimLength text
expected = "Hello, I h..."
actual = FetchMatchedExcerpt(html, []string{"youcantfindme"}, "...", 5)
assert.Equal(t, expected, actual)
// test find the word at the end
// it should return the word beginning with double trimLenth plus len(word)
expected = "... work fine"
actual = FetchMatchedExcerpt(html, []string{"youcant", "fine"}, "...", 3)
assert.Equal(t, expected, actual)
// test find multiple words
// it should return the first matched word with trimmedText
expected = "... have 中文 and 😂..."
actual = FetchMatchedExcerpt(html, []string{"中文", "😂"}, "...", 6)
assert.Equal(t, expected, actual)
}
+55
View File
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package obj
import (
"github.com/apache/answer/internal/base/constant"
"github.com/apache/answer/internal/base/reason"
"github.com/apache/answer/pkg/converter"
"github.com/segmentfault/pacman/errors"
)
// GetObjectTypeStrByObjectID get object key by object id
func GetObjectTypeStrByObjectID(objectID string) (objectTypeStr string, err error) {
if err := checkObjectID(objectID); err != nil {
return "", err
}
objectTypeNumber := converter.StringToInt(objectID[1:4])
objectTypeStr, ok := constant.ObjectTypeNumberMapping[objectTypeNumber]
if ok {
return objectTypeStr, nil
}
return "", errors.BadRequest(reason.ObjectNotFound)
}
// GetObjectTypeNumberByObjectID get object type by object id
func GetObjectTypeNumberByObjectID(objectID string) (objectTypeNumber int, err error) {
if err := checkObjectID(objectID); err != nil {
return 0, err
}
return converter.StringToInt(objectID[1:4]), nil
}
func checkObjectID(objectID string) (err error) {
if len(objectID) < 5 {
return errors.BadRequest(reason.ObjectNotFound)
}
return nil
}
+37
View File
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package random
import (
"crypto/rand"
"encoding/hex"
)
func UsernameSuffix() string {
bytes := make([]byte, 2)
_, _ = rand.Read(bytes)
return hex.EncodeToString(bytes)
}
func Username() string {
bytes := make([]byte, 6)
_, _ = rand.Read(bytes)
return hex.EncodeToString(bytes)
}
+28
View File
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package token
import "github.com/google/uuid"
// GenerateToken generate token
func GenerateToken() string {
uid, _ := uuid.NewV7()
return uid.String()
}
+59
View File
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package uid
import (
"math/rand"
"time"
"github.com/bwmarrin/snowflake"
)
// SnowFlakeID snowflake id
type SnowFlakeID struct {
*snowflake.Node
}
var snowFlakeIDGenerator *SnowFlakeID
func init() {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
node, err := snowflake.NewNode(int64(r.Intn(1000)) + 1)
if err != nil {
panic(err.Error())
}
snowFlakeIDGenerator = &SnowFlakeID{node}
}
func ID() snowflake.ID {
id := snowFlakeIDGenerator.Generate()
return id
}
func IDStr12() string {
id := snowFlakeIDGenerator.Generate()
return id.Base58()
}
func IDStr() string {
id := snowFlakeIDGenerator.Generate()
return id.Base32()
}
+131
View File
@@ -0,0 +1,131 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package uid
import (
"strconv"
"strings"
"github.com/segmentfault/pacman/utils"
)
const salt = int64(100)
// NumToShortID num to string
func NumToShortID(id int64) string {
sid := strconv.FormatInt(id, 10)
if len(sid) < 17 {
return ""
}
sTypeCode := sid[1:4]
sid = sid[4:int32(len(sid))]
id, err := strconv.ParseInt(sid, 10, 64)
if err != nil {
return ""
}
typeCode, err := strconv.ParseInt(sTypeCode, 10, 64)
if err != nil {
return ""
}
code := utils.EnShortID(id, salt)
tcode := utils.EnShortID(typeCode, salt)
return tcode + code
}
// ShortIDToNum string to num
func ShortIDToNum(code string) int64 {
if len(code) < 2 {
return 0
}
scodeType := code[0:2]
code = code[2:int32(len(code))]
id := utils.DeShortID(code, salt)
codeType := utils.DeShortID(scodeType, salt)
return 10000000000000000 + codeType*10000000000000 + id
}
func EnShortID(id string) string {
num, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return id
}
return NumToShortID(num)
}
func DeShortID(sid string) string {
num, err := strconv.ParseInt(sid, 10, 64)
if err != nil {
return strconv.FormatInt(ShortIDToNum(sid), 10)
}
if num < 10000000000000000 {
return strconv.FormatInt(ShortIDToNum(sid), 10)
}
return sid
}
func IsShortID(id string) bool {
num, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return true
}
if num < 10000000000000000 {
return true
}
return false
}
// IsValidNumericID checks whether id can be parsed as a positive int64.
func IsValidNumericID(id string) bool {
id = strings.TrimSpace(id)
if len(id) == 0 {
return false
}
num, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return false
}
return num > 0
}
// NormalizeOptionalID normalizes a raw id parameter.
// It accepts short id and long id, treats empty/null/undefined as "not provided".
// Returns normalized id, whether caller provided a value, and whether value is valid.
func NormalizeOptionalID(raw string) (normalizedID string, provided bool, valid bool) {
raw = strings.TrimSpace(raw)
if len(raw) == 0 || strings.EqualFold(raw, "null") || strings.EqualFold(raw, "undefined") {
return "", false, true
}
normalizedID = DeShortID(raw)
if !IsValidNumericID(normalizedID) {
return "", true, false
}
return normalizedID, true, true
}
// NormalizeRequiredID normalizes a required id parameter.
// Returns normalized id and whether the value is valid.
func NormalizeRequiredID(raw string) (normalizedID string, valid bool) {
normalizedID, provided, valid := NormalizeOptionalID(raw)
if !provided {
return "", false
}
return normalizedID, valid
}
+55
View File
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package writer
import (
"bufio"
"os"
)
// ReplaceFile remove old file and write new file
func ReplaceFile(filePath, content string) error {
_ = os.Remove(filePath)
return WriteFile(filePath, content)
}
// WriteFile write file to path
func WriteFile(filePath, content string) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return err
}
defer func() {
_ = file.Close()
}()
writer := bufio.NewWriter(file)
if _, err := writer.WriteString(content); err != nil {
return err
}
if err := writer.Flush(); err != nil {
return err
}
return nil
}
// MoveFile move file to new path
func MoveFile(oldPath, newPath string) error {
return os.Rename(oldPath, newPath)
}