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

fix: 解决在线文本编辑器打开 GBK 编码文件乱码问题 #5096

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions backend/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/registry"
Expand Down Expand Up @@ -870,16 +871,16 @@ func calculateNetwork(network map[string]types.NetworkStats) (float64, float64)
return rx, tx
}

func checkImageExist(client *client.Client, image string) bool {
images, err := client.ImageList(context.Background(), types.ImageListOptions{})
func checkImageExist(client *client.Client, imageItem string) bool {
images, err := client.ImageList(context.Background(), image.ListOptions{})
if err != nil {
fmt.Println(err)
return false
}

for _, img := range images {
for _, tag := range img.RepoTags {
if tag == image || tag == image+":latest" {
if tag == imageItem || tag == imageItem+":latest" {
return true
}
}
Expand Down
21 changes: 21 additions & 0 deletions backend/app/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"fmt"
"io"
"io/fs"
"os"
"os/exec"
Expand All @@ -14,6 +15,9 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
"github.com/1Panel-dev/1Panel/backend/buserr"
"github.com/1Panel-dev/1Panel/backend/constant"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"

"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
Expand Down Expand Up @@ -227,6 +231,23 @@ func (f *FileService) GetContent(op request.FileContentReq) (response.FileInfo,
Path: op.Path,
Expand: true,
})

content := []byte(info.Content)
var decodeName string
if len(content) > 1024 {
_, decodeName, _ = charset.DetermineEncoding(content[:1024], "")
} else {
_, decodeName, _ = charset.DetermineEncoding(content, "")
}
if decodeName == "windows-1252" {
reader := strings.NewReader(info.Content)
item := transform.NewReader(reader, simplifiedchinese.GBK.NewDecoder())
contents, err := io.ReadAll(item)
if err != nil {
return response.FileInfo{}, err
}
info.Content = string(contents)
}
if err != nil {
return response.FileInfo{}, err
}
Expand Down
9 changes: 5 additions & 4 deletions backend/utils/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"

"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (c Client) CreateNetwork(name string) error {
}

func (c Client) DeleteImage(imageID string) error {
if _, err := c.cli.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{Force: true}); err != nil {
if _, err := c.cli.ImageRemove(context.Background(), imageID, image.RemoveOptions{Force: true}); err != nil {
return err
}
return nil
Expand All @@ -105,7 +106,7 @@ func (c Client) PullImage(imageName string, force bool) error {
return nil
}
}
if _, err := c.cli.ImagePull(context.Background(), imageName, types.ImagePullOptions{}); err != nil {
if _, err := c.cli.ImagePull(context.Background(), imageName, image.PullOptions{}); err != nil {
return err
}
return nil
Expand All @@ -114,7 +115,7 @@ func (c Client) PullImage(imageName string, force bool) error {
func (c Client) GetImageIDByName(imageName string) (string, error) {
filter := filters.NewArgs()
filter.Add("reference", imageName)
list, err := c.cli.ImageList(context.Background(), types.ImageListOptions{
list, err := c.cli.ImageList(context.Background(), image.ListOptions{
Filters: filter,
})
if err != nil {
Expand All @@ -129,7 +130,7 @@ func (c Client) GetImageIDByName(imageName string) (string, error) {
func (c Client) CheckImageExist(imageName string) (bool, error) {
filter := filters.NewArgs()
filter.Add("reference", imageName)
list, err := c.cli.ImageList(context.Background(), types.ImageListOptions{
list, err := c.cli.ImageList(context.Background(), image.ListOptions{
Filters: filter,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/utils/mysql/client/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
)

Expand Down Expand Up @@ -417,7 +417,7 @@ func loadImage(dbType, version string) (string, error) {
if err != nil {
fmt.Println(err)
}
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
images, err := cli.ImageList(context.Background(), image.ListOptions{})
if err != nil {
fmt.Println(err)
}
Expand Down
3 changes: 2 additions & 1 deletion backend/utils/postgresql/client/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/pkg/errors"

"github.com/1Panel-dev/1Panel/backend/buserr"
Expand Down Expand Up @@ -270,7 +271,7 @@ func loadImageTag() (string, error) {
return "", err
}
defer client.Close()
images, err := client.ImageList(context.Background(), types.ImageListOptions{})
images, err := client.ImageList(context.Background(), image.ListOptions{})
if err != nil {
return "", err
}
Expand Down
Loading