This file is indexed.

/usr/share/beets/beetsplug/ipfs.py is in beets 1.4.6-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
# This file is part of beets.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Adds support for ipfs. Requires go-ipfs and a running ipfs daemon
"""

from __future__ import division, absolute_import, print_function

from beets import ui, util, library, config
from beets.plugins import BeetsPlugin

import subprocess
import shutil
import os
import tempfile


class IPFSPlugin(BeetsPlugin):

    def __init__(self):
        super(IPFSPlugin, self).__init__()
        self.config.add({
            'auto': True,
        })

        if self.config['auto']:
            self.import_stages = [self.auto_add]

    def commands(self):
        cmd = ui.Subcommand('ipfs',
                            help='interact with ipfs')
        cmd.parser.add_option('-a', '--add', dest='add',
                                    action='store_true',
                                    help='Add to ipfs')
        cmd.parser.add_option('-g', '--get', dest='get',
                                    action='store_true',
                                    help='Get from ipfs')
        cmd.parser.add_option('-p', '--publish', dest='publish',
                                    action='store_true',
                                    help='Publish local library to ipfs')
        cmd.parser.add_option('-i', '--import', dest='_import',
                                    action='store_true',
                                    help='Import remote library from ipfs')
        cmd.parser.add_option('-l', '--list', dest='_list',
                                    action='store_true',
                                    help='Query imported libraries')
        cmd.parser.add_option('-m', '--play', dest='play',
                                    action='store_true',
                                    help='Play music from remote libraries')

        def func(lib, opts, args):
            if opts.add:
                for album in lib.albums(ui.decargs(args)):
                    if len(album.items()) == 0:
                        self._log.info('{0} does not contain items, aborting',
                                       album)

                    self.ipfs_add(album)
                    album.store()

            if opts.get:
                self.ipfs_get(lib, ui.decargs(args))

            if opts.publish:
                self.ipfs_publish(lib)

            if opts._import:
                self.ipfs_import(lib, ui.decargs(args))

            if opts._list:
                self.ipfs_list(lib, ui.decargs(args))

            if opts.play:
                self.ipfs_play(lib, opts, ui.decargs(args))

        cmd.func = func
        return [cmd]

    def auto_add(self, session, task):
        if task.is_album:
            if self.ipfs_add(task.album):
                task.album.store()

    def ipfs_play(self, lib, opts, args):
        from beetsplug.play import PlayPlugin

        jlib = self.get_remote_lib(lib)
        player = PlayPlugin()
        config['play']['relative_to'] = None
        player.album = True
        player.play_music(jlib, player, args)

    def ipfs_add(self, album):
        try:
            album_dir = album.item_dir()
        except AttributeError:
            return False
        try:
            if album.ipfs:
                self._log.debug('{0} already added', album_dir)
                # Already added to ipfs
                return False
        except AttributeError:
            pass

        self._log.info('Adding {0} to ipfs', album_dir)

        cmd = "ipfs add -q -r".split()
        cmd.append(album_dir)
        try:
            output = util.command_output(cmd).split()
        except (OSError, subprocess.CalledProcessError) as exc:
            self._log.error(u'Failed to add {0}, error: {1}', album_dir, exc)
            return False
        length = len(output)

        for linenr, line in enumerate(output):
            line = line.strip()
            if linenr == length - 1:
                # last printed line is the album hash
                self._log.info("album: {0}", line)
                album.ipfs = line
            else:
                try:
                    item = album.items()[linenr]
                    self._log.info("item: {0}", line)
                    item.ipfs = line
                    item.store()
                except IndexError:
                    # if there's non music files in the to-add folder they'll
                    # get ignored here
                    pass

        return True

    def ipfs_get(self, lib, query):
        query = query[0]
        # Check if query is a hash
        if query.startswith("Qm") and len(query) == 46:
            self.ipfs_get_from_hash(lib, query)
        else:
            albums = self.query(lib, query)
            for album in albums:
                self.ipfs_get_from_hash(lib, album.ipfs)

    def ipfs_get_from_hash(self, lib, _hash):
        try:
            cmd = "ipfs get".split()
            cmd.append(_hash)
            util.command_output(cmd)
        except (OSError, subprocess.CalledProcessError) as err:
            self._log.error('Failed to get {0} from ipfs.\n{1}',
                            _hash, err.output)
            return False

        self._log.info('Getting {0} from ipfs', _hash)
        imp = ui.commands.TerminalImportSession(lib, loghandler=None,
                                                query=None, paths=[_hash])
        imp.run()
        shutil.rmtree(_hash)

    def ipfs_publish(self, lib):
        with tempfile.NamedTemporaryFile() as tmp:
            self.ipfs_added_albums(lib, tmp.name)
            try:
                cmd = "ipfs add -q ".split()
                cmd.append(tmp.name)
                output = util.command_output(cmd)
            except (OSError, subprocess.CalledProcessError) as err:
                msg = "Failed to publish library. Error: {0}".format(err)
                self._log.error(msg)
                return False
            self._log.info("hash of library: {0}", output)

    def ipfs_import(self, lib, args):
        _hash = args[0]
        if len(args) > 1:
            lib_name = args[1]
        else:
            lib_name = _hash
        lib_root = os.path.dirname(lib.path)
        remote_libs = lib_root + "/remotes"
        if not os.path.exists(remote_libs):
            try:
                os.makedirs(remote_libs)
            except OSError as e:
                msg = "Could not create {0}. Error: {1}".format(remote_libs, e)
                self._log.error(msg)
                return False
        path = remote_libs + "/" + lib_name + ".db"
        if not os.path.exists(path):
            cmd = "ipfs get {0} -o".format(_hash).split()
            cmd.append(path)
            try:
                util.command_output(cmd)
            except (OSError, subprocess.CalledProcessError):
                self._log.error("Could not import {0}".format(_hash))
                return False

        # add all albums from remotes into a combined library
        jpath = remote_libs + "/joined.db"
        jlib = library.Library(jpath)
        nlib = library.Library(path)
        for album in nlib.albums():
            if not self.already_added(album, jlib):
                new_album = []
                for item in album.items():
                    item.id = None
                    new_album.append(item)
                added_album = jlib.add_album(new_album)
                added_album.ipfs = album.ipfs
                added_album.store()

    def already_added(self, check, jlib):
        for jalbum in jlib.albums():
            if jalbum.mb_albumid == check.mb_albumid:
                return True
        return False

    def ipfs_list(self, lib, args):
        fmt = config['format_album'].get()
        try:
            albums = self.query(lib, args)
        except IOError:
            ui.print_("No imported libraries yet.")
            return

        for album in albums:
            ui.print_(format(album, fmt), " : ", album.ipfs)

    def query(self, lib, args):
        rlib = self.get_remote_lib(lib)
        albums = rlib.albums(args)
        return albums

    def get_remote_lib(self, lib):
        lib_root = os.path.dirname(lib.path)
        remote_libs = lib_root + "/remotes"
        path = remote_libs + "/joined.db"
        if not os.path.isfile(path):
            raise IOError
        return library.Library(path)

    def ipfs_added_albums(self, rlib, tmpname):
        """ Returns a new library with only albums/items added to ipfs
        """
        tmplib = library.Library(tmpname)
        for album in rlib.albums():
            try:
                if album.ipfs:
                    self.create_new_album(album, tmplib)
            except AttributeError:
                pass
        return tmplib

    def create_new_album(self, album, tmplib):
        items = []
        for item in album.items():
            try:
                if not item.ipfs:
                    break
            except AttributeError:
                pass
            item_path = os.path.basename(item.path).decode(
                util._fsencoding(), 'ignore'
            )
            # Clear current path from item
            item.path = '/ipfs/{0}/{1}'.format(album.ipfs, item_path)

            item.id = None
            items.append(item)
        if len(items) < 1:
            return False
        self._log.info("Adding '{0}' to temporary library", album)
        new_album = tmplib.add_album(items)
        new_album.ipfs = album.ipfs
        new_album.store()