Feat: read CORS config from file
This commit is contained in:
parent
c102c74d6d
commit
ad02a659a6
5 changed files with 42 additions and 10 deletions
|
@ -96,7 +96,8 @@ func addDefaultSettings() {
|
||||||
solid #e9e9e9;"bgcolor="#fff"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="alert alert-warning"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #FF9F00; margin: 0; padding: 20px;"align="center"bgcolor="#FF9F00"valign="top">容量超额警告</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;"valign="top"><table width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">亲爱的<strong style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">{userName}</strong>:</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">由于{notifyReason},您在{siteTitle}的账户的容量使用超出配额,您将无法继续上传新文件,请尽快清理文件,否则我们将会禁用您的账户。</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top"><a href="{siteUrl}Login"class="btn-primary"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">登录{siteTitle}</a></td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">感谢您选择{siteTitle}。</td></tr></tbody></table></td></tr></tbody></table><div class="footer"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;"><table width="100%"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;"align="center"valign="top">此邮件由系统自动发送,请不要直接回复。</td></tr></tbody></table></div></div></td><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td></tr></tbody></table>`, Type: "mail_template"},
|
solid #e9e9e9;"bgcolor="#fff"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="alert alert-warning"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color: #fff; font-weight: 500; text-align: center; border-radius: 3px 3px 0 0; background-color: #FF9F00; margin: 0; padding: 20px;"align="center"bgcolor="#FF9F00"valign="top">容量超额警告</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;"valign="top"><table width="100%"cellpadding="0"cellspacing="0"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">亲爱的<strong style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">{userName}</strong>:</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">由于{notifyReason},您在{siteTitle}的账户的容量使用超出配额,您将无法继续上传新文件,请尽快清理文件,否则我们将会禁用您的账户。</td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top"><a href="{siteUrl}Login"class="btn-primary"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">登录{siteTitle}</a></td></tr><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;"valign="top">感谢您选择{siteTitle}。</td></tr></tbody></table></td></tr></tbody></table><div class="footer"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;"><table width="100%"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tbody><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block"style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;"align="center"valign="top">此邮件由系统自动发送,请不要直接回复。</td></tr></tbody></table></div></div></td><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;"valign="top"></td></tr></tbody></table>`, Type: "mail_template"},
|
||||||
{Name: "ban_time", Value: `10`, Type: "storage_policy"},
|
{Name: "ban_time", Value: `10`, Type: "storage_policy"},
|
||||||
{Name: "maxEditSize", Value: `100000`, Type: "file_edit"},
|
{Name: "maxEditSize", Value: `100000`, Type: "file_edit"},
|
||||||
{Name: "timeout", Value: `3600`, Type: "oss"},
|
{Name: "oss_timeout", Value: `3600`, Type: "timeout"},
|
||||||
|
{Name: "local_archive_timeout", Value: `30`, Type: "timeout"},
|
||||||
{Name: "allowdVisitorDownload", Value: `false`, Type: "share"},
|
{Name: "allowdVisitorDownload", Value: `false`, Type: "share"},
|
||||||
{Name: "login_captcha", Value: `0`, Type: "login"},
|
{Name: "login_captcha", Value: `0`, Type: "login"},
|
||||||
{Name: "qq_login", Value: `0`, Type: "login"},
|
{Name: "qq_login", Value: `0`, Type: "login"},
|
||||||
|
|
|
@ -51,6 +51,16 @@ type thumb struct {
|
||||||
FileSuffix string `validate:"min=1"`
|
FileSuffix string `validate:"min=1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 跨域配置
|
||||||
|
type cors struct {
|
||||||
|
AllowAllOrigins bool
|
||||||
|
AllowOrigins []string
|
||||||
|
AllowMethods []string
|
||||||
|
AllowHeaders []string
|
||||||
|
AllowCredentials bool
|
||||||
|
ExposeHeaders []string
|
||||||
|
}
|
||||||
|
|
||||||
var cfg *ini.File
|
var cfg *ini.File
|
||||||
|
|
||||||
// Init 初始化配置文件
|
// Init 初始化配置文件
|
||||||
|
@ -69,6 +79,7 @@ func Init(path string) {
|
||||||
"Captcha": CaptchaConfig,
|
"Captcha": CaptchaConfig,
|
||||||
"Redis": RedisConfig,
|
"Redis": RedisConfig,
|
||||||
"Thumbnail": ThumbConfig,
|
"Thumbnail": ThumbConfig,
|
||||||
|
"CORS": CORSConfig,
|
||||||
}
|
}
|
||||||
for sectionName, sectionStruct := range sections {
|
for sectionName, sectionStruct := range sections {
|
||||||
err = mapSection(sectionName, sectionStruct)
|
err = mapSection(sectionName, sectionStruct)
|
||||||
|
|
|
@ -34,6 +34,16 @@ var CaptchaConfig = &captcha{
|
||||||
CaptchaLen: 6,
|
CaptchaLen: 6,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CORSConfig 跨域配置
|
||||||
|
var CORSConfig = &cors{
|
||||||
|
AllowAllOrigins: false,
|
||||||
|
AllowOrigins: []string{"UNSET"},
|
||||||
|
AllowMethods: []string{"PUT", "POST", "GET", "OPTIONS"},
|
||||||
|
AllowHeaders: []string{"Cookie", "Content-Length", "Content-Type", "X-Path", "X-FileName"},
|
||||||
|
AllowCredentials: true,
|
||||||
|
ExposeHeaders: nil,
|
||||||
|
}
|
||||||
|
|
||||||
var ThumbConfig = &thumb{
|
var ThumbConfig = &thumb{
|
||||||
MaxWidth: 400,
|
MaxWidth: 400,
|
||||||
MaxHeight: 300,
|
MaxHeight: 300,
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/HFO4/cloudreve/middleware"
|
"github.com/HFO4/cloudreve/middleware"
|
||||||
"github.com/HFO4/cloudreve/pkg/conf"
|
"github.com/HFO4/cloudreve/pkg/conf"
|
||||||
"github.com/HFO4/cloudreve/routers/controllers"
|
"github.com/HFO4/cloudreve/routers/controllers"
|
||||||
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-contrib/pprof"
|
"github.com/gin-contrib/pprof"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
@ -18,13 +19,17 @@ func InitRouter() *gin.Engine {
|
||||||
*/
|
*/
|
||||||
r.Use(middleware.Session(conf.SystemConfig.SessionSecret))
|
r.Use(middleware.Session(conf.SystemConfig.SessionSecret))
|
||||||
|
|
||||||
//// CORS TODO: 根据配置文件来
|
// CORS TODO: 根据配置文件来
|
||||||
//r.Use(cors.New(cors.Config{
|
if conf.CORSConfig.AllowOrigins[0] != "UNSET" || conf.CORSConfig.AllowAllOrigins {
|
||||||
// AllowOrigins: []string{"http://localhost:3000"},
|
r.Use(cors.New(cors.Config{
|
||||||
// AllowMethods: []string{"PUT", "POST", "GET", "OPTIONS"},
|
AllowOrigins: conf.CORSConfig.AllowOrigins,
|
||||||
// AllowHeaders: []string{"Cookie", "Content-Length", "Content-Type", "X-Path", "X-FileName"},
|
AllowAllOrigins: conf.CORSConfig.AllowAllOrigins,
|
||||||
// AllowCredentials: true,
|
AllowMethods: conf.CORSConfig.AllowHeaders,
|
||||||
//}))
|
AllowHeaders: conf.CORSConfig.AllowHeaders,
|
||||||
|
AllowCredentials: conf.CORSConfig.AllowCredentials,
|
||||||
|
ExposeHeaders: conf.CORSConfig.ExposeHeaders,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
// 测试模式加入Mock助手中间件
|
// 测试模式加入Mock助手中间件
|
||||||
if gin.Mode() == gin.TestMode {
|
if gin.Mode() == gin.TestMode {
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/HFO4/cloudreve/pkg/util"
|
"github.com/HFO4/cloudreve/pkg/util"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -60,14 +61,18 @@ func (service *ItemService) Archive(ctx context.Context, c *gin.Context) seriali
|
||||||
return serializer.Err(serializer.CodeNotSet, "无法解析站点URL", err)
|
return serializer.Err(serializer.CodeNotSet, "无法解析站点URL", err)
|
||||||
}
|
}
|
||||||
zipID := util.RandStringRunes(16)
|
zipID := util.RandStringRunes(16)
|
||||||
|
ttl, err := strconv.Atoi(model.GetSettingByName("local_archive_timeout"))
|
||||||
|
if err != nil {
|
||||||
|
ttl = 30
|
||||||
|
}
|
||||||
signedURI, err := auth.SignURI(
|
signedURI, err := auth.SignURI(
|
||||||
fmt.Sprintf("/api/v3/file/archive/%s/archive.zip", zipID),
|
fmt.Sprintf("/api/v3/file/archive/%s/archive.zip", zipID),
|
||||||
time.Now().Unix()+30,
|
time.Now().Unix()+int64(ttl),
|
||||||
)
|
)
|
||||||
finalURL := siteURL.ResolveReference(signedURI).String()
|
finalURL := siteURL.ResolveReference(signedURI).String()
|
||||||
|
|
||||||
// 将压缩文件记录存入缓存
|
// 将压缩文件记录存入缓存
|
||||||
err = cache.Set("archive_"+zipID, zipFile, 30)
|
err = cache.Set("archive_"+zipID, zipFile, ttl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodeIOFailed, "无法写入缓存", err)
|
return serializer.Err(serializer.CodeIOFailed, "无法写入缓存", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue