File tree 2 files changed +46
-1
lines changed
websites/code/studygolang/src/service
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 {
256
256
257
257
const OsChinaDomain = "http://www.oschina.net"
258
258
259
+ // ProjectLogoPrefix 开源项目 logo 前缀
260
+ const ProjectLogoPrefix = "plogo"
261
+
259
262
var PresetUsernames = []string {"polaris" , "blov" , "agolangf" , "xuanbao" }
260
263
261
264
// ParseOneProject 处理单个 project
@@ -295,6 +298,15 @@ func ParseOneProject(projectUrl string) error {
295
298
logoSelection := doc .Find (".Project .PN img" )
296
299
if logoSelection .AttrOr ("title" , "" ) != "" {
297
300
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
+ }
298
310
}
299
311
300
312
// 获取项目相关链接
Original file line number Diff line number Diff line change @@ -2,13 +2,18 @@ package service
2
2
3
3
import (
4
4
"encoding/json"
5
+ "errors"
5
6
gio "io"
7
+ "net/http"
8
+ "net/url"
9
+ "strings"
6
10
7
11
"config"
12
+ "logger"
13
+
8
14
"github.com/qiniu/api.v6/conf"
9
15
"github.com/qiniu/api.v6/io"
10
16
"github.com/qiniu/api.v6/rs"
11
- "logger"
12
17
)
13
18
14
19
var uptoken string
@@ -101,3 +106,31 @@ func UploadMemoryFile(r gio.Reader, key string) (err error) {
101
106
102
107
return
103
108
}
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