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

Lines changed: 1 addition & 17 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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),

0 commit comments

Comments
 (0)