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
10 changed files with 25 additions and 47 deletions

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
}