chore(server): use native file-type implementation (#6686)

This commit is contained in:
Brooooooklyn 2024-04-29 04:46:26 +00:00
parent f98db24391
commit 81729703d9
No known key found for this signature in database
GPG Key ID: 30B1140CE1C07C99
10 changed files with 25 additions and 47 deletions

7
Cargo.lock generated
View File

@ -49,6 +49,7 @@ name = "affine_server_native"
version = "1.0.0"
dependencies = [
"chrono",
"file-format",
"napi",
"napi-build",
"napi-derive",
@ -434,6 +435,12 @@ version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984"
[[package]]
name = "file-format"
version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ba1b81b3c213cf1c071f8bf3b83531f310df99642e58c48247272eef006cae5"
[[package]]
name = "filetime"
version = "0.2.23"

View File

@ -8,6 +8,7 @@ crate-type = ["cdylib"]
[dependencies]
chrono = "0.4"
file-format = { version = "0.24", features = ["reader"] }
napi = { version = "2", default-features = false, features = [
"napi5",
"async",

View File

@ -1,6 +1,8 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export function getMime(input: Uint8Array): string
/**
* Merge updates in form like `Y.applyUpdate(doc, update)` way and return the
* result binary.

View File

@ -3,9 +3,9 @@ import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
/** @type {import('.')} */
const binding = require('./storage.node');
const binding = require('./server-native.node');
export const Storage = binding.Storage;
export const mergeUpdatesInApplyWay = binding.mergeUpdatesInApplyWay;
export const verifyChallengeResponse = binding.verifyChallengeResponse;
export const mintChallengeResponse = binding.mintChallengeResponse;
export const getMime = binding.getMime;

View File

@ -0,0 +1,8 @@
use napi_derive::napi;
#[napi]
pub fn get_mime(input: &[u8]) -> String {
file_format::FileFormat::from_bytes(input)
.media_type()
.to_string()
}

View File

@ -1,5 +1,6 @@
#![deny(clippy::all)]
pub mod file_type;
pub mod hashcash;
use std::fmt::{Debug, Display};

View File

@ -61,7 +61,6 @@
"dotenv": "^16.4.5",
"dotenv-cli": "^7.4.1",
"express": "^4.19.2",
"file-type": "^19.0.0",
"get-stream": "^9.0.1",
"graphql": "^16.8.1",
"graphql-scalars": "^1.23.0",

View File

@ -28,3 +28,5 @@ export const mintChallengeResponse = async (resource: string, bits: number) => {
if (!resource) return null;
return serverNativeModule.mintChallengeResponse(resource, bits);
};
export const getMime = serverNativeModule.getMime;

View File

@ -1,9 +1,9 @@
import { Readable } from 'node:stream';
import { crc32 } from '@node-rs/crc32';
import { fileTypeFromBuffer } from 'file-type';
import { getStreamAsBuffer } from 'get-stream';
import { getMime } from '../native';
import { BlobInputType, PutObjectMetadata } from './provider';
export async function toBuffer(input: BlobInputType): Promise<Buffer> {
@ -35,8 +35,7 @@ export async function autoMetadata(
// mime type
if (!metadata.contentType) {
try {
const typeResult = await fileTypeFromBuffer(blob);
metadata.contentType = typeResult?.mime ?? 'application/octet-stream';
metadata.contentType = getMime(blob);
} catch {
// ignore
}

View File

@ -724,7 +724,6 @@ __metadata:
dotenv: "npm:^16.4.5"
dotenv-cli: "npm:^7.4.1"
express: "npm:^4.19.2"
file-type: "npm:^19.0.0"
get-stream: "npm:^9.0.1"
graphql: "npm:^16.8.1"
graphql-scalars: "npm:^1.23.0"
@ -22536,17 +22535,6 @@ __metadata:
languageName: node
linkType: hard
"file-type@npm:^19.0.0":
version: 19.0.0
resolution: "file-type@npm:19.0.0"
dependencies:
readable-web-to-node-stream: "npm:^3.0.2"
strtok3: "npm:^7.0.0"
token-types: "npm:^5.0.1"
checksum: 10/8befa58f769b19d4a72c214694906b83b584310575300e63c08c48f9f2cfa6cb57fb4e1d08325961938d9dde3ecc4f5737b1604ddedfd759f5a1e65e5b0ca577
languageName: node
linkType: hard
"file-uri-to-path@npm:1.0.0":
version: 1.0.0
resolution: "file-uri-to-path@npm:1.0.0"
@ -32384,15 +32372,6 @@ __metadata:
languageName: node
linkType: hard
"readable-web-to-node-stream@npm:^3.0.2":
version: 3.0.2
resolution: "readable-web-to-node-stream@npm:3.0.2"
dependencies:
readable-stream: "npm:^3.6.0"
checksum: 10/d3a5bf9d707c01183d546a64864aa63df4d9cb835dfd2bf89ac8305e17389feef2170c4c14415a10d38f9b9bfddf829a57aaef7c53c8b40f11d499844bf8f1a4
languageName: node
linkType: hard
"readdirp@npm:~3.6.0":
version: 3.6.0
resolution: "readdirp@npm:3.6.0"
@ -34681,16 +34660,6 @@ __metadata:
languageName: node
linkType: hard
"strtok3@npm:^7.0.0":
version: 7.0.0
resolution: "strtok3@npm:7.0.0"
dependencies:
"@tokenizer/token": "npm:^0.3.0"
peek-readable: "npm:^5.0.0"
checksum: 10/4f2269679fcfce1e9fe0600eff361ea4c687ae0a0e8d9dab6703811071cd92545cbcb32d4ace3d3aa591f777cec1a3e8aeecd5efd71ae216fd2962a7a238b4ab
languageName: node
linkType: hard
"style-loader@npm:^4.0.0":
version: 4.0.0
resolution: "style-loader@npm:4.0.0"
@ -35327,16 +35296,6 @@ __metadata:
languageName: node
linkType: hard
"token-types@npm:^5.0.1":
version: 5.0.1
resolution: "token-types@npm:5.0.1"
dependencies:
"@tokenizer/token": "npm:^0.3.0"
ieee754: "npm:^1.2.1"
checksum: 10/0985369bbea9f53a5ccd79bb9899717b41401a813deb2c7fb1add5d0baf2f702aaf6da78f6e0ccf346d5a9f7acaa7cb5efed7d092d89d8c1e6962959e9509bc0
languageName: node
linkType: hard
"toml@npm:^3.0.0":
version: 3.0.0
resolution: "toml@npm:3.0.0"