Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How to distinguish between single and multiple file media?
In a media manager I have database tables for files, media and collections.
When a user is browsing a collection I want there to be links to see the metadata for single file media like books, movies, etc, but not for multiple file media. For example, for games or software, there would be a link to see the game metadata but when you open the game or software folder the rest of the files don't have a link to see the metadata. I would still hash those unimportant files but I don't want to keep metadata like rating, comments, etc for those. How can I make that distinction?
These are the tables:
CREATE TABLE IF NOT EXISTS "media" (
"uuid" text(36) NOT NULL PRIMARY KEY,
"kind" text NOT NULL, -- file type
"rating" real,
"comments" integer NOT NULL,
"views" integer NOT NULL,
"metadata" text NOT NULL, -- duration, width, height, codec, etc
"release" text, -- datetime
"created" text NOT NULL, -- datetime
"updated" text NOT NULL, -- datetime
"deleted" integer NOT NULL -- boolean
);
CREATE TABLE IF NOT EXISTS "file" (
"uuid" text(36) NOT NULL PRIMARY KEY,
"media_uuid" text(36) NOT NULL,
"path" text NOT NULL,
"size" integer NOT NULL,
"metadata" text NOT NULL, -- json: duration, width, height, codec, etc
"indexed" text NOT NULL, -- datetime
"updated" text NOT NULL, -- datetime
"deleted" integer NOT NULL, -- boolean
"parent" text(36), -- for directories
"is_directory" integer NOT NULL, -- boolean
FOREIGN KEY ("media_uuid") REFERENCES "media" ("uuid") ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS "collection" (
"uuid" text(36) NOT NULL PRIMARY KEY,
"name" text NOT NULL,
"description" text NOT NULL,
"owner_uuid" text(36),
"rating" real,
"views" integer NOT NULL,
"created" text NOT NULL, -- datetime
"updated" text NOT NULL, -- datetime
"deleted" integer NOT NULL, -- datetime
FOREIGN KEY ("owner_uuid") REFERENCES "user" ("uuid") ON DELETE CASCADE ON UPDATE CASCADE
);
2 answers
You could trivially create a view that wraps the media
table and includes a column that indicates if the media entry has more than one file associated with it.
CREATE VIEW IF NOT EXISTS "media_read" AS
SELECT "media".*, count("file"."uuid") > 1 AS "is_multifile"
FROM "media"
LEFT JOIN "file" ON "media_uuid" = "media"."uuid"
GROUP BY "media"."uuid";
0 comment threads
ChatGPT's answer solved this issue for me. Although I wish there was an automatic alternative that didn't require the user to manually mark each media as either single or multiple file.
To make the distinction between single file media and multiple file media in your database, you could add a new column to the media table called is_single_file, which is a boolean column that indicates whether the media is a single file or not.
For example, you could add the following column to the media table:
"is_single_file" integer NOT NULL DEFAULT 1
Then, when you insert a new row into the media table, you can set the is_single_file column to 1 if the media is a single file, and 0 if it is a multiple file media.
For example, if you are inserting a new movie into the media table, you would set the is_single_file column to 1, because movies are typically single file media. On the other hand, if you are inserting a new software into the media table, you would set the is_single_file column to 0, because software is typically a multiple file media.
Once you have this column in place, you can use it to filter the media table when you are displaying links to see the metadata for single file media. For example, you could use a query like the following to get all the single file media in a collection:
SELECT * FROM media WHERE collection_uuid = :collection_uuid AND is_single_file = 1
This would return all the rows in the media table that belong to the specified collection and are single file media. You can then use these rows to display links to see the metadata for the single file media.
1 comment thread