Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/controllers
akuma06 b2b48f61b0 Torrent Generation on not found error (#1600)
* [WIP] Torrent Generation on not found error
As asked in #1517, it allows on-the-fly torrent generation. Since it uses magnet links, it needs some time to connect to peers. So it can't be instant generation, we need the user to wait and try after a minute at least.

* Replace Fatal by simple error

* attempt at fixing travis

* del

* Add Anacrolyx dependency

* Add back difflib

* Remove .torrent suffix in the url example

* Add some explanations when file missing page shown

* Ignore downloads directory

* Either use cache (third-party site) or own download directory

* Wrong import

* If there is an error then it means we aren't generating a torrent file

May it be "torrent not found" or "We do not store torrent files" which are the two only existing errors for this page

* hash is never empty

* TorrentLink may be empty at times

So we add a /download/:hash link if it is

* Update README.md

* Made a mistake here, need to check if false

* Update en-us.all.json

* Update CHANGELOG.md

* Torrent file generation can be triggered by click on button if JS enabled

* Update download.go

* Update download.go

* Use c.JSON instead of text/template

* Return to default behavior if we don't generate the file

* Don't do the query if returned to default behavior

* Add "Could not generate torrent file" error

* Fix JS condition & lower delay until button updates

* Start download automatically once torrent file is generated

* Fix torrentFileExists() constantly returning false if external torrent download URL

* torrent-view-data is two tables instead of one

This allows the removal of useless things without any problem (e.g Website link), but also a better responsibe design since the previous one separated stats after a certain res looking very wonky

* CSS changes to go along

* Remove useless <b></b>

* Update main.css

* In torrentFileExists, check if filestorage path exists instead of looking at the domain in torrent link

When checking if the file is stored on another server i used to simply check if the domain name was inside the torrent link, but we can straight up check for filestorage length

* Fix JS of on-demand stat fetching

* ScrapeAge variable accessible through view.jet.html

Contains last scraped time in hours, is at -1 is torrent has never been scraped
Stats will get updated if it's either at -1 or above 1460 (2 months old)

* Refresh stats if older than two months OR unknown and older than 24h

Show last scraped date even if stats are unknown

* Add StatsObsolete variable to torrent

Indicating if:
- They can be shown
- They need to be updated

* Update scraped data even if Unknown, prevent users from trying to fetch stats every seconds

* Torrent file stored locally by default

* no need to do all of that if no filestorage

* fix filestorage path

* Fix torrent download button stuck on "Generating torrent file" at rare times

* fix some css rules that didn't work on IE

* Fix panic error

Seems like this error is a known bug from  anacrolyx torrent https://github.com/anacrolix/torrent/issues/83

To prevent it, I'm creating a single client and modifying the socket.go to make it not raise a panic but a simple error log.
2017-10-21 09:40:43 +02:00
..
activities
api Fix Api documetation (#1685) 2017-10-18 22:27:55 +02:00
captcha
databasedumps
faq
feed Fix invalid comment date when JS is enabled, wrong url in an admin torrent list link, malformed pubDate for RSS feed (#1654) 2017-10-12 17:53:04 +02:00
middlewares Ads csp (#1509) 2017-09-06 11:18:37 +10:00
moderator Fix a condition that didn't behave as intended (#1651) 2017-10-12 02:54:01 +02:00
oauth
pprof
report
router
search Show refine constantly in no results page (#1675) 2017-10-16 19:52:36 +10:00
settings "No comments" message, avatar in comments, classic theme improvements, some other changes, Old Navigation (#1545) 2017-09-11 19:45:39 +02:00
static
themeToggle Make golang theme toggler behave exactly like JS one (#1648) 2017-10-11 17:31:54 +02:00
torrent Torrent Generation on not found error (#1600) 2017-10-21 09:40:43 +02:00
upload Fix filesize bug 2017-07-29 12:48:43 +02:00
user Update last-scraped date on view even if stats.go return unknown stats & other things (#1673) 2017-10-16 17:03:18 +10:00
README.md How-To Controller (#1386) 2017-08-22 11:02:22 +10:00
router.go Add toggle dark theme for non-JS users (#1381) 2017-08-21 10:48:17 +10:00

How-To Controller

Our routing system is based on Gin-Gonic HTTP web framework. For more information on how to use a gin.Context and what are the possibilities offered by Gin, you should take a look into their documentation. This readme will only explain to you how the routing works and how to easily render a static 'hello world!' template.

How the routing works

  1. Our routing system based on gin has one main file that connect all our controllers. This file is controllers/router.go here. You can see that in the first lines, that this file import multiple sub directories. All the import that start with an underscore is a controller package. In fact, the main router.go only imports the different controllers and do nothing else. So if you need to create a whole new controller, you would have to add it in this main router.go as an import that start with an underscore.
  2. Every controller package have another router.go file. It can be named differently but for homogenuity purposes, please name it like that. This file is the core of your controller package, you need it to add your controllers to the main router. All of the sub router.go files look pretty much the same. For example, here is the searchController package router.go file.
  • This file first import the controllers/router package. This step is important in order to have access to the function router.Get().
  • After it declares an init() function. This function is called automatically when the searchController package is imported.
  • In this function we call router.Get().XXX(). This part is where you link the different controllers to the routes. The XXX part are gin functions. In fact, router.Get() return a gin.Router so you can use all the functions from the gin.Router here.
  • Therefore router.Get().Any("/", SearchHandler) tells that every time you go to "/", it is the SearchHandler controller that should be called.
  1. Every controller package have other go files. Those files should be where you put your controllers functions. Try to not put every controller functions in the same file. A controller function is the same as those designed by the Gin documentation. They should be function that accept as only argument: c *gin.Context and return nothing. For example, you can see here the SearchHandler controller function.

Router Nyaa Functions

Our routing system doesn't have much function that you can use besides the gin defined one. However we have one that you might find very helpful, it is router.GetUser(c). This function declared in the controllers/router package returns the current User during the request. You can only use this function in a controller function which have access to the c *gin.Context.

But how to display something in a controller function?

Adding a controller function should be easy now for you, and you might have seen some gin examples to display thing and understood that it works. However, except if you want to render some JSON, please do not use the default gin rendering template for html. As you may have seen, we have our own templating system that is based on CloudyKit Jet Template. This template system is lighter and have better performance while having more functionnalities. To use this templating system, it is quite easy. First you need to import the templates package templates then you have multiple functions available to render some basic template. For example, templates.ModelList(c, "url/of/template/file.jet.html", ArrayOfModels, Navigation, SearchForm) is a function displaying a template file with a local variable Models which is your ArrayOfModels. You also have templates.Form(c, "url/of/template/file.jet.html", form) which is a function displaying a template file with a Form local variable that is equal to the form variable passed. Furthermore you have templates.Static(c, "url/of/template/file.jet.html") which is a function displaying a static template file without any local variables Finally, you can use a mix of templates.CommonVariables and templates.Render(c, "url/of/template/file.jet.html", variables) to render more specific templates which need special cases like here. When you have rendered and you don't want to continue executing the function after the display of the template, you have to return. For example, here, we want to stop the function and render the template "errors/no_results.jet.html", so we return just after calling templates.Render().

Mini Tutorial: Hello World!

To do this you will need a template file, a controller function and that's all!

The template file

This tutorial won't go deep in the templating system, for more information about it, go to the readme in /templates/. First create a new file named "hello.jet.html" in /templates/site/static/ with this in it:

<p>Hello <em>World</em>!</p>

Now let's move to the controller

The controller package

First let's create a new directory named "helloworld" in /controllers/. In this directory we will create two files, a router.go and a helloworld.go. In helloworld.go we will place in it the controller function HelloWorld(c *gin.Context) and render the static template:

package helloWorldController

import (
	"github.com/NyaaPantsu/nyaa/templates" // We import the templates package to use templates.* functions
	"github.com/gin-gonic/gin" // We import the gin package to use c *gin.Context
)

// HelloWorld : Controller for Hello World view page
func HelloWorld(c *gin.Context) {
  // We render the static template 
	templates.Static(c, "site/static/hello.jet.html")
  // We don't have to return since it's the end of the function
}

In router.go we will link the controller function to the route we want to use it:

package helloWorldController

import "github.com/NyaaPantsu/nyaa/controllers/router" // We import the router package to get the gin.Router and add the route
// When the package is called, we do what's inside
func init() {
  // we get the gin.Router an for Any method (Get/POST/PUT...) we associate /hello path url to the HelloWorld controller
	router.Get().Any("/hello", HelloWorld)
}

Now we need to tell to the Nyaa Programm that we have yet another controller package to add, for this you need to go to controllers/router.go and add the following line to the import section:

	_ "github.com/NyaaPantsu/nyaa/controllers/helloworld"           // helloworld controller

The End

Now you can save everything and compile/run and go to http://localhost:9999/hello to see your wonderfull controller in action!