Skip to content
Snippets Groups Projects
shows.go 2.36 KiB
Newer Older
  • Learn to ignore specific revisions
  • //
    //  tank
    //
    //  Import and Playlist Daemon for autoradio project
    //
    //
    //  Copyright (C) 2017-2019 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 (
    	"errors"
    	"os"
    	"path/filepath"
    )
    
    func (st *Store) getShowPath(show string) string {
    	return filepath.Join(st.basePath, show)
    }
    
    // this will not fail if the show already exists
    func (st *Store) CreateShow(name string) (show *Show, err error) {
    	showDirPath := filepath.Join(st.basePath, name)
    	if err := os.Mkdir(showDirPath, 0755); err != nil {
    		if !os.IsExist(err) {
    			return nil, errors.New("unable to create directory for show '" + name + "': " + err.Error())
    		}
    		if stat, err := os.Stat(showDirPath); err != nil {
    			return nil, errors.New("unable to create directory for show '" + name + "': " + err.Error())
    		} else if !stat.IsDir() {
    			return nil, errors.New("unable to create directory for show '" + name + "': path exists and is not a directory")
    		}
    	}
    	show = &Show{Name: name}
    	err = st.db.FirstOrCreate(show).Error
    	return
    }
    
    func (st *Store) CloneShow(name, from string) error {
    	// TODO:
    	//   within transaction: {
    	//     check if show already exists -> error
    	//     st.CreateShow(name)
    	//     for file in files_of_show {
    	//       create file in show <name> -> <dest-id>
    	//       create hardlink for /store/base/path/<from>/<src-id> -> /store/base/path/<name>/<dest-id>
    	//     }
    	//   }
    	return ErrNotImplemented
    }
    
    // this will also remove all files and playlists belonging to the show!
    func (st *Store) DeleteShow(name string) error {
    	if err := st.db.Delete(&Show{Name: name}).Error; err != nil {
    		return err
    	}
    	return os.RemoveAll(st.getShowPath(name))
    }
    
    func (st *Store) ListShows() (shows Shows, err error) {
    	err = st.db.Find(&shows).Error
    	return
    }