TaskFlow/db/files.go

43 lines
939 B
Go
Raw Normal View History

2016-01-31 14:05:46 +08:00
package db
2016-02-01 22:34:19 +08:00
/*
stores the functions related to file IO
*/
2016-01-31 14:05:46 +08:00
import (
"log"
)
// AddFile is used to add the md5 of a file name which is uploaded to our application
// this will enable us to randomize the URL without worrying about the file names
func AddFile(fileName, token string) error {
SQL := database.prepare("insert into files values(?,?)")
tx := database.begin()
2016-01-31 14:05:46 +08:00
_, err = tx.Stmt(SQL).Exec(fileName, token)
if err != nil {
log.Println(err)
tx.Rollback()
} else {
log.Println(tx.Commit())
}
return err
}
// GetFileName is used to fetch the name according to the md5 checksum from the db
func GetFileName(token string) (string, error) {
sql := "select name from files where autoName=?"
var fileName string
rows := database.query(sql, fileName)
2016-01-31 14:05:46 +08:00
if rows.Next() {
err := rows.Scan(&fileName)
if err != nil {
log.Println(err)
return "", err
}
}
if err != nil {
return "", err
}
2016-01-31 14:05:46 +08:00
return fileName, nil
}