-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Add file upload and download endpoints
- Loading branch information
1 parent
f1ce06a
commit 9062e48
Showing
8 changed files
with
715 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package model | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/SohelAhmedJoni/Awazz-Backend/internal/durable" | ||
) | ||
|
||
func (s *File) Save() error { | ||
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
str := `CREATE TABLE IF NOT EXISTS FILE( | ||
ULID VARCHAR(128) PRIMARY KEY, | ||
Name TEXT NOT NULL, | ||
CreatedAt INTEGER NOT NULL, | ||
UpdatedAt INTEGER, | ||
Hash TEXT NOT NULL, | ||
HashType TEXT NOT NULL, | ||
MimeType TEXT NOT NULL, | ||
Ext TEXT NOT NULL | ||
)` | ||
_, err = db.Exec(str) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
statement, err := db.Prepare("INSERT INTO FILE (ULID,Name,CreatedAt,UpdatedAt,Hash,HashType,MimeType,Ext) VALUES (?,?,?,?,?,?,?,?)") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = statement.Exec(s.Uuid, s.Name, s.CreatedAt, s.UpdatedAt, s.Hash, s.HashType, s.MimeType, s.Ext) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return nil | ||
} | ||
|
||
func (g *File) Get(ULID string) error { | ||
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
|
||
rows, err := db.Query("SELECT * from FILE where ULID = ?", ULID) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
err = rows.Scan(&g.Uuid, &g.Name, &g.CreatedAt, &g.UpdatedAt, &g.Hash, &g.HashType, &g.MimeType, &g.Ext) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
err = rows.Err() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *File) Delete(ULID string) error { | ||
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
|
||
_, err = db.Exec("DELETE FROM FILE WHERE ULID= ?", ULID) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
return nil | ||
} | ||
|
||
func (u *FileList) Get(ULIDS []string) error { | ||
db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
|
||
for _, ULID := range ULIDS { | ||
var g File | ||
rows, err := db.Query(fmt.Sprintf("SELECT * from FILE where ULID = %v", ULID)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer rows.Close() | ||
rows.Next() | ||
err = rows.Scan(g.Uuid, g.Name, g.CreatedAt, g.UpdatedAt, g.Hash, g.HashType, g.MimeType, g.Ext) | ||
if err != nil { | ||
panic(err) | ||
} | ||
u.Files = append(u.Files, &g) | ||
err = rows.Err() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.