révision
d02672add8
14 fichiers modifiés avec 106 ajouts et 45 suppressions
|
@ -1,8 +1,14 @@
|
|||
package config
|
||||
|
||||
const (
|
||||
// TorrentFileStorage = "/var/tmp/torrent_outgoing"
|
||||
// TorrentFileStorage = "/var/www/wherever/you/want"
|
||||
// TorrentStorageLink = "https://your.site/somewhere/%s.torrent"
|
||||
TorrentFileStorage = ""
|
||||
TorrentStorageLink = ""
|
||||
|
||||
// TODO: deprecate this and move all files to the same server
|
||||
TorrentCacheLink = "http://anicache.com/torrent/%s.torrent"
|
||||
|
||||
//disable uploads by default
|
||||
UploadsDisabled = 1
|
||||
)
|
||||
|
|
|
@ -30,7 +30,7 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
|
|||
// db.SingularTable(true)
|
||||
if config.Environment == "DEVELOPMENT" {
|
||||
db.LogMode(true)
|
||||
db.AutoMigrate(&model.Torrents{}, &model.UsersFollowers{}, &model.User{}, &model.Comment{}, &model.OldComment{})
|
||||
db.AutoMigrate(&model.Torrents{}, &model.UserFollows{}, &model.User{}, &model.Comment{}, &model.OldComment{})
|
||||
// db.Model(&model.User{}).AddIndex("idx_user_token", "token")
|
||||
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/util"
|
||||
|
||||
"html"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -34,7 +34,7 @@ type Torrents struct {
|
|||
Description string `gorm:"column:description"`
|
||||
WebsiteLink string `gorm:"column:website_link"`
|
||||
|
||||
Uploader *User `gorm:"ForeignKey:uploader"`
|
||||
Uploader *User `gorm:"ForeignKey:UploaderId"`
|
||||
OldComments []OldComment `gorm:"ForeignKey:torrent_id"`
|
||||
Comments []Comment `gorm:"ForeignKey:torrent_id"`
|
||||
}
|
||||
|
@ -66,8 +66,10 @@ type TorrentsJson struct {
|
|||
Category string `json:"category"`
|
||||
Downloads int `json:"downloads"`
|
||||
UploaderId uint `json:"uploader_id"`
|
||||
UploaderName template.HTML `json:"uploader_name"`
|
||||
WebsiteLink template.URL `json:"website_link"`
|
||||
Magnet template.URL `json:"magnet"`
|
||||
TorrentLink template.URL `json:"torrent"`
|
||||
}
|
||||
|
||||
/* Model Conversion to Json */
|
||||
|
@ -79,12 +81,21 @@ func (t *Torrents) ToJson() TorrentsJson {
|
|||
commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date})
|
||||
}
|
||||
for _, c := range t.Comments {
|
||||
|
||||
commentsJson = append(commentsJson, CommentsJson{Username: c.User.Username, Content: util.MarkdownToHTML(c.Content), Date: c.CreatedAt})
|
||||
}
|
||||
uploader := ""
|
||||
if t.Uploader != nil {
|
||||
uploader = t.Uploader.Username
|
||||
}
|
||||
torrentlink := ""
|
||||
if t.Id <= config.LastOldTorrentId && len(config.TorrentCacheLink) > 0 {
|
||||
torrentlink = fmt.Sprintf(config.TorrentCacheLink, t.Hash)
|
||||
} else if t.Id > config.LastOldTorrentId && len(config.TorrentStorageLink) > 0 {
|
||||
torrentlink = fmt.Sprintf(config.TorrentStorageLink, t.Hash)
|
||||
}
|
||||
res := TorrentsJson{
|
||||
Id: strconv.FormatUint(uint64(t.Id), 10),
|
||||
Name: html.UnescapeString(t.Name),
|
||||
Name: t.Name,
|
||||
Status: t.Status,
|
||||
Hash: t.Hash,
|
||||
Date: t.Date.Format(time.RFC3339),
|
||||
|
@ -95,8 +106,10 @@ func (t *Torrents) ToJson() TorrentsJson {
|
|||
Category: strconv.Itoa(t.Category),
|
||||
Downloads: t.Downloads,
|
||||
UploaderId: t.UploaderId,
|
||||
UploaderName: util.SafeText(uploader),
|
||||
WebsiteLink: util.Safe(t.WebsiteLink),
|
||||
Magnet: util.Safe(magnet)}
|
||||
Magnet: util.Safe(magnet),
|
||||
TorrentLink: util.Safe(torrentlink)}
|
||||
|
||||
return res
|
||||
}
|
||||
|
|
|
@ -4,9 +4,6 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// omit is the bool type for omitting a field of struct.
|
||||
type omit bool
|
||||
|
||||
type User struct {
|
||||
Id uint `gorm:"column:user_id;primary_key"`
|
||||
Username string `gorm:"column:username"`
|
||||
|
@ -15,27 +12,26 @@ type User struct {
|
|||
Status int `gorm:"column:status"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
/*Api*/Token string `gorm:"column:api_token"`
|
||||
//ApiTokenExpiry
|
||||
|
||||
// Liking
|
||||
LikingCount int `json:"likingCount"`
|
||||
LikedCount int `json:"likedCount"`
|
||||
Likings []User `gorm:"foreignkey:userId;associationforeignkey:follower_id;many2many:users_followers;"`
|
||||
Liked []User `gorm:"foreignkey:follower_id;associationforeignkey:userId;many2many:users_followers;"`
|
||||
|
||||
Md5 string `json:"md5"`
|
||||
Token string `gorm:"column:api_token"`
|
||||
TokenExpiration time.Time `gorm:"column:api_token_expiry"`
|
||||
Language string `gorm:"column:language"`
|
||||
Torrents []Torrents `gorm:"ForeignKey:UploaderId"`
|
||||
|
||||
// TODO: move this to PublicUser
|
||||
LikingCount int `json:"likingCount" gorm:"-"`
|
||||
LikedCount int `json:"likedCount" gorm:"-"`
|
||||
Likings []User `gorm:"foreignkey:userId;associationforeignkey:follower_id;many2many:user_follows"`
|
||||
Liked []User `gorm:"foreignkey:follower_id;associationforeignkey:userId;many2many:user_follows"`
|
||||
|
||||
Md5 string `json:"md5"` // Used for gravatar
|
||||
Torrents []Torrents `gorm:"ForeignKey:UploaderId"`
|
||||
}
|
||||
|
||||
type PublicUser struct {
|
||||
User *User
|
||||
}
|
||||
|
||||
// UsersFollowers is a relation table to relate users each other.
|
||||
type UsersFollowers struct {
|
||||
UserID uint `gorm:"column:userId"`
|
||||
FollowerID uint `gorm:"column:follower_id"`
|
||||
// different users following eachother
|
||||
type UserFollows struct {
|
||||
UserID uint `gorm:"column:user_id"`
|
||||
FollowerID uint `gorm:"column:following"`
|
||||
}
|
||||
|
|
|
@ -58,7 +58,10 @@ func GetTorrentById(id string) (model.Torrents, error) {
|
|||
if tmp.Find(&torrent).RecordNotFound() {
|
||||
return torrent, errors.New("Article is not found.")
|
||||
}
|
||||
// .Preload("Comments.User") doesn't work
|
||||
// GORM relly likes not doing its job correctly
|
||||
// (or maybe I'm just retarded)
|
||||
torrent.Uploader = new(model.User)
|
||||
db.ORM.Where("user_id = ?", torrent.UploaderId).Find(torrent.Uploader)
|
||||
for i := range torrent.Comments {
|
||||
torrent.Comments[i].User = new(model.User)
|
||||
db.ORM.Where("user_id = ?", torrent.Comments[i].UserId).Find(torrent.Comments[i].User)
|
||||
|
|
|
@ -118,6 +118,9 @@ func SetCookieHandler(w http.ResponseWriter, email string, pass string) (int, er
|
|||
// RegisterHanderFromForm sets cookie from a RegistrationForm.
|
||||
func RegisterHanderFromForm(w http.ResponseWriter, registrationForm formStruct.RegistrationForm) (int, error) {
|
||||
email := registrationForm.Email
|
||||
if email == "" {
|
||||
email = registrationForm.Username
|
||||
}
|
||||
pass := registrationForm.Password
|
||||
log.Debugf("RegisterHandler UserEmail : %s", email)
|
||||
log.Debugf("RegisterHandler UserPassword : %s", pass)
|
||||
|
|
|
@ -47,14 +47,14 @@ func SuggestUsername(username string) string {
|
|||
|
||||
func CheckEmail(email string) bool {
|
||||
if len(email) == 0 {
|
||||
return true
|
||||
return false
|
||||
}
|
||||
var count int
|
||||
db.ORM.Model(model.User{}).Where("email = ?", email).Count(&count)
|
||||
if count == 0 {
|
||||
return false // duplicate
|
||||
if count != 0 {
|
||||
return true // error: duplicate
|
||||
}
|
||||
return true
|
||||
return false
|
||||
}
|
||||
|
||||
// CreateUserFromForm creates a user from a registration form.
|
||||
|
@ -62,17 +62,25 @@ func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.Use
|
|||
var user model.User
|
||||
log.Debugf("registrationForm %+v\n", registrationForm)
|
||||
modelHelper.AssignValue(&user, ®istrationForm)
|
||||
user.Md5 = crypto.GenerateMD5Hash(user.Email) // Gravatar
|
||||
|
||||
if user.Email == "" {
|
||||
user.Md5 = ""
|
||||
} else {
|
||||
user.Md5 = crypto.GenerateMD5Hash(user.Email)
|
||||
}
|
||||
token, err := crypto.GenerateRandomToken32()
|
||||
if err != nil {
|
||||
return user, errors.New("Token not generated.")
|
||||
}
|
||||
|
||||
user.Token = token
|
||||
user.TokenExpiration = timeHelper.FewDaysLater(config.AuthTokenExpirationDay)
|
||||
log.Debugf("user %+v\n", user)
|
||||
if db.ORM.Create(&user).Error != nil {
|
||||
return user, errors.New("User is not created.")
|
||||
}
|
||||
|
||||
user.CreatedAt = time.Now()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
|
@ -137,7 +145,12 @@ func RetrieveUsers() []*model.PublicUser {
|
|||
|
||||
// UpdateUserCore updates a user. (Applying the modifed data of user).
|
||||
func UpdateUserCore(user *model.User) (int, error) {
|
||||
user.Md5 = crypto.GenerateMD5Hash(user.Email)
|
||||
if user.Email == "" {
|
||||
user.Md5 = ""
|
||||
} else {
|
||||
user.Md5 = crypto.GenerateMD5Hash(user.Email)
|
||||
}
|
||||
|
||||
token, err := crypto.GenerateRandomToken32()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, errors.New("Token not generated.")
|
||||
|
@ -147,6 +160,7 @@ func UpdateUserCore(user *model.User) (int, error) {
|
|||
if db.ORM.Save(user).Error != nil {
|
||||
return http.StatusInternalServerError, errors.New("User is not updated.")
|
||||
}
|
||||
|
||||
user.UpdatedAt = time.Now()
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
|
|
@ -33,9 +33,11 @@
|
|||
<a href="{{.Magnet}}" title="Magnet link">
|
||||
<span class="glyphicon glyphicon-magnet" aria-hidden="true"></span>
|
||||
</a>
|
||||
<a href="http://anicache.com/torrent/{{.Hash}}.torrent" title="Torrent file">
|
||||
{{if ne .TorrentLink ""}}
|
||||
<a href="{{.TorrentLink}}" title="Torrent file">
|
||||
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span>
|
||||
</a>
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
|
|
|
@ -39,9 +39,11 @@
|
|||
<a href="{{.Magnet}}" title="Magnet link">
|
||||
<span class="glyphicon glyphicon-magnet" aria-hidden="true"></span>
|
||||
</a>
|
||||
<a href="http://anicache.com/torrent/{{.Hash}}.torrent" title="Torrent file">
|
||||
{{if ne .TorrentLink ""}}
|
||||
<a href="{{.TorrentLink}}" title="Torrent file">
|
||||
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span>
|
||||
</a>
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
|
||||
<div class="form-group">
|
||||
<label for="desc">{{T "torrent_description"}}</label>
|
||||
<p class="help-block">{{T "limited_html_set_is_allowed_use"}} <span style="font-family:monospace"><br/></span>.</p>
|
||||
<p class="help-block">{{T "description_markdown_notice"}}</p>
|
||||
<textarea name="desc" class="form-control" rows="10">{{.Description}}</textarea>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{{define "title"}}{{ T "register_success_title" }}{{end}}
|
||||
{{define "title"}}{{ T "register_success_title" }}{{end}}
|
||||
{{define "contclass"}}cont-view{{end}}
|
||||
{{define "content"}}
|
||||
<div class="blockBody">
|
||||
|
@ -7,9 +7,13 @@
|
|||
|
||||
<h2>{{T "sign_up_success"}}</h2>
|
||||
<hr class="colorgraph">
|
||||
{{if ne .User.Email ""}}
|
||||
<p>{{ T "signup_verification_email" }}</p>
|
||||
{{else}}
|
||||
<p>{{ T "signup_verification_noemail" }}</p>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{define "js_footer"}}<script type="text/javascript" charset="utf-8" src="{{.URL.Parse "/js/registerPage.js"}}"></script>{{end}}
|
||||
{{define "js_footer"}}<script type="text/javascript" charset="utf-8" src="{{.URL.Parse "/js/registerPage.js"}}"></script>{{end}}
|
||||
|
|
|
@ -26,10 +26,15 @@
|
|||
<td>{{T "size"}}</td>
|
||||
<td>{{.Filesize}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Uploader</td>
|
||||
<td><a href="{{$.URL.Parse (printf "/user/%d/-" .UploaderId) }}">{{.UploaderName}}</a></td>
|
||||
{{if ne .WebsiteLink ""}}
|
||||
<tr>
|
||||
<td>{{T "Link"}}</td>
|
||||
<td><a href="{{.WebsiteLink}}">{{.WebsiteLink}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
<tr>
|
||||
<td>{{T "links"}}</td>
|
||||
<td>
|
||||
|
@ -37,9 +42,11 @@
|
|||
<span class="glyphicon glyphicon-magnet" aria-hidden="true"></span> Download!
|
||||
</a>
|
||||
<a style="padding-left: 0.5em"></a>
|
||||
<a aria-label="Torrent file" href="http://anicache.com/torrent/{{.Hash}}.torrent" type="button" class="btn btn-success download-btn">
|
||||
{{if ne .TorrentLink ""}}
|
||||
<a aria-label="Torrent file" href="{{.TorrentLink}}" type="button" class="btn btn-success download-btn">
|
||||
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span> Torrent file
|
||||
</a>
|
||||
{{end}}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
|
|
@ -101,7 +101,11 @@
|
|||
},
|
||||
{
|
||||
"id":"signup_verification_email",
|
||||
"translation": "Now, as the final step of registration please check your mail inbox (or spam) and click the link provided for activating your account!"
|
||||
"translation": "Finally, please check your mail inbox (and spam folder!) for the verification email."
|
||||
},
|
||||
{
|
||||
"id":"signup_verification_noemail",
|
||||
"translation": "Registration was successful, you may now use your account."
|
||||
},
|
||||
{
|
||||
"id":"settings",
|
||||
|
@ -197,7 +201,7 @@
|
|||
},
|
||||
{
|
||||
"id": "sign_in",
|
||||
"translation": "Sign in"
|
||||
"translation": "Sign In"
|
||||
},
|
||||
{
|
||||
"id": "sign_up",
|
||||
|
@ -328,7 +332,7 @@
|
|||
"translation": "Torrent file"
|
||||
},
|
||||
{
|
||||
"id": "uploading_torrent_prefills_fields",
|
||||
"id": "uploading_file_prefills_fields",
|
||||
"translation": "Uploading a torrent file allows pre-filling some fields, this is recommended."
|
||||
},
|
||||
{
|
||||
|
@ -436,8 +440,8 @@
|
|||
"translation": "Torrent Description"
|
||||
},
|
||||
{
|
||||
"id": "limited_html_set_is_allowed_use",
|
||||
"translation": "A limited set of HTML is allowed in the description, make sure to use"
|
||||
"id": "description_markdown_notice",
|
||||
"translation": "Markdown can be used in descriptions."
|
||||
},
|
||||
{
|
||||
"id": "show_all",
|
||||
|
|
11
util/safe.go
11
util/safe.go
|
@ -1,7 +1,14 @@
|
|||
package util
|
||||
|
||||
import "html/template"
|
||||
import (
|
||||
"html"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
func Safe(s string) template.URL {
|
||||
return template.URL(s)
|
||||
}
|
||||
}
|
||||
|
||||
func SafeText(s string) template.HTML {
|
||||
return template.HTML(html.EscapeString(s))
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket