@@ -66,7 +66,7 @@ func (f *FileDao) CheckCommitHf(repoType, orgRepo, commit, authorization string)
6666 return resp .StatusCode , myerr .New ("request commit err" )
6767}
6868
69- func (f * FileDao ) GetFileCommitSha (repoType , orgRepo , commit , authorization string ) (string , error ) {
69+ func (f * FileDao ) GetFileCommitSha (repoType , orgRepo , commit , authorization string , source string ) (string , error ) {
7070 metaShaKey := GetMetaShaRepoKey (orgRepo , commit , authorization )
7171 if v , ok := f .baseData .Cache .Get (metaShaKey ); ok {
7272 return v .(string ), nil
@@ -76,34 +76,41 @@ func (f *FileDao) GetFileCommitSha(repoType, orgRepo, commit, authorization stri
7676 err error
7777 )
7878 if config .SysConfig .Online () {
79- code , sha , err := f .getCommitHfRemote (repoType , orgRepo , commit , authorization )
80- if err != nil {
81- return "" , myerr .NewAppendCode (code , fmt .Sprintf ("request fail.%v" , err ))
82- }
83- if code != http .StatusOK && code != http .StatusTemporaryRedirect {
84- zap .S ().Errorf ("getFileCommitSha %s code:%d" , orgRepo , code )
85- if code == http .StatusNotFound {
86- return "" , myerr .NewAppendCode (code , "未找到该资源。" )
87- } else if code == http .StatusUnauthorized || code == http .StatusForbidden {
88- return "" , myerr .NewAppendCode (code , "没有该资源的访问权限,请联系管理员。" )
89- } else {
90- return "" , myerr .NewAppendCode (code , fmt .Sprintf ("请求资源失败.(%d)" , code ))
91- }
92- } else {
93- commitSha = sha
94- }
95- f .baseData .Cache .Set (metaShaKey , commitSha , config .SysConfig .GetDefaultExpiration ())
96- f .baseData .Cache .Set (GetMetaShaRepoKey (orgRepo , commitSha , authorization ), commitSha , config .SysConfig .GetDefaultExpiration ())
97- return commitSha , nil
79+ goto remoteRequestMeta
9880 }
9981 commitSha , err = f .GetCommitHfOffline (repoType , orgRepo , commit )
10082 if err != nil {
101- zap .S ().Errorf ("getFileCommitSha GetCommitHfOffline err.%v" , err )
83+ if source == "file" {
84+ // 若只是发起文件下载(先在线后离线),将不会校验meta文件是否存在,没有就创建,主要是看文件本身是否存在。
85+ goto remoteRequestMeta
86+ }
87+ zap .S ().Warnf ("getFileCommitSha GetCommitHfOffline err.%v" , err )
10288 return "" , myerr .NewAppendCode (http .StatusNotFound , fmt .Sprintf ("%s is not found" , orgRepo ))
10389 }
10490 f .baseData .Cache .Set (metaShaKey , commitSha , config .SysConfig .GetDefaultExpiration ())
10591 f .baseData .Cache .Set (GetMetaShaRepoKey (orgRepo , commitSha , authorization ), commitSha , config .SysConfig .GetDefaultExpiration ())
10692 return commitSha , nil
93+
94+ remoteRequestMeta:
95+ code , sha , err := f .getCommitHfRemote (repoType , orgRepo , commit , authorization )
96+ if err != nil {
97+ return "" , myerr .NewAppendCode (code , fmt .Sprintf ("request fail.%v" , err ))
98+ }
99+ if code != http .StatusOK && code != http .StatusTemporaryRedirect {
100+ zap .S ().Errorf ("getFileCommitSha %s code:%d" , orgRepo , code )
101+ if code == http .StatusNotFound {
102+ return "" , myerr .NewAppendCode (code , "未找到该资源。" )
103+ } else if code == http .StatusUnauthorized || code == http .StatusForbidden {
104+ return "" , myerr .NewAppendCode (code , "没有该资源的访问权限,请联系管理员。" )
105+ } else {
106+ return "" , myerr .NewAppendCode (code , fmt .Sprintf ("请求资源失败.(%d)" , code ))
107+ }
108+ } else {
109+ commitSha = sha
110+ }
111+ f .baseData .Cache .Set (metaShaKey , commitSha , config .SysConfig .GetDefaultExpiration ())
112+ f .baseData .Cache .Set (GetMetaShaRepoKey (orgRepo , commitSha , authorization ), commitSha , config .SysConfig .GetDefaultExpiration ())
113+ return commitSha , nil
107114}
108115
109116// 若为离线或在线请求失败,将进行本地仓库查找。
@@ -176,7 +183,7 @@ func (f *FileDao) FileGetGenerator(c echo.Context, repoType, orgRepo, commit, fi
176183 pathInfo , err := f .GetPathsInfo (hfUri , repoType , orgRepo , commit , authorization , fileName )
177184 if err != nil {
178185 if e , ok := err .(myerr.Error ); ok {
179- zap .S ().Errorf ("GetPathsInfo code:%d, err:%v" , e .StatusCode (), err )
186+ zap .S ().Warnf ("GetPathsInfo code:%d, err:%v" , e .StatusCode (), err )
180187 return util .ErrorEntryUnknown (c , e .StatusCode (), e .Error ())
181188 }
182189 zap .S ().Errorf ("GetPathsInfo err:%v" , err )
@@ -448,8 +455,10 @@ func (f *FileDao) FileChunkGet(c echo.Context, taskParam *downloader.TaskParam,
448455 taskParam .Context = ctx
449456 taskParam .ResponseChan = responseChan
450457 taskParam .Cancel = cancel
451- go f .downloaderDao .FileDownload (startPos , endPos , isInnerRequest , taskParam )
452- if err := util .ResponseStream (c , fmt .Sprintf ("%s/%s" , taskParam .OrgRepo , taskParam .FileName ), respHeaders , responseChan ); err != nil {
458+ fileErrCh := make (chan error , 1 )
459+ fileName := fmt .Sprintf ("%s/%s" , taskParam .OrgRepo , taskParam .FileName )
460+ go f .downloaderDao .FileDownload (fileErrCh , startPos , endPos , isInnerRequest , taskParam )
461+ if err := util .ResponseStream (ctx , c , fileName , respHeaders , responseChan , fileErrCh ); err != nil {
453462 zap .S ().Errorf ("FileChunkGet stream err.%v" , err )
454463 return util .ErrorProxyTimeout (c )
455464 }
0 commit comments