// // tank // // Import and Playlist Daemon for autoradio project // // // Copyright (C) 2017-2018 Christian Pointner <equinox@helsinki.at> // // This file is part of tank. // // tank is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // tank is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with tank. If not, see <http://www.gnu.org/licenses/>. // package store import ( "fmt" "os" "path/filepath" ) func (st *Store) getFilePath(group string, id uint64) string { filename := fmt.Sprintf("%d.flac", id) // TODO: get file extension from config? return filepath.Join(st.getGroupPath(group), filename) } func (st *Store) ListFiles(group string) (files Files, err error) { // TODO: why does this not work? // err = st.db.Model(&Group{Name: group}).Related(&files).Error err = st.db.Where("group_name = ?", group).Find(&files).Error return } func (st *Store) CreateFile(group string, file File) (*File, error) { if _, err := st.CreateGroup(group); err != nil { return nil, err } file.ID = 0 file.GroupName = group err := st.db.Create(&file).Error return &file, err } func (st *Store) GetFile(group string, id uint64) (file *File, err error) { file = &File{} // we have to make sure that the file actually belongs to <group> // otherwise a bad user can trick us into returning files for groups it // normally has no access to err = st.db.Where("group_name = ?", group).First(file, id).Error return } func (st *Store) UpdateFile(group string, id uint64, file File) (*File, error) { tx := st.db.Begin() if err := tx.First(&File{}, id).Error; err != nil { tx.Rollback() return nil, err } file.ID = id file.GroupName = group err := tx.Save(&file).Error tx.Commit() return &file, err } func (st *Store) DeleteFile(group string, id uint64) error { tx := st.db.Begin() if err := tx.First(&File{}, id).Error; err != nil { tx.Rollback() return err } if err := tx.Delete(&File{ID: id}).Error; err != nil { // TODO: this is likely due to a FK constraint: // - fetch all playlists that are using this file... tx.Rollback() return err } tx.Commit() filename := st.getFilePath(group, id) if err := os.Remove(filename); err != nil && !os.IsNotExist(err) { return fmt.Errorf("unable to delete file '%s': %v", filename, err) } return nil }