|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/crazyuploader/rdctl-bot/internal/realdebrid" |
| 8 | + "github.com/gofiber/fiber/v2" |
| 9 | +) |
| 10 | + |
| 11 | +// GetStatus retrieves the Real-Debrid account status |
| 12 | +func (d *Dependencies) GetStatus(c *fiber.Ctx) error { |
| 13 | + user, err := d.RDClient.GetUser() |
| 14 | + if err != nil { |
| 15 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 16 | + } |
| 17 | + return c.JSON(fiber.Map{"success": true, "data": user}) |
| 18 | +} |
| 19 | + |
| 20 | +// GetTorrents retrieves the list of active torrents |
| 21 | +func (d *Dependencies) GetTorrents(c *fiber.Ctx) error { |
| 22 | + limit, _ := strconv.Atoi(c.Query("limit", "50")) |
| 23 | + offset, _ := strconv.Atoi(c.Query("offset", "0")) |
| 24 | + |
| 25 | + torrents, err := d.RDClient.GetTorrents(limit, offset) |
| 26 | + if err != nil { |
| 27 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 28 | + } |
| 29 | + |
| 30 | + // Format status and size for frontend convenience |
| 31 | + for i := range torrents { |
| 32 | + torrents[i].Status = realdebrid.FormatStatus(torrents[i].Status) |
| 33 | + } |
| 34 | + |
| 35 | + return c.JSON(fiber.Map{"success": true, "data": torrents}) |
| 36 | +} |
| 37 | + |
| 38 | +// GetTorrentInfo retrieves detailed information about a single torrent |
| 39 | +func (d *Dependencies) GetTorrentInfo(c *fiber.Ctx) error { |
| 40 | + id := c.Params("id") |
| 41 | + torrent, err := d.RDClient.GetTorrentInfo(id) |
| 42 | + if err != nil { |
| 43 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 44 | + } |
| 45 | + torrent.Status = realdebrid.FormatStatus(torrent.Status) |
| 46 | + return c.JSON(fiber.Map{"success": true, "data": torrent}) |
| 47 | +} |
| 48 | + |
| 49 | +// AddTorrent adds a new torrent from a magnet link |
| 50 | +func (d *Dependencies) AddTorrent(c *fiber.Ctx) error { |
| 51 | + var body struct { |
| 52 | + Magnet string `json:"magnet"` |
| 53 | + } |
| 54 | + if err := c.BodyParser(&body); err != nil { |
| 55 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"success": false, "error": "Invalid request body"}) |
| 56 | + } |
| 57 | + |
| 58 | + if body.Magnet == "" { |
| 59 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"success": false, "error": "Magnet link is required"}) |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := d.RDClient.AddMagnet(body.Magnet) |
| 63 | + if err != nil { |
| 64 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 65 | + } |
| 66 | + |
| 67 | + // Automatically select all files |
| 68 | + if err := d.RDClient.SelectAllFiles(resp.ID); err != nil { |
| 69 | + log.Printf("Failed to select files for torrent %s: %v", resp.ID, err) |
| 70 | + // Non-fatal, just log it |
| 71 | + } |
| 72 | + |
| 73 | + return c.Status(fiber.StatusCreated).JSON(fiber.Map{"success": true, "data": resp}) |
| 74 | +} |
| 75 | + |
| 76 | +// DeleteTorrent deletes a torrent |
| 77 | +func (d *Dependencies) DeleteTorrent(c *fiber.Ctx) error { |
| 78 | + id := c.Params("id") |
| 79 | + if err := d.RDClient.DeleteTorrent(id); err != nil { |
| 80 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 81 | + } |
| 82 | + return c.Status(fiber.StatusOK).JSON(fiber.Map{"success": true, "message": "Torrent deleted successfully"}) |
| 83 | +} |
| 84 | + |
| 85 | +// GetDownloads retrieves the download history |
| 86 | +func (d *Dependencies) GetDownloads(c *fiber.Ctx) error { |
| 87 | + limit, _ := strconv.Atoi(c.Query("limit", "50")) |
| 88 | + offset, _ := strconv.Atoi(c.Query("offset", "0")) |
| 89 | + |
| 90 | + downloads, err := d.RDClient.GetDownloads(limit, offset) |
| 91 | + if err != nil { |
| 92 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 93 | + } |
| 94 | + return c.JSON(fiber.Map{"success": true, "data": downloads}) |
| 95 | +} |
| 96 | + |
| 97 | +// UnrestrictLink unrestricts a hoster link |
| 98 | +func (d *Dependencies) UnrestrictLink(c *fiber.Ctx) error { |
| 99 | + var body struct { |
| 100 | + Link string `json:"link"` |
| 101 | + } |
| 102 | + if err := c.BodyParser(&body); err != nil { |
| 103 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"success": false, "error": "Invalid request body"}) |
| 104 | + } |
| 105 | + |
| 106 | + if body.Link == "" { |
| 107 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"success": false, "error": "Link is required"}) |
| 108 | + } |
| 109 | + |
| 110 | + unrestricted, err := d.RDClient.UnrestrictLink(body.Link) |
| 111 | + if err != nil { |
| 112 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 113 | + } |
| 114 | + |
| 115 | + return c.JSON(fiber.Map{"success": true, "data": unrestricted}) |
| 116 | +} |
| 117 | + |
| 118 | +// DeleteDownload deletes a download from history |
| 119 | +func (d *Dependencies) DeleteDownload(c *fiber.Ctx) error { |
| 120 | + id := c.Params("id") |
| 121 | + if err := d.RDClient.DeleteDownload(id); err != nil { |
| 122 | + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 123 | + } |
| 124 | + return c.Status(fiber.StatusOK).JSON(fiber.Map{"success": true, "message": "Download link removed successfully"}) |
| 125 | +} |
| 126 | + |
| 127 | +// GetUserStats retrieves statistics for a user |
| 128 | +func (d *Dependencies) GetUserStats(c *fiber.Ctx) error { |
| 129 | + userID, err := strconv.ParseUint(c.Params("id"), 10, 64) |
| 130 | + if err != nil { |
| 131 | + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"success": false, "error": "Invalid user ID"}) |
| 132 | + } |
| 133 | + |
| 134 | + stats, err := d.CommandRepo.GetUserStats(uint(userID)) |
| 135 | + if err != nil { |
| 136 | + return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"success": false, "error": err.Error()}) |
| 137 | + } |
| 138 | + return c.JSON(fiber.Map{"success": true, "data": stats}) |
| 139 | +} |
0 commit comments