-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
17 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
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,66 @@ | ||
package ctrladmin | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"mime/multipart" | ||
"strings" | ||
|
||
"github.com/jinzhu/gorm" | ||
"github.com/pkg/errors" | ||
|
||
"senan.xyz/g/gonic/model" | ||
) | ||
|
||
func playlistParseLine(c *Controller, playlistID int, path string) error { | ||
if strings.HasPrefix(path, "#") || strings.TrimSpace(path) == "" { | ||
return nil | ||
} | ||
track := &model.Track{} | ||
query := c.DB.Raw(` | ||
SELECT tracks.id FROM TRACKS | ||
JOIN albums ON tracks.album_id = albums.id | ||
WHERE (? || '/' || albums.left_path || albums.right_path || '/' || tracks.filename) = ? | ||
`, c.MusicPath, path) | ||
err := query.First(&track).Error | ||
switch { | ||
case gorm.IsRecordNotFoundError(err): | ||
return fmt.Errorf("couldn't match track %q", path) | ||
case err != nil: | ||
return errors.Wrap(err, "while matching") | ||
} | ||
c.DB.Create(&model.PlaylistItem{ | ||
PlaylistID: playlistID, | ||
TrackID: track.ID, | ||
}) | ||
return nil | ||
} | ||
|
||
func playlistParseUpload(c *Controller, userID int, header *multipart.FileHeader) ([]string, bool) { | ||
file, err := header.Open() | ||
if err != nil { | ||
return []string{fmt.Sprintf("couldn't open file %q", header.Filename)}, false | ||
} | ||
playlistName := strings.TrimSuffix(header.Filename, ".m3u8") | ||
if playlistName == "" { | ||
return []string{fmt.Sprintf("invalid filename %q", header.Filename)}, false | ||
} | ||
playlist := &model.Playlist{} | ||
c.DB.FirstOrCreate(playlist, model.Playlist{ | ||
Name: playlistName, | ||
UserID: userID, | ||
}) | ||
c.DB.Delete(&model.PlaylistItem{}, "playlist_id = ?", playlist.ID) | ||
var errors []string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
path := scanner.Text() | ||
if err := playlistParseLine(c, playlist.ID, path); err != nil { | ||
errors = append(errors, err.Error()) | ||
} | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return []string{fmt.Sprintf("scanning line of playlist: %v", err)}, true | ||
} | ||
return errors, true | ||
} |
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