storage.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. package storage
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "log"
  8. "mime/multipart"
  9. "os"
  10. "path"
  11. "strings"
  12. "time"
  13. "git.aetherial.dev/aeth/keiji/pkg/env"
  14. "github.com/google/uuid"
  15. )
  16. const TECHNICAL = "technical"
  17. const CONFIGURATION = "configuration"
  18. const BLOG = "blog"
  19. const CREATIVE = "creative"
  20. const DIGITAL_ART = "digital_art"
  21. const HOMEPAGE = "homepage"
  22. var Topics = []string{
  23. TECHNICAL,
  24. BLOG,
  25. CREATIVE,
  26. HOMEPAGE,
  27. }
  28. type DatabaseSchema struct {
  29. // Gotta figure out what this looks like
  30. // so that the ExtractAll() function gets
  31. // all of the data from the database
  32. }
  33. type MenuElement struct {
  34. Png string `json:"png"`
  35. Category string `json:"category"`
  36. MenuLinks []LinkPair `json:"menu_links"`
  37. }
  38. type AdminPage struct {
  39. Tables map[string][]TableData `json:"tables"`
  40. }
  41. type TableData struct { // TODO: add this to the database io interface
  42. DisplayName string `json:"display_name"`
  43. Link string `json:"link"`
  44. }
  45. type LinkPair struct {
  46. Link string `json:"link"`
  47. Text string `json:"text"`
  48. }
  49. type NavBarItem struct {
  50. Png []byte `json:"png"`
  51. File *multipart.FileHeader `form:"file"`
  52. Link string `json:"link" form:"link"`
  53. Redirect string `json:"redirect" form:"redirect"`
  54. }
  55. type Asset struct {
  56. Name string
  57. Data []byte
  58. }
  59. type Identifier string
  60. type Document struct {
  61. Row int
  62. Ident Identifier `json:"id"`
  63. Title string `json:"title"`
  64. Created string `json:"created"`
  65. Body string `json:"body"`
  66. Category string `json:"category"`
  67. Sample string `json:"sample"`
  68. }
  69. /*
  70. Truncates a text post into a 256 character long 'sample' for displaying posts
  71. */
  72. func (d *Document) MakeSample() string {
  73. t := strings.Split(d.Body, "")
  74. var sample []string
  75. if len(d.Body) < 256 {
  76. return d.Body
  77. }
  78. for i := 0; i < 256; i++ {
  79. sample = append(sample, t[i])
  80. }
  81. sample = append(sample, " ...")
  82. return strings.Join(sample, "")
  83. }
  84. type Image struct {
  85. Ident Identifier `json:"identifier"`
  86. Location string
  87. Title string `json:"title" form:"title"`
  88. File *multipart.FileHeader `form:"file"`
  89. Desc string `json:"description" form:"description"`
  90. Created string
  91. Category string
  92. Data []byte
  93. }
  94. type DocumentIO interface {
  95. GetDocument(id Identifier) (Document, error)
  96. GetImage(id Identifier) (Image, error)
  97. GetAllImages() []Image
  98. UpdateDocument(doc Document) error
  99. DeleteDocument(id Identifier) error
  100. DeleteNavbarItem(id Identifier) error
  101. AddDocument(doc Document) (Identifier, error)
  102. AddImage(data []byte, title, desc string) (Identifier, error)
  103. AddAsset(name string, data []byte) error
  104. AddAdminTableEntry(TableData, string) error
  105. AddNavbarItem(NavBarItem) error
  106. AddMenuItem(LinkPair) error
  107. GetByCategory(category string) []Document
  108. AllDocuments() []Document
  109. GetDropdownElements() []LinkPair
  110. GetNavBarLinks() []NavBarItem
  111. GetAssets() []Asset
  112. GetAdminTables() AdminPage
  113. }
  114. var (
  115. ErrDuplicate = errors.New("record already exists")
  116. ErrNotExists = errors.New("row not exists")
  117. ErrUpdateFailed = errors.New("update failed")
  118. ErrDeleteFailed = errors.New("delete failed")
  119. )
  120. type SQLiteRepo struct {
  121. db *sql.DB
  122. imageIO ImageIO
  123. }
  124. type ImageIO interface {
  125. Put([]byte, Identifier) error
  126. Get(Identifier) ([]byte, error)
  127. }
  128. type FilesystemImageIO struct {
  129. RootDir string
  130. }
  131. /*
  132. Put a data blob on the filesystem
  133. :param b: the
  134. */
  135. func (f FilesystemImageIO) Put(b []byte, id Identifier) error {
  136. fh, err := os.OpenFile(path.Join(f.RootDir, string(id)), os.O_CREATE|os.O_RDWR, os.ModePerm)
  137. if err != nil {
  138. return err
  139. }
  140. defer fh.Close()
  141. _, err = fh.Write(b)
  142. if err != nil {
  143. return err
  144. }
  145. return nil
  146. }
  147. /*
  148. Get a data blob from the filesystem
  149. :param id: the identifier of the image to retrieve
  150. */
  151. func (f FilesystemImageIO) Get(id Identifier) ([]byte, error) {
  152. fh, err := os.Open(path.Join(f.RootDir, string(id)))
  153. if err != nil {
  154. return nil, err
  155. }
  156. b, err := io.ReadAll(fh)
  157. if err != nil {
  158. return nil, err
  159. }
  160. return b, nil
  161. }
  162. // Instantiate a new SQLiteRepo struct
  163. func NewSQLiteRepo(db *sql.DB, imgIo ImageIO) *SQLiteRepo {
  164. return &SQLiteRepo{
  165. db: db,
  166. imageIO: imgIo,
  167. }
  168. }
  169. // Creates a new SQL table for text posts
  170. func (r *SQLiteRepo) Migrate(seedQueries []string) error {
  171. for i := range seedQueries {
  172. _, err := r.db.Exec(seedQueries[i])
  173. if err != nil {
  174. return err
  175. }
  176. }
  177. return nil
  178. }
  179. /*
  180. Get all dropdown menu elements. Returns a list of LinkPair structs with the text and redirect location
  181. */
  182. func (s *SQLiteRepo) GetDropdownElements() []LinkPair {
  183. rows, err := s.db.Query("SELECT * FROM menu")
  184. var menuItems []LinkPair
  185. defer rows.Close()
  186. for rows.Next() {
  187. var id int
  188. var item LinkPair
  189. err = rows.Scan(&id, &item.Link, &item.Text)
  190. if err != nil {
  191. log.Fatal(err)
  192. }
  193. menuItems = append(menuItems, item)
  194. }
  195. return menuItems
  196. }
  197. /*
  198. Retrieve a dropdown element by its text name on the UI
  199. */
  200. func (s *SQLiteRepo) GetDropdownElementByName(text string) (LinkPair, bool) {
  201. rows := s.db.QueryRow("SELECT * FROM menu WHERE text = ?", text)
  202. var item LinkPair
  203. var id int
  204. if err := rows.Scan(&id, &item.Link, &item.Text); err != nil {
  205. if errors.Is(err, sql.ErrNoRows) {
  206. return item, false
  207. }
  208. log.Fatal(err)
  209. }
  210. return item, true
  211. }
  212. /*
  213. Get all nav bar items. Returns a list of NavBarItem structs with the png data, the file name, and the redirect location of the icon
  214. /*
  215. Get all nav bar items. Returns a list of NavBarItem structs with the png data, the file name, and the redirect location of the icon
  216. */
  217. func (s *SQLiteRepo) GetNavBarLinks() []NavBarItem {
  218. rows, err := s.db.Query("SELECT * FROM navbar")
  219. var navbarItems []NavBarItem
  220. defer rows.Close()
  221. for rows.Next() {
  222. var item NavBarItem
  223. var id int
  224. err = rows.Scan(&id, &item.Png, &item.Link, &item.Redirect)
  225. if err != nil {
  226. log.Fatal(err)
  227. }
  228. navbarItems = append(navbarItems, item)
  229. }
  230. return navbarItems
  231. }
  232. /*
  233. get all assets from the asset table
  234. */
  235. func (s *SQLiteRepo) GetAssets() []Asset {
  236. rows, err := s.db.Query("SELECT * FROM assets")
  237. var assets []Asset
  238. defer rows.Close()
  239. for rows.Next() {
  240. var item Asset
  241. var id int
  242. err = rows.Scan(&id, &item.Name, &item.Data)
  243. if err != nil {
  244. log.Fatal(err)
  245. }
  246. assets = append(assets, item)
  247. }
  248. return assets
  249. }
  250. /*
  251. get all assets from the asset table
  252. */
  253. func (s *SQLiteRepo) GetAdminTables() AdminPage {
  254. rows, err := s.db.Query("SELECT * FROM admin")
  255. adminPage := AdminPage{Tables: map[string][]TableData{}}
  256. defer rows.Close()
  257. for rows.Next() {
  258. var item TableData
  259. var id int
  260. var category string
  261. err = rows.Scan(&id, &item.DisplayName, &item.Link, &category)
  262. if err != nil {
  263. log.Fatal(err)
  264. }
  265. adminPage.Tables[category] = append(adminPage.Tables[category], item)
  266. }
  267. return adminPage
  268. }
  269. /*
  270. Retrieve a document from the sqlite db
  271. :param id: the Identifier of the post
  272. */
  273. func (s *SQLiteRepo) GetDocument(id Identifier) (Document, error) {
  274. row := s.db.QueryRow("SELECT * FROM posts WHERE id = ?", id)
  275. var post Document
  276. var rowNum int
  277. if err := row.Scan(&rowNum, &post.Ident, &post.Title, &post.Created, &post.Body, &post.Category, &post.Sample); err != nil {
  278. if errors.Is(err, sql.ErrNoRows) {
  279. return post, ErrNotExists
  280. }
  281. return post, err
  282. }
  283. return post, nil
  284. }
  285. /*
  286. Get all documents by category
  287. :param category: the category to retrieve all docs from
  288. */
  289. func (s *SQLiteRepo) GetByCategory(category string) []Document {
  290. rows, err := s.db.Query("SELECT * FROM posts WHERE category = ?", category)
  291. if err != nil {
  292. log.Fatal(err)
  293. }
  294. var docs []Document
  295. defer rows.Close()
  296. for rows.Next() {
  297. var doc Document
  298. err := rows.Scan(&doc.Row, &doc.Ident, &doc.Title, &doc.Created, &doc.Body, &doc.Category, &doc.Sample)
  299. if err != nil {
  300. log.Fatal(err)
  301. }
  302. docs = append(docs, doc)
  303. }
  304. err = rows.Err()
  305. if err != nil {
  306. log.Fatal(err)
  307. }
  308. return docs
  309. }
  310. /*
  311. get image data from the images table
  312. :param id: the serial identifier of the post
  313. */
  314. func (s *SQLiteRepo) GetImage(id Identifier) (Image, error) {
  315. row := s.db.QueryRow("SELECT * FROM images WHERE id = ?", id)
  316. var rowNum int
  317. var title, desc, created string
  318. if err := row.Scan(&rowNum, &id, &title, &desc, &created); err != nil {
  319. if errors.Is(err, sql.ErrNoRows) {
  320. return Image{}, ErrNotExists
  321. }
  322. return Image{}, err
  323. }
  324. data, err := s.imageIO.Get(id)
  325. if err != nil {
  326. return Image{}, err
  327. }
  328. return Image{Ident: id, Title: title, Desc: desc, Data: data, Created: created}, nil
  329. }
  330. /*
  331. Get all of the images from the datastore
  332. */
  333. func (s *SQLiteRepo) GetAllImages() []Image {
  334. rows, err := s.db.Query("SELECT * FROM images")
  335. if err != nil {
  336. log.Fatal(err)
  337. }
  338. imgs := []Image{}
  339. for rows.Next() {
  340. var img Image
  341. var rowNum int
  342. err := rows.Scan(&rowNum, &img.Ident, &img.Title, &img.Desc, &img.Created)
  343. if err != nil {
  344. log.Fatal(err)
  345. }
  346. b, err := s.imageIO.Get(img.Ident)
  347. if err != nil {
  348. log.Fatal(err)
  349. }
  350. imgs = append(imgs, Image{Ident: img.Ident, Title: img.Title, Desc: img.Desc, Data: b, Created: img.Created})
  351. }
  352. err = rows.Err()
  353. if err != nil {
  354. log.Fatal(err)
  355. }
  356. return imgs
  357. }
  358. /*
  359. Add an image to the database
  360. :param title: the title of the image
  361. :param location: the location to save the image to
  362. :param desc: the description of the image, if any
  363. :param data: the binary data for the image
  364. */
  365. func (s *SQLiteRepo) AddImage(data []byte, title string, desc string) (Identifier, error) {
  366. id := newIdentifier()
  367. err := s.imageIO.Put(data, id)
  368. if err != nil {
  369. return Identifier(""), err
  370. }
  371. _, err = s.db.Exec("INSERT INTO images (id, title, desc, created) VALUES (?,?,?,?)", string(id), title, desc, time.Now().String())
  372. if err != nil {
  373. return Identifier(""), err
  374. }
  375. return id, nil
  376. }
  377. /*
  378. Updates a document in the database with the supplied. Only changes the title, the body, category. Keys off of the documents Identifier
  379. :param doc: the Document to upload into the database
  380. */
  381. func (s *SQLiteRepo) UpdateDocument(doc Document) error {
  382. tx, err := s.db.Begin()
  383. if err != nil {
  384. return err
  385. }
  386. stmt, err := tx.Prepare("UPDATE posts SET title = ?, body = ?, category = ?, sample = ? WHERE id = ?;")
  387. if err != nil {
  388. tx.Rollback()
  389. return err
  390. }
  391. res, err := stmt.Exec(doc.Title, doc.Body, doc.Category, doc.MakeSample(), doc.Ident)
  392. if err != nil {
  393. tx.Rollback()
  394. return err
  395. }
  396. affected, _ := res.RowsAffected()
  397. if affected != 1 {
  398. return ErrNotExists
  399. }
  400. tx.Commit()
  401. return nil
  402. }
  403. /*
  404. Adds a LinkPair to the menu database table
  405. :param item: the LinkPair to upload
  406. */
  407. func (s *SQLiteRepo) AddMenuItem(item LinkPair) error {
  408. tx, err := s.db.Begin()
  409. if err != nil {
  410. return err
  411. }
  412. _, found := s.GetDropdownElementByName(item.Text)
  413. if found {
  414. tx.Rollback()
  415. return errors.New("Row exists.")
  416. }
  417. stmt, _ := tx.Prepare("INSERT INTO menu(link, text) VALUES (?,?)")
  418. _, err = stmt.Exec(item.Link, item.Text)
  419. if err != nil {
  420. tx.Rollback()
  421. return err
  422. }
  423. tx.Commit()
  424. return nil
  425. }
  426. /*
  427. Adds an item to the navbar database table
  428. :param item: the NavBarItem to upload
  429. */
  430. func (s *SQLiteRepo) AddNavbarItem(item NavBarItem) error {
  431. tx, err := s.db.Begin()
  432. if err != nil {
  433. return err
  434. }
  435. stmt, err := tx.Prepare("INSERT INTO navbar(png, link, redirect) VALUES (?,?,?)")
  436. if err != nil {
  437. tx.Rollback()
  438. return err
  439. }
  440. _, err = stmt.Exec(item.Png, item.Link, item.Redirect)
  441. if err != nil {
  442. tx.Rollback()
  443. return err
  444. }
  445. tx.Commit()
  446. return nil
  447. }
  448. /*
  449. Adds an asset to the asset database table asset
  450. :param name: the name of the asset (filename)
  451. :param data: the byte array of the PNG to upload TODO: limit this to 256kb
  452. */
  453. func (s *SQLiteRepo) AddAsset(name string, data []byte) error {
  454. tx, err := s.db.Begin()
  455. if err != nil {
  456. return err
  457. }
  458. stmt, _ := tx.Prepare("INSERT INTO assets(name, data) VALUES (?,?)")
  459. _, err = stmt.Exec(name, data)
  460. if err != nil {
  461. tx.Rollback()
  462. return err
  463. }
  464. tx.Commit()
  465. return nil
  466. }
  467. /*
  468. Adds a document to the database (for text posts)
  469. :param doc: the Document to add
  470. */
  471. func (s *SQLiteRepo) AddDocument(doc Document) (Identifier, error) {
  472. id := newIdentifier()
  473. tx, err := s.db.Begin()
  474. if err != nil {
  475. return Identifier(""), err
  476. }
  477. stmt, _ := tx.Prepare("INSERT INTO posts(id, title, created, body, category, sample) VALUES (?,?,?,?,?,?)")
  478. _, err = stmt.Exec(id, doc.Title, doc.Created, doc.Body, doc.Category, doc.MakeSample())
  479. if err != nil {
  480. tx.Rollback()
  481. return Identifier(""), err
  482. }
  483. tx.Commit()
  484. return id, nil
  485. }
  486. /*
  487. Add an entry to the 'admin' table in the database
  488. :param item: an admin table k/v text to redirect pair
  489. :param tableName: the name of the table to populate the link in on the UI
  490. */
  491. func (s *SQLiteRepo) AddAdminTableEntry(item TableData, category string) error {
  492. tx, err := s.db.Begin()
  493. if err != nil {
  494. return err
  495. }
  496. stmt, _ := tx.Prepare("INSERT INTO admin (display_name, link, category) VALUES (?,?,?)")
  497. _, err = stmt.Exec(item.DisplayName, item.Link, category)
  498. if err != nil {
  499. tx.Rollback()
  500. return err
  501. }
  502. tx.Commit()
  503. return nil
  504. }
  505. /*
  506. Delete a document from the db
  507. :param id: the identifier of the document to remove
  508. */
  509. func (s *SQLiteRepo) DeleteDocument(id Identifier) error {
  510. tx, err := s.db.Begin()
  511. if err != nil {
  512. return err
  513. }
  514. stmt, _ := tx.Prepare("DELETE FROM posts WHERE id=?")
  515. _, err = stmt.Exec(id)
  516. if err != nil {
  517. tx.Rollback()
  518. return err
  519. }
  520. tx.Commit()
  521. return nil
  522. }
  523. /*
  524. Delete navigation bar item
  525. :param id: the name or 'id' of the navbar item to remove
  526. */
  527. func (s *SQLiteRepo) DeleteNavbarItem(id Identifier) error {
  528. tx, err := s.db.Begin()
  529. if err != nil {
  530. return err
  531. }
  532. stmt, _ := tx.Prepare("DELETE FROM navbar WHERE link=?")
  533. _, err = stmt.Exec(id)
  534. if err != nil {
  535. tx.Rollback()
  536. return err
  537. }
  538. tx.Commit()
  539. return nil
  540. }
  541. // Get all Hosts from the host table
  542. func (s *SQLiteRepo) AllDocuments() []Document {
  543. rows, err := s.db.Query("SELECT * FROM posts")
  544. if err != nil {
  545. fmt.Printf("There was an issue getting all posts. %s", err.Error())
  546. return nil
  547. }
  548. defer rows.Close()
  549. all := []Document{}
  550. for rows.Next() {
  551. var post Document
  552. if err := rows.Scan(&post.Row, &post.Ident, &post.Title, &post.Created, &post.Body, &post.Category, &post.Sample); err != nil {
  553. fmt.Printf("There was an error getting all documents. %s", err.Error())
  554. return nil
  555. }
  556. all = append(all, post)
  557. }
  558. return all
  559. }
  560. type InvalidSkipArg struct{ Skip int }
  561. func (i *InvalidSkipArg) Error() string {
  562. return fmt.Sprintf("Invalid skip amount was passed: %v", i.Skip)
  563. }
  564. type ImageStoreItem struct {
  565. Identifier string `json:"identifier"`
  566. Filename string `json:"filename"`
  567. AbsolutePath string `json:"absolute_path"`
  568. Title string `json:"title" form:"title"`
  569. Created string `json:"created"`
  570. Desc string `json:"description" form:"description"`
  571. Category string `json:"category"`
  572. ApiPath string
  573. }
  574. /*
  575. Function to return the location of the image store. Wrapping the env call in
  576. a function so that refactoring is easier
  577. */
  578. func GetImageStore() string {
  579. return os.Getenv(env.IMAGE_STORE)
  580. }
  581. // Wrapping the new id call in a function to make refactoring easier
  582. func newIdentifier() Identifier {
  583. return Identifier(uuid.NewString())
  584. }