shortcut.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package shortcut
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. "imuslab.com/arozos/mod/filesystem/arozfs"
  7. "imuslab.com/arozos/mod/utils"
  8. )
  9. /*
  10. A simple package to better handle shortcuts in ArozOS
  11. Author: tobychui
  12. */
  13. func ReadShortcut(shortcutContent []byte) (*arozfs.ShortcutData, error) {
  14. //Split the content of the shortcut files into lines
  15. fileContent := strings.ReplaceAll(strings.TrimSpace(string(shortcutContent)), "\r\n", "\n")
  16. lines := strings.Split(fileContent, "\n")
  17. if len(lines) < 4 {
  18. return nil, errors.New("Corrupted Shortcut File")
  19. }
  20. for i := 0; i < len(lines); i++ {
  21. lines[i] = strings.TrimSpace(lines[i])
  22. }
  23. //Render it as shortcut data
  24. result := arozfs.ShortcutData{
  25. Type: lines[0],
  26. Name: lines[1],
  27. Path: lines[2],
  28. Icon: lines[3],
  29. }
  30. return &result, nil
  31. }
  32. //Generate the content of a shortcut base the the four important field of shortcut information
  33. func GenerateShortcutBytes(shortcutTarget string, shortcutType string, shortcutText string, shortcutIcon string) []byte {
  34. //Check if there are desktop icon. If yes, override icon on module
  35. if shortcutType == "module" && utils.FileExists(arozfs.ToSlash(filepath.Join("./web/", filepath.Dir(shortcutIcon), "/desktop_icon.png"))) {
  36. shortcutIcon = arozfs.ToSlash(filepath.Join(filepath.Dir(shortcutIcon), "/desktop_icon.png"))
  37. }
  38. //Clean the shortcut text
  39. shortcutText = arozfs.FilterIllegalCharInFilename(shortcutText, " ")
  40. return []byte(shortcutType + "\n" + shortcutText + "\n" + shortcutTarget + "\n" + shortcutIcon)
  41. }