2018-05-27 09:36:35 +00:00
package panel
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/common"
2018-05-27 09:36:35 +00:00
)
func Backups ( w http . ResponseWriter , r * http . Request , user common . User , backupURL string ) common . RouteError {
2018-06-17 07:28:18 +00:00
basePage , ferr := buildBasePage ( w , r , & user , "backups" , "backups" )
2018-05-27 09:36:35 +00:00
if ferr != nil {
return ferr
}
if backupURL != "" {
// We don't want them trying to break out of this directory, it shouldn't hurt since it's a super admin, but it's always good to practice good security hygiene, especially if this is one of many instances on a managed server not controlled by the superadmin/s
backupURL = common . Stripslashes ( backupURL )
var ext = filepath . Ext ( "./backups/" + backupURL )
2019-04-06 01:08:49 +00:00
if ext != ".sql" && ext != ".zip" {
return common . NotFound ( w , r , basePage . Header )
}
info , err := os . Stat ( "./backups/" + backupURL )
if err != nil {
return common . NotFound ( w , r , basePage . Header )
}
w . Header ( ) . Set ( "Content-Length" , strconv . FormatInt ( info . Size ( ) , 10 ) )
2018-05-27 09:36:35 +00:00
if ext == ".sql" {
2019-04-06 01:08:49 +00:00
// TODO: Change the served filename to gosora_backup_%timestamp%.sql, the time the file was generated, not when it was modified aka what the name of it should be
2018-05-27 09:36:35 +00:00
w . Header ( ) . Set ( "Content-Disposition" , "attachment; filename=gosora_backup.sql" )
2018-08-22 06:37:36 +00:00
w . Header ( ) . Set ( "Content-Type" , "application/sql" )
2019-04-06 01:08:49 +00:00
} else {
// TODO: Change the served filename to gosora_backup_%timestamp%.zip, the time the file was generated, not when it was modified aka what the name of it should be
w . Header ( ) . Set ( "Content-Disposition" , "attachment; filename=gosora_backup.zip" )
w . Header ( ) . Set ( "Content-Type" , "application/zip" )
2018-05-27 09:36:35 +00:00
}
2019-04-06 01:08:49 +00:00
// TODO: Fix the problem where non-existent files aren't greeted with custom 404s on ServeFile()'s side
http . ServeFile ( w , r , "./backups/" + backupURL )
return nil
2018-05-27 09:36:35 +00:00
}
var backupList [ ] common . BackupItem
backupFiles , err := ioutil . ReadDir ( "./backups" )
if err != nil {
return common . InternalError ( err , w , r )
}
for _ , backupFile := range backupFiles {
var ext = filepath . Ext ( backupFile . Name ( ) )
if ext != ".sql" {
continue
}
backupList = append ( backupList , common . BackupItem { backupFile . Name ( ) , backupFile . ModTime ( ) } )
}
2018-06-17 07:28:18 +00:00
pi := common . PanelBackupPage { basePage , backupList }
2019-02-10 05:52:26 +00:00
return renderTemplate ( "panel_backups" , w , r , basePage . Header , & pi )
2018-05-27 09:36:35 +00:00
}