Skip to content
Snippets Groups Projects
files.go 2.09 KiB
Newer Older
Christian Pointner's avatar
Christian Pointner committed
//
//  tank
//
//  Import and Playlist Daemon for autoradio project
//
//
//  Copyright (C) 2017-2018 Christian Pointner <equinox@helsinki.at>
Christian Pointner's avatar
Christian Pointner committed
//
//  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)
}

Christian Pointner's avatar
Christian Pointner committed
func (st *Store) ListFiles(group string) (files Files, err error) {
	// err = st.db.Model(&Group{Name: group}).Related(&files).Error
	err = st.db.Where("group_name = ?", group).Find(&files).Error
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{}
	err = st.db.First(file, id).Error
func (st *Store) UpdateFile(group string, id uint64, file File) (*File, error) {
	file.ID = id
	file.GroupName = group
	err := st.db.Update(&file).Error
	return &file, err
func (st *Store) DeleteFile(group string, id uint64) error {
	if err := st.db.Delete(&File{ID: id}).Error; err != nil {
		return err
	}
	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
Christian Pointner's avatar
Christian Pointner committed
}