File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed
websites/code/studygolang/src/service Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -256,6 +256,9 @@ func ParseProjectList(pUrl string) error {
256256
257257const OsChinaDomain = "http://www.oschina.net"
258258
259+ // ProjectLogoPrefix 开源项目 logo 前缀
260+ const ProjectLogoPrefix = "plogo"
261+
259262var PresetUsernames = []string {"polaris" , "blov" , "agolangf" , "xuanbao" }
260263
261264// ParseOneProject 处理单个 project
@@ -295,6 +298,15 @@ func ParseOneProject(projectUrl string) error {
295298 logoSelection := doc .Find (".Project .PN img" )
296299 if logoSelection .AttrOr ("title" , "" ) != "" {
297300 project .Logo = logoSelection .AttrOr ("src" , "" )
301+
302+ if ! strings .HasPrefix (project .Logo , "http" ) {
303+ project .Logo = OsChinaDomain + project .Logo
304+ }
305+
306+ project .Logo , err = UploadUrlFile (project .Logo , ProjectLogoPrefix )
307+ if err != nil {
308+ logger .Errorln ("project logo upload error:" , err )
309+ }
298310 }
299311
300312 // 获取项目相关链接
Original file line number Diff line number Diff line change @@ -2,13 +2,18 @@ package service
22
33import (
44 "encoding/json"
5+ "errors"
56 gio "io"
7+ "net/http"
8+ "net/url"
9+ "strings"
610
711 "config"
12+ "logger"
13+
814 "github.com/qiniu/api.v6/conf"
915 "github.com/qiniu/api.v6/io"
1016 "github.com/qiniu/api.v6/rs"
11- "logger"
1217)
1318
1419var uptoken string
@@ -101,3 +106,31 @@ func UploadMemoryFile(r gio.Reader, key string) (err error) {
101106
102107 return
103108}
109+
110+ // UploadUrlFile 将外站图片URL转为本站,如果失败,返回原图
111+ func UploadUrlFile (origUrl , prefix string ) (string , error ) {
112+ if origUrl == "" || strings .Contains (origUrl , "studygolang" ) {
113+ return origUrl , errors .New ("origin image is empty or is studygolang.com" )
114+ }
115+
116+ resp , err := http .Get (origUrl )
117+ if err != nil {
118+ return origUrl , err
119+ }
120+ defer resp .Body .Close ()
121+
122+ objUrl , err := url .Parse (origUrl )
123+ if err != nil {
124+ return origUrl , err
125+ }
126+
127+ filename := objUrl .Path [strings .LastIndex (objUrl .Path , "/" ):]
128+ uri := prefix + filename
129+
130+ err = UploadMemoryFile (resp .Body , uri )
131+ if err != nil {
132+ return origUrl , err
133+ }
134+
135+ return "http://studygolang.qiniudn.com/" + uri , nil
136+ }
You can’t perform that action at this time.
0 commit comments