diff --git a/models/attachment.go b/models/attachment.go index d800a47109da7..639c37f5b9551 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -58,6 +58,16 @@ func (a *Attachment) IncreaseDownloadCount() error { return nil } +// FileSize is returning the file datasize +func (a *Attachment) FileSize() (string, error) { + stats, err := os.Stat(AttachmentLocalPath(a.UUID)) + if err != nil { + return "error", fmt.Errorf("AttachmentFileSize: %v", err) + } + result := float64(stats.Size()) / float64(1048576) + return fmt.Sprintf("%.1f", result) + " MB", nil +} + // AttachmentLocalPath returns where attachment is stored in local file // system based on given UUID. func AttachmentLocalPath(uuid string) string { @@ -104,7 +114,7 @@ func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) { attach := &Attachment{UUID: uuid} has, err := e.Get(attach) if err != nil { - return nil, err + return nil, nil } else if !has { return nil, ErrAttachmentNotExist{0, uuid} } @@ -126,6 +136,11 @@ func GetAttachmentByUUID(uuid string) (*Attachment, error) { return getAttachmentByUUID(x, uuid) } +// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName. +func GetAttachmentByReleaseIDFileName(releaseID int64, fileName string) (*Attachment, error) { + return getAttachmentByReleaseIDFileName(x, releaseID, fileName) +} + func getAttachmentsByIssueID(e Engine, issueID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) return attachments, e.Where("issue_id = ? AND comment_id = 0", issueID).Find(&attachments) @@ -142,6 +157,18 @@ func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { return attachments, x.Where("comment_id=?", commentID).Find(&attachments) } +// getAttachmentByReleaseIDFileName return a file based on the the following infos: +func getAttachmentByReleaseIDFileName(e Engine, releaseID int64, fileName string) (*Attachment, error) { + attach := &Attachment{ReleaseID: releaseID, Name: fileName} + has, err := e.Get(attach) + if err != nil { + return nil, err + } else if !has { + return nil, ErrAttachmentNotExist{0, attach.UUID} + } + return attach, nil +} + // DeleteAttachment deletes the given attachment and optionally the associated file. func DeleteAttachment(a *Attachment, remove bool) error { _, err := DeleteAttachments([]*Attachment{a}, remove) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 9b411648c61cd..9f9ebfb70c7bd 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -268,6 +268,37 @@ func Action(ctx *context.Context) { ctx.Redirect(redirectTo) } +// RedirectDownload return a file based on the following infos: +func RedirectDownload(ctx *context.Context) { + var ( + vTag = ctx.Params("vTag") + fileName = ctx.Params("fileName") + ) + tagNames := []string{vTag} + curRepo := ctx.Repo.Repository + releases, err := models.GetReleasesByRepoIDAndNames(curRepo.ID, tagNames) + if err != nil { + if models.IsErrAttachmentNotExist(err) { + ctx.Error(404) + return + } + ctx.Handle(500, "RedirectDownload: Failed to get attachment", err) + return + } + if len(releases) == 1 { + release := releases[0] + att, err := models.GetAttachmentByReleaseIDFileName(release.ID, fileName) + if err != nil { + ctx.Handle(404, "RedirectDownload -> Attachment not found", err) + return + } + if att != nil { + ctx.Redirect(setting.AppSubURL + "/attachments/" + att.UUID) + } + } + ctx.Handle(404, "RedirectDownload -> Attachment not found", err) +} + // Download download an archive of a repository func Download(ctx *context.Context) { var ( diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 5cb561b640f59..46dd71dd93f6f 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -487,6 +487,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/delete", repo.DeleteMilestone) }, reqRepoWriter, context.RepoRef()) m.Group("/releases", func() { + m.Get("/download/:vTag/:fileName", repo.RedirectDownload) m.Get("/new", repo.NewRelease) m.Post("/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost) m.Post("/delete", repo.DeleteRelease) diff --git a/templates/repo/release/list.tmpl b/templates/repo/release/list.tmpl index c18bc4884d29f..b703d25499940 100644 --- a/templates/repo/release/list.tmpl +++ b/templates/repo/release/list.tmpl @@ -53,21 +53,24 @@