Skip to content

Commit dc0253b

Browse files
KN4CK3Rdelvhlunnytechknowlogick
authored
Replace ServeStream with ServeContent (#20903)
* Replace ServeStream with ServeContent. * Update modules/timeutil/timestamp.go Co-authored-by: delvh <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 5e232e8 commit dc0253b

File tree

17 files changed

+31
-41
lines changed

17 files changed

+31
-41
lines changed

modules/context/context.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,7 @@ func (ctx *Context) SetServeHeaders(filename string) {
358358
}
359359

360360
// ServeContent serves content to http request
361-
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) {
362-
modTime := time.Now()
363-
for _, p := range params {
364-
switch v := p.(type) {
365-
case time.Time:
366-
modTime = v
367-
}
368-
}
361+
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, modTime time.Time) {
369362
ctx.SetServeHeaders(name)
370363
http.ServeContent(ctx.Resp, ctx.Req, name, modTime, r)
371364
}
@@ -382,15 +375,6 @@ func (ctx *Context) ServeFile(file string, names ...string) {
382375
http.ServeFile(ctx.Resp, ctx.Req, file)
383376
}
384377

385-
// ServeStream serves file via io stream
386-
func (ctx *Context) ServeStream(rd io.Reader, name string) {
387-
ctx.SetServeHeaders(name)
388-
_, err := io.Copy(ctx.Resp, rd)
389-
if err != nil {
390-
ctx.ServerError("Download file failed", err)
391-
}
392-
}
393-
394378
// UploadStream returns the request body or the first form file
395379
// Only form files need to get closed.
396380
func (ctx *Context) UploadStream() (rd io.ReadCloser, needToClose bool, err error) {

modules/timeutil/timestamp.go

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func (ts TimeStamp) AsTime() (tm time.Time) {
5454
return ts.AsTimeInLocation(setting.DefaultUILocation)
5555
}
5656

57+
// AsLocalTime convert timestamp as time.Time in local location
58+
func (ts TimeStamp) AsLocalTime() time.Time {
59+
return time.Unix(int64(ts), 0)
60+
}
61+
5762
// AsTimeInLocation convert timestamp as time.Time in Local locale
5863
func (ts TimeStamp) AsTimeInLocation(loc *time.Location) (tm time.Time) {
5964
tm = time.Unix(int64(ts), 0).In(loc)

routers/api/packages/composer/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func createPackageMetadataResponse(registryURL string, pds []*packages_model.Pac
9999
Name: pd.Package.Name,
100100
Version: pd.Version.Version,
101101
Type: packageType,
102-
Created: time.Unix(int64(pd.Version.CreatedUnix), 0),
102+
Created: pd.Version.CreatedUnix.AsLocalTime(),
103103
Metadata: pd.Metadata.(*composer_module.Metadata),
104104
Dist: Dist{
105105
Type: "zip",

routers/api/packages/composer/composer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func DownloadPackageFile(ctx *context.Context) {
184184
}
185185
defer s.Close()
186186

187-
ctx.ServeStream(s, pf.Name)
187+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
188188
}
189189

190190
// UploadPackage creates a new package

routers/api/packages/conan/conan.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func downloadFile(ctx *context.Context, fileFilter stringSet, fileKey string) {
475475
}
476476
defer s.Close()
477477

478-
ctx.ServeStream(s, pf.Name)
478+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
479479
}
480480

481481
// DeleteRecipeV1 deletes the requested recipe(s)
@@ -723,7 +723,7 @@ func listRevisions(ctx *context.Context, revisions []*conan_model.PropertyValue)
723723

724724
revs := make([]*revisionInfo, 0, len(revisions))
725725
for _, rev := range revisions {
726-
revs = append(revs, &revisionInfo{Revision: rev.Value, Time: time.Unix(int64(rev.CreatedUnix), 0)})
726+
revs = append(revs, &revisionInfo{Revision: rev.Value, Time: rev.CreatedUnix.AsLocalTime()})
727727
}
728728

729729
jsonResponse(ctx, http.StatusOK, &RevisionList{revs})
@@ -743,7 +743,7 @@ func LatestRecipeRevision(ctx *context.Context) {
743743
return
744744
}
745745

746-
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: time.Unix(int64(revision.CreatedUnix), 0)})
746+
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: revision.CreatedUnix.AsLocalTime()})
747747
}
748748

749749
// LatestPackageRevision gets the latest package revision
@@ -760,7 +760,7 @@ func LatestPackageRevision(ctx *context.Context) {
760760
return
761761
}
762762

763-
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: time.Unix(int64(revision.CreatedUnix), 0)})
763+
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: revision.CreatedUnix.AsLocalTime()})
764764
}
765765

766766
// ListRecipeRevisionFiles gets a list of all recipe revision files

routers/api/packages/generic/generic.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func DownloadPackageFile(ctx *context.Context) {
5353
}
5454
defer s.Close()
5555

56-
ctx.ServeStream(s, pf.Name)
56+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
5757
}
5858

5959
// UploadPackage uploads the specific generic package.

routers/api/packages/helm/helm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func DownloadPackageFile(ctx *context.Context) {
138138
}
139139
defer s.Close()
140140

141-
ctx.ServeStream(s, pf.Name)
141+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
142142
}
143143

144144
// UploadPackage creates a new package

routers/api/packages/maven/maven.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func servePackageFile(ctx *context.Context, params parameters) {
177177
}
178178
}
179179

180-
ctx.ServeStream(s, pf.Name)
180+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
181181
}
182182

183183
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.

routers/api/packages/npm/npm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func DownloadPackageFile(ctx *context.Context) {
103103
}
104104
defer s.Close()
105105

106-
ctx.ServeStream(s, pf.Name)
106+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
107107
}
108108

109109
// UploadPackage creates a new package

routers/api/packages/nuget/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func createRegistrationLeafResponse(l *linkBuilder, pd *packages_model.PackageDe
176176
return &RegistrationLeafResponse{
177177
Type: []string{"Package", "http://schema.nuget.org/catalog#Permalink"},
178178
Listed: true,
179-
Published: time.Unix(int64(pd.Version.CreatedUnix), 0),
179+
Published: pd.Version.CreatedUnix.AsLocalTime(),
180180
RegistrationLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
181181
PackageContentURL: l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version),
182182
RegistrationIndexURL: l.GetRegistrationIndexURL(pd.Package.Name),

routers/api/packages/nuget/nuget.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func DownloadPackageFile(ctx *context.Context) {
179179
}
180180
defer s.Close()
181181

182-
ctx.ServeStream(s, pf.Name)
182+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
183183
}
184184

185185
// UploadPackage creates a new package with the metadata contained in the uploaded nupgk file
@@ -378,7 +378,7 @@ func DownloadSymbolFile(ctx *context.Context) {
378378
return
379379
}
380380

381-
s, _, err := packages_service.GetPackageFileStream(ctx, pfs[0])
381+
s, pf, err := packages_service.GetPackageFileStream(ctx, pfs[0])
382382
if err != nil {
383383
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist {
384384
apiError(ctx, http.StatusNotFound, err)
@@ -389,7 +389,7 @@ func DownloadSymbolFile(ctx *context.Context) {
389389
}
390390
defer s.Close()
391391

392-
ctx.ServeStream(s, pfs[0].Name)
392+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
393393
}
394394

395395
// DeletePackage hard deletes the package

routers/api/packages/pub/pub.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func packageDescriptorToMetadata(baseURL string, pd *packages_model.PackageDescr
6969
return &versionMetadata{
7070
Version: pd.Version.Version,
7171
ArchiveURL: fmt.Sprintf("%s/files/%s.tar.gz", baseURL, url.PathEscape(pd.Version.Version)),
72-
Published: time.Unix(int64(pd.Version.CreatedUnix), 0),
72+
Published: pd.Version.CreatedUnix.AsLocalTime(),
7373
Pubspec: pd.Metadata.(*pub_module.Metadata).Pubspec,
7474
}
7575
}
@@ -271,5 +271,5 @@ func DownloadPackageFile(ctx *context.Context) {
271271
}
272272
defer s.Close()
273273

274-
ctx.ServeStream(s, pf.Name)
274+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
275275
}

routers/api/packages/pypi/pypi.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func DownloadPackageFile(ctx *context.Context) {
9090
}
9191
defer s.Close()
9292

93-
ctx.ServeStream(s, pf.Name)
93+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
9494
}
9595

9696
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.

routers/api/packages/rubygems/rubygems.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func DownloadPackageFile(ctx *context.Context) {
188188
}
189189
defer s.Close()
190190

191-
ctx.ServeStream(s, pf.Name)
191+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
192192
}
193193

194194
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.

routers/web/repo/repo.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ func download(ctx *context.Context, archiveName string, archiver *repo_model.Rep
468468
return
469469
}
470470
defer fr.Close()
471-
ctx.ServeStream(fr, downloadName)
471+
472+
ctx.ServeContent(downloadName, fr, archiver.CreatedUnix.AsLocalTime())
472473
}
473474

474475
// InitiateDownload will enqueue an archival request, as needed. It may submit

routers/web/user/package.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,5 +393,5 @@ func DownloadPackageFile(ctx *context.Context) {
393393
}
394394
defer s.Close()
395395

396-
ctx.ServeStream(s, pf.Name)
396+
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
397397
}

services/packages/packages.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func Cleanup(unused context.Context, olderThan time.Duration) error {
402402
}
403403

404404
// GetFileStreamByPackageNameAndVersion returns the content of the specific package file
405-
func GetFileStreamByPackageNameAndVersion(ctx context.Context, pvi *PackageInfo, pfi *PackageFileInfo) (io.ReadCloser, *packages_model.PackageFile, error) {
405+
func GetFileStreamByPackageNameAndVersion(ctx context.Context, pvi *PackageInfo, pfi *PackageFileInfo) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
406406
log.Trace("Getting package file stream: %v, %v, %s, %s, %s, %s", pvi.Owner.ID, pvi.PackageType, pvi.Name, pvi.Version, pfi.Filename, pfi.CompositeKey)
407407

408408
pv, err := packages_model.GetVersionByNameAndVersion(ctx, pvi.Owner.ID, pvi.PackageType, pvi.Name, pvi.Version)
@@ -418,7 +418,7 @@ func GetFileStreamByPackageNameAndVersion(ctx context.Context, pvi *PackageInfo,
418418
}
419419

420420
// GetFileStreamByPackageVersionAndFileID returns the content of the specific package file
421-
func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_model.User, versionID, fileID int64) (io.ReadCloser, *packages_model.PackageFile, error) {
421+
func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_model.User, versionID, fileID int64) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
422422
log.Trace("Getting package file stream: %v, %v, %v", owner.ID, versionID, fileID)
423423

424424
pv, err := packages_model.GetVersionByID(ctx, versionID)
@@ -449,7 +449,7 @@ func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_mod
449449
}
450450

451451
// GetFileStreamByPackageVersion returns the content of the specific package file
452-
func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.PackageVersion, pfi *PackageFileInfo) (io.ReadCloser, *packages_model.PackageFile, error) {
452+
func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.PackageVersion, pfi *PackageFileInfo) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
453453
pf, err := packages_model.GetFileForVersionByName(ctx, pv.ID, pfi.Filename, pfi.CompositeKey)
454454
if err != nil {
455455
return nil, nil, err
@@ -459,7 +459,7 @@ func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.Packa
459459
}
460460

461461
// GetPackageFileStream returns the content of the specific package file
462-
func GetPackageFileStream(ctx context.Context, pf *packages_model.PackageFile) (io.ReadCloser, *packages_model.PackageFile, error) {
462+
func GetPackageFileStream(ctx context.Context, pf *packages_model.PackageFile) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
463463
pb, err := packages_model.GetBlobByID(ctx, pf.BlobID)
464464
if err != nil {
465465
return nil, nil, err

0 commit comments

Comments
 (0)