mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-25 06:14:12 +01:00
Replace ServeStream
with ServeContent
(#20903)
* Replace ServeStream with ServeContent. * Update modules/timeutil/timestamp.go Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
5e232e86de
commit
dc0253b063
17 changed files with 31 additions and 41 deletions
|
@ -358,14 +358,7 @@ func (ctx *Context) SetServeHeaders(filename string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeContent serves content to http request
|
// ServeContent serves content to http request
|
||||||
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) {
|
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, modTime time.Time) {
|
||||||
modTime := time.Now()
|
|
||||||
for _, p := range params {
|
|
||||||
switch v := p.(type) {
|
|
||||||
case time.Time:
|
|
||||||
modTime = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ctx.SetServeHeaders(name)
|
ctx.SetServeHeaders(name)
|
||||||
http.ServeContent(ctx.Resp, ctx.Req, name, modTime, r)
|
http.ServeContent(ctx.Resp, ctx.Req, name, modTime, r)
|
||||||
}
|
}
|
||||||
|
@ -382,15 +375,6 @@ func (ctx *Context) ServeFile(file string, names ...string) {
|
||||||
http.ServeFile(ctx.Resp, ctx.Req, file)
|
http.ServeFile(ctx.Resp, ctx.Req, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeStream serves file via io stream
|
|
||||||
func (ctx *Context) ServeStream(rd io.Reader, name string) {
|
|
||||||
ctx.SetServeHeaders(name)
|
|
||||||
_, err := io.Copy(ctx.Resp, rd)
|
|
||||||
if err != nil {
|
|
||||||
ctx.ServerError("Download file failed", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadStream returns the request body or the first form file
|
// UploadStream returns the request body or the first form file
|
||||||
// Only form files need to get closed.
|
// Only form files need to get closed.
|
||||||
func (ctx *Context) UploadStream() (rd io.ReadCloser, needToClose bool, err error) {
|
func (ctx *Context) UploadStream() (rd io.ReadCloser, needToClose bool, err error) {
|
||||||
|
|
|
@ -54,6 +54,11 @@ func (ts TimeStamp) AsTime() (tm time.Time) {
|
||||||
return ts.AsTimeInLocation(setting.DefaultUILocation)
|
return ts.AsTimeInLocation(setting.DefaultUILocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AsLocalTime convert timestamp as time.Time in local location
|
||||||
|
func (ts TimeStamp) AsLocalTime() time.Time {
|
||||||
|
return time.Unix(int64(ts), 0)
|
||||||
|
}
|
||||||
|
|
||||||
// AsTimeInLocation convert timestamp as time.Time in Local locale
|
// AsTimeInLocation convert timestamp as time.Time in Local locale
|
||||||
func (ts TimeStamp) AsTimeInLocation(loc *time.Location) (tm time.Time) {
|
func (ts TimeStamp) AsTimeInLocation(loc *time.Location) (tm time.Time) {
|
||||||
tm = time.Unix(int64(ts), 0).In(loc)
|
tm = time.Unix(int64(ts), 0).In(loc)
|
||||||
|
|
|
@ -99,7 +99,7 @@ func createPackageMetadataResponse(registryURL string, pds []*packages_model.Pac
|
||||||
Name: pd.Package.Name,
|
Name: pd.Package.Name,
|
||||||
Version: pd.Version.Version,
|
Version: pd.Version.Version,
|
||||||
Type: packageType,
|
Type: packageType,
|
||||||
Created: time.Unix(int64(pd.Version.CreatedUnix), 0),
|
Created: pd.Version.CreatedUnix.AsLocalTime(),
|
||||||
Metadata: pd.Metadata.(*composer_module.Metadata),
|
Metadata: pd.Metadata.(*composer_module.Metadata),
|
||||||
Dist: Dist{
|
Dist: Dist{
|
||||||
Type: "zip",
|
Type: "zip",
|
||||||
|
|
|
@ -184,7 +184,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackage creates a new package
|
// UploadPackage creates a new package
|
||||||
|
|
|
@ -475,7 +475,7 @@ func downloadFile(ctx *context.Context, fileFilter stringSet, fileKey string) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteRecipeV1 deletes the requested recipe(s)
|
// DeleteRecipeV1 deletes the requested recipe(s)
|
||||||
|
@ -723,7 +723,7 @@ func listRevisions(ctx *context.Context, revisions []*conan_model.PropertyValue)
|
||||||
|
|
||||||
revs := make([]*revisionInfo, 0, len(revisions))
|
revs := make([]*revisionInfo, 0, len(revisions))
|
||||||
for _, rev := range revisions {
|
for _, rev := range revisions {
|
||||||
revs = append(revs, &revisionInfo{Revision: rev.Value, Time: time.Unix(int64(rev.CreatedUnix), 0)})
|
revs = append(revs, &revisionInfo{Revision: rev.Value, Time: rev.CreatedUnix.AsLocalTime()})
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(ctx, http.StatusOK, &RevisionList{revs})
|
jsonResponse(ctx, http.StatusOK, &RevisionList{revs})
|
||||||
|
@ -743,7 +743,7 @@ func LatestRecipeRevision(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: time.Unix(int64(revision.CreatedUnix), 0)})
|
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: revision.CreatedUnix.AsLocalTime()})
|
||||||
}
|
}
|
||||||
|
|
||||||
// LatestPackageRevision gets the latest package revision
|
// LatestPackageRevision gets the latest package revision
|
||||||
|
@ -760,7 +760,7 @@ func LatestPackageRevision(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: time.Unix(int64(revision.CreatedUnix), 0)})
|
jsonResponse(ctx, http.StatusOK, &revisionInfo{Revision: revision.Value, Time: revision.CreatedUnix.AsLocalTime()})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListRecipeRevisionFiles gets a list of all recipe revision files
|
// ListRecipeRevisionFiles gets a list of all recipe revision files
|
||||||
|
|
|
@ -53,7 +53,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackage uploads the specific generic package.
|
// UploadPackage uploads the specific generic package.
|
||||||
|
|
|
@ -138,7 +138,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackage creates a new package
|
// UploadPackage creates a new package
|
||||||
|
|
|
@ -177,7 +177,7 @@ func servePackageFile(ctx *context.Context, params parameters) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
|
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
|
||||||
|
|
|
@ -103,7 +103,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackage creates a new package
|
// UploadPackage creates a new package
|
||||||
|
|
|
@ -176,7 +176,7 @@ func createRegistrationLeafResponse(l *linkBuilder, pd *packages_model.PackageDe
|
||||||
return &RegistrationLeafResponse{
|
return &RegistrationLeafResponse{
|
||||||
Type: []string{"Package", "http://schema.nuget.org/catalog#Permalink"},
|
Type: []string{"Package", "http://schema.nuget.org/catalog#Permalink"},
|
||||||
Listed: true,
|
Listed: true,
|
||||||
Published: time.Unix(int64(pd.Version.CreatedUnix), 0),
|
Published: pd.Version.CreatedUnix.AsLocalTime(),
|
||||||
RegistrationLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
|
RegistrationLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
|
||||||
PackageContentURL: l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version),
|
PackageContentURL: l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version),
|
||||||
RegistrationIndexURL: l.GetRegistrationIndexURL(pd.Package.Name),
|
RegistrationIndexURL: l.GetRegistrationIndexURL(pd.Package.Name),
|
||||||
|
|
|
@ -179,7 +179,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackage creates a new package with the metadata contained in the uploaded nupgk file
|
// UploadPackage creates a new package with the metadata contained in the uploaded nupgk file
|
||||||
|
@ -378,7 +378,7 @@ func DownloadSymbolFile(ctx *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s, _, err := packages_service.GetPackageFileStream(ctx, pfs[0])
|
s, pf, err := packages_service.GetPackageFileStream(ctx, pfs[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist {
|
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist {
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
apiError(ctx, http.StatusNotFound, err)
|
||||||
|
@ -389,7 +389,7 @@ func DownloadSymbolFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pfs[0].Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePackage hard deletes the package
|
// DeletePackage hard deletes the package
|
||||||
|
|
|
@ -69,7 +69,7 @@ func packageDescriptorToMetadata(baseURL string, pd *packages_model.PackageDescr
|
||||||
return &versionMetadata{
|
return &versionMetadata{
|
||||||
Version: pd.Version.Version,
|
Version: pd.Version.Version,
|
||||||
ArchiveURL: fmt.Sprintf("%s/files/%s.tar.gz", baseURL, url.PathEscape(pd.Version.Version)),
|
ArchiveURL: fmt.Sprintf("%s/files/%s.tar.gz", baseURL, url.PathEscape(pd.Version.Version)),
|
||||||
Published: time.Unix(int64(pd.Version.CreatedUnix), 0),
|
Published: pd.Version.CreatedUnix.AsLocalTime(),
|
||||||
Pubspec: pd.Metadata.(*pub_module.Metadata).Pubspec,
|
Pubspec: pd.Metadata.(*pub_module.Metadata).Pubspec,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,5 +271,5 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
|
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
|
||||||
|
|
|
@ -188,7 +188,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
|
// UploadPackageFile adds a file to the package. If the package does not exist, it gets created.
|
||||||
|
|
|
@ -468,7 +468,8 @@ func download(ctx *context.Context, archiveName string, archiver *repo_model.Rep
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer fr.Close()
|
defer fr.Close()
|
||||||
ctx.ServeStream(fr, downloadName)
|
|
||||||
|
ctx.ServeContent(downloadName, fr, archiver.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitiateDownload will enqueue an archival request, as needed. It may submit
|
// InitiateDownload will enqueue an archival request, as needed. It may submit
|
||||||
|
|
|
@ -393,5 +393,5 @@ func DownloadPackageFile(ctx *context.Context) {
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
ctx.ServeStream(s, pf.Name)
|
ctx.ServeContent(pf.Name, s, pf.CreatedUnix.AsLocalTime())
|
||||||
}
|
}
|
||||||
|
|
|
@ -402,7 +402,7 @@ func Cleanup(unused context.Context, olderThan time.Duration) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFileStreamByPackageNameAndVersion returns the content of the specific package file
|
// GetFileStreamByPackageNameAndVersion returns the content of the specific package file
|
||||||
func GetFileStreamByPackageNameAndVersion(ctx context.Context, pvi *PackageInfo, pfi *PackageFileInfo) (io.ReadCloser, *packages_model.PackageFile, error) {
|
func GetFileStreamByPackageNameAndVersion(ctx context.Context, pvi *PackageInfo, pfi *PackageFileInfo) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
|
||||||
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)
|
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)
|
||||||
|
|
||||||
pv, err := packages_model.GetVersionByNameAndVersion(ctx, pvi.Owner.ID, pvi.PackageType, pvi.Name, pvi.Version)
|
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFileStreamByPackageVersionAndFileID returns the content of the specific package file
|
// GetFileStreamByPackageVersionAndFileID returns the content of the specific package file
|
||||||
func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_model.User, versionID, fileID int64) (io.ReadCloser, *packages_model.PackageFile, error) {
|
func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_model.User, versionID, fileID int64) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
|
||||||
log.Trace("Getting package file stream: %v, %v, %v", owner.ID, versionID, fileID)
|
log.Trace("Getting package file stream: %v, %v, %v", owner.ID, versionID, fileID)
|
||||||
|
|
||||||
pv, err := packages_model.GetVersionByID(ctx, versionID)
|
pv, err := packages_model.GetVersionByID(ctx, versionID)
|
||||||
|
@ -449,7 +449,7 @@ func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_mod
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFileStreamByPackageVersion returns the content of the specific package file
|
// GetFileStreamByPackageVersion returns the content of the specific package file
|
||||||
func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.PackageVersion, pfi *PackageFileInfo) (io.ReadCloser, *packages_model.PackageFile, error) {
|
func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.PackageVersion, pfi *PackageFileInfo) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
|
||||||
pf, err := packages_model.GetFileForVersionByName(ctx, pv.ID, pfi.Filename, pfi.CompositeKey)
|
pf, err := packages_model.GetFileForVersionByName(ctx, pv.ID, pfi.Filename, pfi.CompositeKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
@ -459,7 +459,7 @@ func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.Packa
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPackageFileStream returns the content of the specific package file
|
// GetPackageFileStream returns the content of the specific package file
|
||||||
func GetPackageFileStream(ctx context.Context, pf *packages_model.PackageFile) (io.ReadCloser, *packages_model.PackageFile, error) {
|
func GetPackageFileStream(ctx context.Context, pf *packages_model.PackageFile) (io.ReadSeekCloser, *packages_model.PackageFile, error) {
|
||||||
pb, err := packages_model.GetBlobByID(ctx, pf.BlobID)
|
pb, err := packages_model.GetBlobByID(ctx, pf.BlobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
Loading…
Reference in a new issue