This file is indexed.

/usr/share/beets/beetsplug/embedart.py is in beets 1.3.1-1.

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
# This file is part of beets.
# Copyright 2013, Adrian Sampson.
#
# 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.

"""Allows beets to embed album art into file metadata."""
import logging
import imghdr

from beets.plugins import BeetsPlugin
from beets import mediafile
from beets import ui
from beets.ui import decargs
from beets.util import syspath, normpath, displayable_path
from beets.util.artresizer import ArtResizer
from beets import config

log = logging.getLogger('beets')

def _embed(path, items, maxwidth=0):
    """Embed an image file, located at `path`, into each item.
    """
    if maxwidth:
        path = ArtResizer.shared.resize(maxwidth, syspath(path))

    data = open(syspath(path), 'rb').read()
    kindstr = imghdr.what(None, data)
    if kindstr is None:
        log.error(u'Could not embed art of unkown type: {0}'.format(
            displayable_path(path)
        ))
        return
    elif kindstr not in ('jpeg', 'png'):
        log.error(u'Image type {0} is not allowed as cover art: {1}'.format(
            kindstr, displayable_path(path)
        ))
        return

    # Add art to each file.
    log.debug('Embedding album art.')

    for item in items:
        try:
            f = mediafile.MediaFile(syspath(item.path))
        except mediafile.UnreadableFileError as exc:
            log.warn('Could not embed art in {0}: {1}'.format(
                displayable_path(item.path), exc
            ))
            continue
        f.art = data
        f.save()

class EmbedCoverArtPlugin(BeetsPlugin):
    """Allows albumart to be embedded into the actual files.
    """
    def __init__(self):
        super(EmbedCoverArtPlugin, self).__init__()
        self.config.add({
            'maxwidth': 0,
            'auto': True,
        })
        if self.config['maxwidth'].get(int) and \
                not ArtResizer.shared.local:
            self.config['maxwidth'] = 0
            log.warn("embedart: ImageMagick or PIL not found; "
                     "'maxwidth' option ignored")

    def commands(self):
        # Embed command.
        embed_cmd = ui.Subcommand('embedart',
            help='embed image files into file metadata')
        embed_cmd.parser.add_option('-f', '--file', metavar='PATH',
            help='the image file to embed')
        def embed_func(lib, opts, args):
            if opts.file:
                imagepath = normpath(opts.file)
                embed(lib, imagepath, decargs(args))
            else:
                embed_current(lib, decargs(args))
        embed_cmd.func = embed_func

        # Extract command.
        extract_cmd = ui.Subcommand('extractart',
                                    help='extract an image from file metadata')
        extract_cmd.parser.add_option('-o', dest='outpath',
                                      help='image output file')
        def extract_func(lib, opts, args):
            outpath = normpath(opts.outpath or 'cover')
            extract(lib, outpath, decargs(args))
        extract_cmd.func = extract_func

        # Clear command.
        clear_cmd = ui.Subcommand('clearart',
                                  help='remove images from file metadata')
        def clear_func(lib, opts, args):
            clear(lib, decargs(args))
        clear_cmd.func = clear_func

        return [embed_cmd, extract_cmd, clear_cmd]

# "embedart" command with --file argument.
def embed(lib, imagepath, query):
    albums = lib.albums(query)
    for i_album in albums:
        album = i_album
        break
    else:
        log.error('No album matches query.')
        return

    log.info(u'Embedding album art into {0.albumartist} - {0.album}.'.format(
        album
    ))
    _embed(imagepath, album.items(),
           config['embedart']['maxwidth'].get(int))

# "embedart" command without explicit file.
def embed_current(lib, query):
    albums = lib.albums(query)
    for album in albums:
        if not album.artpath:
            log.info(u'No album art present: {0} - {1}'.
                     format(album.albumartist, album.album))
            continue

        log.info(u'Embedding album art into {0} - {1}'.
                 format(album.albumartist, album.album))
        _embed(album.artpath, album.items(),
               config['embedart']['maxwidth'].get(int))

# "extractart" command.
def extract(lib, outpath, query):
    items = lib.items(query)
    for i_item in items:
        item = i_item
        break
    else:
        log.error('No item matches query.')
        return

    # Extract the art.
    try:
        mf = mediafile.MediaFile(syspath(item.path))
    except mediafile.UnreadableFileError as exc:
        log.error(u'Could not extract art from {0}: {1}'.format(
            displayable_path(item.path), exc
        ))
        return

    art = mf.art
    if not art:
        log.error('No album art present in %s - %s.' %
                  (item.artist, item.title))
        return

    # Add an extension to the filename.
    ext = imghdr.what(None, h=art)
    if not ext:
        log.error('Unknown image type.')
        return
    outpath += '.' + ext

    log.info(u'Extracting album art from: {0.artist} - {0.title}\n'
             u'To: {1}'.format(item, displayable_path(outpath)))
    with open(syspath(outpath), 'wb') as f:
        f.write(art)

# "clearart" command.
def clear(lib, query):
    log.info('Clearing album art from items:')
    for item in lib.items(query):
        log.info(u'%s - %s' % (item.artist, item.title))
        try:
            mf = mediafile.MediaFile(syspath(item.path))
        except mediafile.UnreadableFileError as exc:
            log.error(u'Could not clear art from {0}: {1}'.format(
                displayable_path(item.path), exc
            ))
            continue
        mf.art = None
        mf.save()

# Automatically embed art into imported albums.
@EmbedCoverArtPlugin.listen('album_imported')
def album_imported(lib, album):
    if album.artpath and config['embedart']['auto']:
        _embed(album.artpath, album.items(),
               config['embedart']['maxwidth'].get(int))