Skip to content

Commit

Permalink
feat: add type to fs read api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 7, 2022
1 parent ccce6a3 commit 61fa6f3
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.3.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.5.0
github.com/winfsp/cgofuse v1.5.0
gorm.io/driver/mysql v1.3.4
gorm.io/driver/postgres v1.3.7
Expand Down Expand Up @@ -49,7 +50,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/bootstrap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func InitConfig() {
log.Fatalf("failed to create config file: %+v", err)
}
conf.Conf = conf.DefaultConfig()
if !utils.WriteToJson(flags.Config, conf.Conf) {
if !utils.WriteJsonToFile(flags.Config, conf.Conf) {
log.Fatalf("failed to create default config file")
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func initSettings() {
log.Fatalf("failed get setting: %+v", err)
}
}
db.ResetTypeMap()
}

func isActive(key string) bool {
Expand Down Expand Up @@ -80,6 +81,8 @@ func initialSettings() {
{Key: conf.TextTypes, Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx,vtt,srt,ass,rs,lrc", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
{Key: conf.AudioTypes, Value: "mp3,flac,ogg,m4a,wav,opus", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
{Key: conf.VideoTypes, Value: "mp4,mkv,avi,mov,rmvb,webm,flv", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
{Key: conf.ImageTypes, Value: "jpg,tiff,jpeg,png,gif,bmp,svg,ico,swf,webp", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
{Key: conf.OfficeTypes, Value: "doc,docx,xls,xlsx,ppt,pptx,pdf", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
{Key: conf.ProxyTypes, Value: "m3u8", Type: conf.TypeText, Group: model.PREVIEW, Flag: model.PRIVATE},
{Key: conf.PdfViewerUrl, Value: "https://alist-org.github.io/pdf.js/web/viewer.html?file=$url", Type: conf.TypeString, Group: model.PREVIEW},
{Key: conf.AudioAutoplay, Value: "true", Type: conf.TypeBool, Group: model.PREVIEW},
Expand Down
12 changes: 12 additions & 0 deletions internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
TextTypes = "text_types"
AudioTypes = "audio_types"
VideoTypes = "video_types"
ImageTypes = "image_types"
OfficeTypes = "office_types"
ProxyTypes = "proxy_types"
PdfViewerUrl = "pdf_viewer_url"
AudioAutoplay = "audio_autoplay"
Expand All @@ -37,3 +39,13 @@ const (

Token = "token"
)

const (
UNKNOWN = iota
FOLDER
OFFICE
VIDEO
AUDIO
TEXT
IMAGE
)
2 changes: 2 additions & 0 deletions internal/conf/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ var (
var (
Conf *Config
)

var TypesMap = make(map[string][]string)
25 changes: 21 additions & 4 deletions internal/db/settingitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package db

import (
"fmt"
"strings"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand All @@ -11,8 +13,23 @@ import (
var settingsMap map[string]string
var publicSettingsMap map[string]string

func ResetTypeMap() {
settingsMap := GetSettingsMap()
conf.TypesMap[conf.AudioTypes] = strings.Split(settingsMap[conf.AudioTypes], ",")
conf.TypesMap[conf.VideoTypes] = strings.Split(settingsMap[conf.VideoTypes], ",")
conf.TypesMap[conf.ImageTypes] = strings.Split(settingsMap[conf.ImageTypes], ",")
conf.TypesMap[conf.TextTypes] = strings.Split(settingsMap[conf.TextTypes], ",")
conf.TypesMap[conf.OfficeTypes] = strings.Split(settingsMap[conf.OfficeTypes], ",")
}

func settingsUpdate() {
settingsMap = nil
publicSettingsMap = nil
ResetTypeMap()
}

func GetPublicSettingsMap() map[string]string {
if settingsMap == nil || publicSettingsMap == nil {
if publicSettingsMap == nil {
publicSettingsMap = make(map[string]string)
settingItems, err := GetPublicSettingItems()
if err != nil {
Expand Down Expand Up @@ -88,12 +105,12 @@ func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
}

func SaveSettingItems(items []model.SettingItem) error {
settingsMap = nil
settingsUpdate()
return errors.WithStack(db.Save(items).Error)
}

func SaveSettingItem(item model.SettingItem) error {
settingsMap = nil
settingsUpdate()
return errors.WithStack(db.Save(item).Error)
}

Expand All @@ -108,6 +125,6 @@ func DeleteSettingItemByKey(key string) error {
if !old.IsDeprecated() {
return errors.Errorf("setting [%s] is not deprecated", key)
}
settingsMap = nil
settingsUpdate()
return errors.WithStack(db.Delete(&settingItem).Error)
}
23 changes: 22 additions & 1 deletion pkg/utils/file.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package utils

import (
"github.com/alist-org/alist/v3/internal/conf"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/alist-org/alist/v3/internal/conf"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -49,3 +49,24 @@ func CreateTempFile(r io.ReadCloser) (*os.File, error) {
}
return f, nil
}

// GetFileType get file type
func GetFileType(filename string) int {
ext := Ext(filename)
if SliceContains(conf.TypesMap[conf.OfficeTypes], ext) {
return conf.OFFICE
}
if SliceContains(conf.TypesMap[conf.AudioTypes], ext) {
return conf.AUDIO
}
if SliceContains(conf.TypesMap[conf.VideoTypes], ext) {
return conf.VIDEO
}
if SliceContains(conf.TypesMap[conf.ImageTypes], ext) {
return conf.IMAGE
}
if SliceContains(conf.TypesMap[conf.TextTypes], ext) {
return conf.TEXT
}
return conf.UNKNOWN
}
7 changes: 4 additions & 3 deletions pkg/utils/json.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package utils

import (
"io/ioutil"

json "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"io/ioutil"
)

var Json = json.ConfigCompatibleWithStandardLibrary

// WriteToJson write struct to json file
func WriteToJson(src string, conf interface{}) bool {
// WriteJsonToFile write struct to json file
func WriteJsonToFile(src string, conf interface{}) bool {
data, err := Json.MarshalIndent(conf, "", " ")
if err != nil {
log.Errorf("failed convert Conf to []byte:%s", err.Error())
Expand Down
5 changes: 4 additions & 1 deletion pkg/utils/slice.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils

// SliceEqual check if two slices are equal
func SliceEqual[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
Expand All @@ -12,6 +13,7 @@ func SliceEqual[T comparable](a, b []T) bool {
return true
}

// SliceContains check if slice contains element
func SliceContains[T comparable](arr []T, v T) bool {
for _, vv := range arr {
if vv == v {
Expand All @@ -21,9 +23,10 @@ func SliceContains[T comparable](arr []T, v T) bool {
return false
}

// SliceConvert convert slice to another type slice
func SliceConvert[S any, D any](srcS []S, convert func(src S) (D, error)) ([]D, error) {
var res []D
for i, _ := range srcS {
for i := range srcS {
dst, err := convert(srcS[i])
if err != nil {
return nil, err
Expand Down
10 changes: 3 additions & 7 deletions server/handles/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/internal/sign"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
Expand Down Expand Up @@ -85,8 +84,7 @@ func shouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
return true
}
proxyTypes := setting.GetByKey(conf.ProxyTypes)
if strings.Contains(proxyTypes, utils.Ext(filename)) {
if utils.SliceContains(conf.TypesMap[conf.ProxyTypes], utils.Ext(filename)) {
return true
}
return false
Expand All @@ -103,12 +101,10 @@ func canProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
return true
}
proxyTypes := setting.GetByKey(conf.ProxyTypes)
if strings.Contains(proxyTypes, utils.Ext(filename)) {
if utils.SliceContains(conf.TypesMap[conf.ProxyTypes], utils.Ext(filename)) {
return true
}
textTypes := setting.GetByKey(conf.TextTypes)
if strings.Contains(textTypes, utils.Ext(filename)) {
if utils.SliceContains(conf.TypesMap[conf.TextTypes], utils.Ext(filename)) {
return true
}
return false
Expand Down
8 changes: 8 additions & 0 deletions server/handles/fsread.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs"
Expand Down Expand Up @@ -35,6 +36,7 @@ type ObjResp struct {
Modified time.Time `json:"modified"`
Sign string `json:"sign"`
Thumbnail string `json:"thumbnail"`
Type int `json:"type"`
}

type FsListResp struct {
Expand Down Expand Up @@ -171,13 +173,18 @@ func toObjResp(objs []model.Obj) []ObjResp {
if t, ok := obj.(model.Thumbnail); ok {
thumbnail = t.Thumbnail()
}
tp := conf.FOLDER
if !obj.IsDir() {
tp = utils.GetFileType(obj.GetName())
}
resp = append(resp, ObjResp{
Name: obj.GetName(),
Size: obj.GetSize(),
IsDir: obj.IsDir(),
Modified: obj.ModTime(),
Sign: common.Sign(obj),
Thumbnail: thumbnail,
Type: tp,
})
}
return resp
Expand Down Expand Up @@ -256,6 +263,7 @@ func FsGet(c *gin.Context) {
IsDir: obj.IsDir(),
Modified: obj.ModTime(),
Sign: common.Sign(obj),
Type: utils.GetFileType(obj.GetName()),
},
RawURL: rawURL,
Readme: getReadme(meta, req.Path),
Expand Down

0 comments on commit 61fa6f3

Please sign in to comment.