storage.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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) GetMenuItemByName(link, text string) (LinkPair, bool) {
  201. rows := s.db.QueryRow("SELECT * FROM menu WHERE link = ? AND text = ?", link, 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. }
  209. log.Printf("%+v\n", item)
  210. return item, true
  211. }
  212. // get Admin table entry by its display name, link, and category.
  213. func (s *SQLiteRepo) GetAdminTableEntry(displayName, link, category string) (TableData, bool) {
  214. rows := s.db.QueryRow("SELECT * FROM admin WHERE display_name = ? AND link = ? AND category = ?", displayName, link, category)
  215. var item TableData
  216. var id int
  217. if err := rows.Scan(&id, &item.DisplayName, &item.Link, &category); err != nil {
  218. if errors.Is(err, sql.ErrNoRows) {
  219. return item, false
  220. }
  221. log.Fatal(err)
  222. }
  223. return item, true
  224. }
  225. // get navbar entry.
  226. func (s *SQLiteRepo) GetNavbarLink(link, redirect string) (NavBarItem, bool) {
  227. rows := s.db.QueryRow("SELECT * FROM navbar WHERE link = ? AND redirect = ?", link, redirect)
  228. var item NavBarItem
  229. var id int
  230. if err := rows.Scan(&id, &item.Png, &item.Link, &item.Redirect); err != nil {
  231. if errors.Is(err, sql.ErrNoRows) {
  232. return item, false
  233. }
  234. log.Fatal(err)
  235. }
  236. return item, true
  237. }
  238. // get an asset from the store
  239. func (s *SQLiteRepo) GetAsset(name string) (Asset, bool) {
  240. rows := s.db.QueryRow("SELECT * FROM assets WHERE name = ?", name)
  241. var item Asset
  242. var id int
  243. if err := rows.Scan(&id, &item.Name, &item.Data); err != nil {
  244. if errors.Is(err, sql.ErrNoRows) {
  245. return item, false
  246. }
  247. log.Fatal(err)
  248. }
  249. return item, true
  250. }
  251. /*
  252. 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
  253. /*
  254. 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
  255. */
  256. func (s *SQLiteRepo) GetNavBarLinks() []NavBarItem {
  257. rows, err := s.db.Query("SELECT * FROM navbar")
  258. var navbarItems []NavBarItem
  259. defer rows.Close()
  260. for rows.Next() {
  261. var item NavBarItem
  262. var id int
  263. err = rows.Scan(&id, &item.Png, &item.Link, &item.Redirect)
  264. if err != nil {
  265. log.Fatal(err)
  266. }
  267. navbarItems = append(navbarItems, item)
  268. }
  269. return navbarItems
  270. }
  271. /*
  272. get all assets from the asset table
  273. */
  274. func (s *SQLiteRepo) GetAssets() []Asset {
  275. rows, err := s.db.Query("SELECT * FROM assets")
  276. var assets []Asset
  277. defer rows.Close()
  278. for rows.Next() {
  279. var item Asset
  280. var id int
  281. err = rows.Scan(&id, &item.Name, &item.Data)
  282. if err != nil {
  283. log.Fatal(err)
  284. }
  285. assets = append(assets, item)
  286. }
  287. return assets
  288. }
  289. /*
  290. get all assets from the asset table
  291. */
  292. func (s *SQLiteRepo) GetAdminTables() AdminPage {
  293. rows, err := s.db.Query("SELECT * FROM admin")
  294. adminPage := AdminPage{Tables: map[string][]TableData{}}
  295. defer rows.Close()
  296. for rows.Next() {
  297. var item TableData
  298. var id int
  299. var category string
  300. err = rows.Scan(&id, &item.DisplayName, &item.Link, &category)
  301. if err != nil {
  302. log.Fatal(err)
  303. }
  304. adminPage.Tables[category] = append(adminPage.Tables[category], item)
  305. }
  306. return adminPage
  307. }
  308. /*
  309. Retrieve a document from the sqlite db
  310. :param id: the Identifier of the post
  311. */
  312. func (s *SQLiteRepo) GetDocument(id Identifier) (Document, error) {
  313. row := s.db.QueryRow("SELECT * FROM posts WHERE id = ?", id)
  314. var post Document
  315. var rowNum int
  316. if err := row.Scan(&rowNum, &post.Ident, &post.Title, &post.Created, &post.Body, &post.Category, &post.Sample); err != nil {
  317. if errors.Is(err, sql.ErrNoRows) {
  318. return post, ErrNotExists
  319. }
  320. return post, err
  321. }
  322. return post, nil
  323. }
  324. /*
  325. Get all documents by category
  326. :param category: the category to retrieve all docs from
  327. */
  328. func (s *SQLiteRepo) GetByCategory(category string) []Document {
  329. rows, err := s.db.Query("SELECT * FROM posts WHERE category = ?", category)
  330. if err != nil {
  331. log.Fatal(err)
  332. }
  333. var docs []Document
  334. defer rows.Close()
  335. for rows.Next() {
  336. var doc Document
  337. err := rows.Scan(&doc.Row, &doc.Ident, &doc.Title, &doc.Created, &doc.Body, &doc.Category, &doc.Sample)
  338. if err != nil {
  339. log.Fatal(err)
  340. }
  341. docs = append(docs, doc)
  342. }
  343. err = rows.Err()
  344. if err != nil {
  345. log.Fatal(err)
  346. }
  347. return docs
  348. }
  349. /*
  350. get image data from the images table
  351. :param id: the serial identifier of the post
  352. */
  353. func (s *SQLiteRepo) GetImage(id Identifier) (Image, error) {
  354. row := s.db.QueryRow("SELECT * FROM images WHERE id = ?", id)
  355. var rowNum int
  356. var title, desc, created string
  357. if err := row.Scan(&rowNum, &id, &title, &desc, &created); err != nil {
  358. if errors.Is(err, sql.ErrNoRows) {
  359. return Image{}, ErrNotExists
  360. }
  361. return Image{}, err
  362. }
  363. data, err := s.imageIO.Get(id)
  364. if err != nil {
  365. return Image{}, err
  366. }
  367. return Image{Ident: id, Title: title, Desc: desc, Data: data, Created: created}, nil
  368. }
  369. /*
  370. Get all of the images from the datastore
  371. */
  372. func (s *SQLiteRepo) GetAllImages() []Image {
  373. rows, err := s.db.Query("SELECT * FROM images")
  374. if err != nil {
  375. log.Fatal(err)
  376. }
  377. imgs := []Image{}
  378. for rows.Next() {
  379. var img Image
  380. var rowNum int
  381. err := rows.Scan(&rowNum, &img.Ident, &img.Title, &img.Desc, &img.Created)
  382. if err != nil {
  383. log.Fatal(err)
  384. }
  385. b, err := s.imageIO.Get(img.Ident)
  386. if err != nil {
  387. log.Fatal(err)
  388. }
  389. imgs = append(imgs, Image{Ident: img.Ident, Title: img.Title, Desc: img.Desc, Data: b, Created: img.Created})
  390. }
  391. err = rows.Err()
  392. if err != nil {
  393. log.Fatal(err)
  394. }
  395. return imgs
  396. }
  397. /*
  398. Add an image to the database
  399. :param title: the title of the image
  400. :param location: the location to save the image to
  401. :param desc: the description of the image, if any
  402. :param data: the binary data for the image
  403. */
  404. func (s *SQLiteRepo) AddImage(data []byte, title string, desc string) (Identifier, error) {
  405. id := newIdentifier()
  406. err := s.imageIO.Put(data, id)
  407. if err != nil {
  408. return Identifier(""), err
  409. }
  410. _, err = s.db.Exec("INSERT INTO images (id, title, desc, created) VALUES (?,?,?,?)", string(id), title, desc, time.Now().String())
  411. if err != nil {
  412. return Identifier(""), err
  413. }
  414. return id, nil
  415. }
  416. /*
  417. Updates a document in the database with the supplied. Only changes the title, the body, category. Keys off of the documents Identifier
  418. :param doc: the Document to upload into the database
  419. */
  420. func (s *SQLiteRepo) UpdateDocument(doc Document) error {
  421. tx, err := s.db.Begin()
  422. if err != nil {
  423. return err
  424. }
  425. stmt, err := tx.Prepare("UPDATE posts SET title = ?, body = ?, category = ?, sample = ? WHERE id = ?;")
  426. if err != nil {
  427. tx.Rollback()
  428. return err
  429. }
  430. res, err := stmt.Exec(doc.Title, doc.Body, doc.Category, doc.MakeSample(), doc.Ident)
  431. if err != nil {
  432. tx.Rollback()
  433. return err
  434. }
  435. affected, _ := res.RowsAffected()
  436. if affected != 1 {
  437. return ErrNotExists
  438. }
  439. tx.Commit()
  440. return nil
  441. }
  442. /*
  443. Adds a LinkPair to the menu database table
  444. :param item: the LinkPair to upload
  445. */
  446. func (s *SQLiteRepo) AddMenuItem(item LinkPair) error {
  447. tx, err := s.db.Begin()
  448. if err != nil {
  449. return err
  450. }
  451. _, found := s.GetMenuItemByName(item.Link, item.Text)
  452. if found {
  453. tx.Rollback()
  454. return ErrDuplicate
  455. }
  456. stmt, _ := tx.Prepare("INSERT INTO menu(link, text) VALUES (?,?)")
  457. _, err = stmt.Exec(item.Link, item.Text)
  458. if err != nil {
  459. tx.Rollback()
  460. return err
  461. }
  462. tx.Commit()
  463. return nil
  464. }
  465. /*
  466. Adds an item to the navbar database table
  467. :param item: the NavBarItem to upload
  468. */
  469. func (s *SQLiteRepo) AddNavbarItem(item NavBarItem) error {
  470. tx, err := s.db.Begin()
  471. if err != nil {
  472. return err
  473. }
  474. _, found := s.GetNavbarLink(item.Link, item.Redirect)
  475. if found {
  476. tx.Rollback()
  477. return ErrDuplicate
  478. }
  479. stmt, err := tx.Prepare("INSERT INTO navbar(png, link, redirect) VALUES (?,?,?)")
  480. if err != nil {
  481. tx.Rollback()
  482. return err
  483. }
  484. _, err = stmt.Exec(item.Png, item.Link, item.Redirect)
  485. if err != nil {
  486. tx.Rollback()
  487. return err
  488. }
  489. tx.Commit()
  490. return nil
  491. }
  492. /*
  493. Adds an asset to the asset database table asset
  494. :param name: the name of the asset (filename)
  495. :param data: the byte array of the PNG to upload TODO: limit this to 256kb
  496. */
  497. func (s *SQLiteRepo) AddAsset(name string, data []byte) error {
  498. tx, err := s.db.Begin()
  499. if err != nil {
  500. return err
  501. }
  502. _, found := s.GetAsset(name)
  503. if found {
  504. tx.Rollback()
  505. return ErrDuplicate
  506. }
  507. stmt, _ := tx.Prepare("INSERT INTO assets(name, data) VALUES (?,?)")
  508. _, err = stmt.Exec(name, data)
  509. if err != nil {
  510. tx.Rollback()
  511. return err
  512. }
  513. tx.Commit()
  514. return nil
  515. }
  516. /*
  517. Adds a document to the database (for text posts)
  518. :param doc: the Document to add
  519. */
  520. func (s *SQLiteRepo) AddDocument(doc Document) (Identifier, error) {
  521. id := newIdentifier()
  522. tx, err := s.db.Begin()
  523. if err != nil {
  524. return Identifier(""), err
  525. }
  526. stmt, _ := tx.Prepare("INSERT INTO posts(id, title, created, body, category, sample) VALUES (?,?,?,?,?,?)")
  527. _, err = stmt.Exec(id, doc.Title, doc.Created, doc.Body, doc.Category, doc.MakeSample())
  528. if err != nil {
  529. tx.Rollback()
  530. return Identifier(""), err
  531. }
  532. tx.Commit()
  533. return id, nil
  534. }
  535. /*
  536. Add an entry to the 'admin' table in the database
  537. :param item: an admin table k/v text to redirect pair
  538. :param tableName: the name of the table to populate the link in on the UI
  539. */
  540. func (s *SQLiteRepo) AddAdminTableEntry(item TableData, category string) error {
  541. tx, err := s.db.Begin()
  542. if err != nil {
  543. return err
  544. }
  545. _, found := s.GetAdminTableEntry(item.DisplayName, item.Link, category)
  546. if found {
  547. tx.Rollback()
  548. return ErrDuplicate
  549. }
  550. stmt, _ := tx.Prepare("INSERT INTO admin (display_name, link, category) VALUES (?,?,?)")
  551. _, err = stmt.Exec(item.DisplayName, item.Link, category)
  552. if err != nil {
  553. tx.Rollback()
  554. return err
  555. }
  556. tx.Commit()
  557. return nil
  558. }
  559. /*
  560. Delete a document from the db
  561. :param id: the identifier of the document to remove
  562. */
  563. func (s *SQLiteRepo) DeleteDocument(id Identifier) error {
  564. tx, err := s.db.Begin()
  565. if err != nil {
  566. return err
  567. }
  568. stmt, _ := tx.Prepare("DELETE FROM posts WHERE id=?")
  569. _, err = stmt.Exec(id)
  570. if err != nil {
  571. tx.Rollback()
  572. return err
  573. }
  574. tx.Commit()
  575. return nil
  576. }
  577. /*
  578. Delete navigation bar item
  579. :param id: the name or 'id' of the navbar item to remove
  580. */
  581. func (s *SQLiteRepo) DeleteNavbarItem(id Identifier) error {
  582. tx, err := s.db.Begin()
  583. if err != nil {
  584. return err
  585. }
  586. stmt, _ := tx.Prepare("DELETE FROM navbar WHERE link=?")
  587. result, err := stmt.Exec(id)
  588. if err != nil {
  589. tx.Rollback()
  590. return err
  591. }
  592. rowsAffected, _ := result.RowsAffected()
  593. if rowsAffected < 1 {
  594. return ErrNotExists
  595. }
  596. tx.Commit()
  597. return nil
  598. }
  599. // Get all Hosts from the host table
  600. func (s *SQLiteRepo) AllDocuments() []Document {
  601. rows, err := s.db.Query("SELECT * FROM posts")
  602. if err != nil {
  603. fmt.Printf("There was an issue getting all posts. %s", err.Error())
  604. return nil
  605. }
  606. defer rows.Close()
  607. all := []Document{}
  608. for rows.Next() {
  609. var post Document
  610. if err := rows.Scan(&post.Row, &post.Ident, &post.Title, &post.Created, &post.Body, &post.Category, &post.Sample); err != nil {
  611. fmt.Printf("There was an error getting all documents. %s", err.Error())
  612. return nil
  613. }
  614. all = append(all, post)
  615. }
  616. return all
  617. }
  618. type InvalidSkipArg struct{ Skip int }
  619. func (i *InvalidSkipArg) Error() string {
  620. return fmt.Sprintf("Invalid skip amount was passed: %v", i.Skip)
  621. }
  622. type ImageStoreItem struct {
  623. Identifier string `json:"identifier"`
  624. Filename string `json:"filename"`
  625. AbsolutePath string `json:"absolute_path"`
  626. Title string `json:"title" form:"title"`
  627. Created string `json:"created"`
  628. Desc string `json:"description" form:"description"`
  629. Category string `json:"category"`
  630. ApiPath string
  631. }
  632. /*
  633. Function to return the location of the image store. Wrapping the env call in
  634. a function so that refactoring is easier
  635. */
  636. func GetImageStore() string {
  637. return os.Getenv(env.IMAGE_STORE)
  638. }
  639. // Wrapping the new id call in a function to make refactoring easier
  640. func newIdentifier() Identifier {
  641. return Identifier(uuid.NewString())
  642. }