アップロードされたファイルを有効にする方法


このノートでは、ノードのJSでのファイルの検証と圧縮をどのように処理するかを調べます.
妥当性検査や圧縮処理の処理方法が良い場合は、コメント欄にドロップしてください.
ほとんどの場合、ファイルはMulter、Busboy、またはForidableのいずれかを使用してノードJSサーバーで解析されます.
このWriteUpで使用されるコンテンツは、マルチを使用して、それは簡単に任意のシステムに適用することができます.
ファイル確認
ノードJSのファイルは通常JSON形式です.ファイルのフォーマットは以下の2つのうちの1つです.
// If memory storage is used
{
  fieldname: 'image',
  originalname: 'image.png',
  encoding: '7bit',
  mimetype: 'image/png',
  buffer: <Buffer bytes>,
  size: 25471
}

// If the file is stored locally
{
  fieldname: 'image',
  originalname: 'Meta1.png',
  encoding: '7bit',
  mimetype: 'image/png',
  destination: 'uploads/',
  filename: 'ed84692635f46d86c4be044f4acca667',
  path: 'uploads/ed84692635f46d86c4be044f4acca667',
  size: 25471
}
検証に使用するフィールドは、元の名前、MIME型、およびサイズフィールドです.

ファイル拡張子の確認


いくつかの作り付けのJS関数を組み合わせたビット単位の右シフト演算子を使用してファイル拡張子を取得します.
const file_extension = image.originalname.slice(
    ((image.originalname.lastIndexOf('.') - 1) >>> 0) + 2
);
上記のメソッドは、misspeltファイル名、すなわちイメージを含むケースの98 %のために働くことが証明されました.PNG写真、写真.JPEG .日本電子
ファイル拡張子があるので、有効かどうかを確認できます.
// Array of allowed files
const array_of_allowed_files = ['png', 'jpeg', 'jpg', 'gif'];

// Get the extension of the uploaded file
const file_extension = image.originalname.slice(
    ((image.originalname.lastIndexOf('.') - 1) >>> 0) + 2
);

// Check if the uploaded file is allowed
if (!array_of_allowed_files.includes(file_extension)) {
  throw Error('Invalid file');
}
誰でもファイル名を編集して、拡張を変えることができるので、ファイル拡張子だけをチェックすることは実用的でありません.
このため、ファイルのMIMEタイプを確認してイメージを確認する必要があります.これを行うには、同様のアプローチに従います.
const array_of_allowed_file_types = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
if (!array_of_allowed_file_types.includes(image.memetype)) {
  throw Error('Invalid file');
}
二つのチェックを組み合わせると、
// Array of allowed files
const array_of_allowed_files = ['png', 'jpeg', 'jpg', 'gif'];
const array_of_allowed_file_types = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];

// Get the extension of the uploaded file
const file_extension = image.originalname.slice(
    ((image.originalname.lastIndexOf('.') - 1) >>> 0) + 2
);

// Check if the uploaded file is allowed
if (!array_of_allowed_files.includes(file_extension) || !array_of_allowed_file_types.includes(image.memetype)) {
  throw Error('Invalid file');
}

ファイルサイズのチェック


ファイルサイズをチェックするには、Sizeフィールドを使用します.サイズは通常バイトで与えられるので、私たちは評価のためにそれを所望のフォーマットに変えなければなりません.この場合、MBに変換しました.
// Allowed file size in mb
const allowed_file_size = 2;
if ((image.size / (1024 * 1024)) > allowed_file_size) {                  
  throw Error('File too large');
}
上記の検証を一緒にすると、アップロードされたファイルを検証するExpressの典型的なミドルウェアは以下のコードのようになります
export const auth = (req, res, next) => {
    const image = req.file;
    // Array of allowed files
    const array_of_allowed_files = ['png', 'jpeg', 'jpg', 'gif'];
    const array_of_allowed_file_types = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
    // Allowed file size in mb
    const allowed_file_size = 2;
    // Get the extension of the uploaded file
    const file_extension = image.originalname.slice(
        ((image.originalname.lastIndexOf('.') - 1) >>> 0) + 2
    );

    // Check if the uploaded file is allowed
    if (!array_of_allowed_files.includes(file_extension) || !array_of_allowed_file_types.includes(image.memetype)) {
        throw Error('Invalid file');
    }

    if ((image.size / (1024 * 1024)) > allowed_file_size) {                  
       throw Error('File too large');
    }
    return next();
}

結論


ファイルの検証は非常に重要です.このライトアップは、画像を使用して、単一のファイルをアップロードするが、それは簡単に他のファイルの種類のために動作するように変更することができます.ループ内で追加すると、同様にファイルの配列を検証できます.
コードを簡単に統合することができますNPMパッケージにバンドルされています.それを見つけるために、リンクに続いてください.Fileguard .