1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=0);
24
25namespace Ampache\Module\WebDav;
26
27use Ampache\Repository\Model\Media;
28use Ampache\Repository\Model\Catalog;
29use Ampache\Module\Util\ObjectTypeToClassNameMapper;
30use Sabre\DAV;
31
32/**
33 * This class wrap Ampache songs to WebDAV files.
34 */
35class WebDavFile extends DAV\File
36{
37    private $libitem;
38
39    /**
40     * @param Media $libitem
41     */
42    public function __construct(Media $libitem)
43    {
44        $this->libitem = $libitem;
45        $this->libitem->format();
46    }
47
48    /**
49     * getName
50     * @return string
51     */
52    public function getName()
53    {
54        return $this->libitem->f_file;
55    }
56
57    /**
58     * get
59     * @return resource|null
60     */
61    public function get()
62    {
63        debug_event(self::class, 'File get', 5);
64        // Only media associated to a local catalog is supported
65        if ($this->libitem->catalog) {
66            $catalog = Catalog::create_from_id($this->libitem->catalog);
67            if ($catalog->get_type() === 'local') {
68                return fopen($this->libitem->file, 'r');
69            } else {
70                debug_event(self::class, 'Catalog associated to the media is not local. This is currently unsupported.', 3);
71            }
72        } else {
73            debug_event(self::class, 'No catalog associated to the media.', 3);
74        }
75
76        return null;
77    }
78
79    /**
80     * getSize
81     * @return integer
82     */
83    public function getSize()
84    {
85        return $this->libitem->size;
86    }
87
88    /**
89     * getETag
90     * @return string
91     */
92    public function getETag()
93    {
94        return md5(ObjectTypeToClassNameMapper::reverseMap(get_class($this->libitem)) . "_" . $this->libitem->id . "_" . $this->libitem->update_time);
95    }
96}
97