Add MediaMetadata model.

Add a model for storing image and video metadata for uploaded files.

Metadata is extracted using ExifTool. You will need to install ExifTool
after this commit. ExifTool 12.22 is the minimum required version
because we use the `--binary` option, which was added in this release.

The MediaMetadata model is separate from the MediaAsset model because
some files contain tons of metadata, and most of it is non-essential.
The MediaAsset model represents an uploaded file and contains essential
metadata, like the file's size and type, while the MediaMetadata model
represents all the other non-essential metadata associated with a file.

Metadata is stored as a JSON column in the database.

ExifTool returns all the file's metadata, not just the EXIF metadata.
EXIF is one of several types of image metadata, hence why we call
it MediaMetadata instead of EXIFMetadata.
This commit is contained in:
evazion
2021-09-07 18:22:34 -05:00
parent 291758ddb7
commit 3d660953d4
20 changed files with 235 additions and 21 deletions

View File

@@ -1,4 +1,17 @@
class MediaAsset < ApplicationRecord
has_one :media_metadata, dependent: :destroy
def self.create_from_media_file!(media_file)
create!(
md5: media_file.md5,
file_ext: media_file.file_ext,
file_size: media_file.file_size,
image_width: media_file.width,
image_height: media_file.height,
media_metadata: MediaMetadata.new(metadata: media_file.metadata),
)
end
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :md5, :file_ext, :file_size, :image_width, :image_height)
q = q.apply_default_order(params)

View File

@@ -0,0 +1,22 @@
# MediaMetadata represents the EXIF and other metadata associated with a
# MediaAsset (an uploaded image or video file). The `metadata` field contains a
# JSON hash of the file's metadata as returned by ExifTool.
#
# @see ExifTool
# @see https://exiftool.org/TagNames/index.html
class MediaMetadata < ApplicationRecord
self.table_name = "media_metadata"
attribute :id
attribute :created_at
attribute :updated_at
attribute :media_asset_id
attribute :metadata
belongs_to :media_asset
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :media_asset_id)
q = q.apply_default_order(params)
q
end
end

View File

@@ -65,6 +65,7 @@ class Upload < ApplicationRecord
belongs_to :uploader, :class_name => "User"
belongs_to :post, optional: true
has_one :media_asset, foreign_key: :md5, primary_key: :md5
before_validation :initialize_attributes, on: :create
before_validation :assign_rating_from_tags
@@ -114,6 +115,7 @@ class Upload < ApplicationRecord
return
end
media_asset.destroy!
DanbooruLogger.info("Uploads: Deleting files for upload md5=#{md5}")
Danbooru.config.storage_manager.delete_file(nil, md5, file_ext, :original)
Danbooru.config.storage_manager.delete_file(nil, md5, file_ext, :large)