1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-19 05:19:54 +00:00
transmission/libtransmission/mime-types.js
Charles Kerr f59118d1fe
feat: add torrent-get 'primary-mime-type' to RPC. (#1464)
* feat: add torrent-get 'primary-mime-type' to RPC.

This is a cheap way for RPC clients to know what type of content is in a
torrent. This info can be used to display the torrent, e.g. by using an
icon that corresponds to the mime type.

* use size_t for content byte count

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>

* explicit boolean expressions

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>

* use uint64_t for content byte counts

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>

* avoid unnecessary logic branches

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>

* explicit cast

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>

* refactor: add an autogenerated mime-type.h header

* chore: maybe fix the win32 FTBFS

* chore: add mime-types.[ch] to xcode

* Squashed commit of the following:

commit 4c7153fa48
Author: Mike Gelfand <mikedld@users.noreply.github.com>
Date:   Tue Oct 13 03:15:19 2020 +0300

    Remove autotools-based build system (#1465)

    * Support .git files (e.g. for worktrees, submodules)
    * Fix symlinks in source tarball, switch to TXZ, adjust non-release name
    * Remove autotools stuff

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2020-10-13 10:33:56 -05:00

72 lines
2 KiB
JavaScript
Executable file

#!/usr/bin/env node
const copyright =
`/*
* This file Copyright (C) ${new Date().getFullYear()} Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*/`;
const fs = require('fs');
const https = require('https');
// https://github.com/jshttp/mime-db
// > If you're crazy enough to use this in the browser, you can just grab
// > the JSON file using jsDelivr. It is recommended to replace master with
// > a release tag as the JSON format may change in the future.
//
// This script keeps it at `master` to pick up any fixes that may have landed.
// If the format changes, we'll just update this script.
const url = 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json';
https.get(url, (res) => {
res.setEncoding('utf8');
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
try {
const suffixes = [];
const mime_types = JSON.parse(chunks.join(''));
for (const [mime_type, info] of Object.entries(mime_types)) {
for (const suffix of info?.extensions || []) {
suffixes.push([ suffix, mime_type ]);
}
}
const max_suffix_len = suffixes
.reduce((acc, [suffix]) => Math.max(acc, suffix.length), 0);
const mime_type_lines = suffixes
.map(([suffix, mime_type]) => ` { "${suffix}", "${mime_type}" }`)
.sort()
.join(',\n');
fs.writeFileSync('mime-types.c', `${copyright}
#include "mime-types.h"
struct mime_type_suffix const mime_type_suffixes[MIME_TYPE_SUFFIX_COUNT] =
{
${mime_type_lines}
};
`);
fs.writeFileSync('mime-types.h', `${copyright}
#pragma once
#define MIME_TYPE_SUFFIX_MAXLEN ${max_suffix_len}
#define MIME_TYPE_SUFFIX_COUNT ${suffixes.length}
struct mime_type_suffix
{
char const* suffix;
char const* mime_type;
};
extern struct mime_type_suffix const mime_type_suffixes[MIME_TYPE_SUFFIX_COUNT];
`);
} catch (e) {
console.error(e.message);
}
});
});