21 lines
1.3 KiB
C#
21 lines
1.3 KiB
C#
|
|
namespace WordVersioning.Api.Services;
|
||
|
|
|
||
|
|
/// <summary>ตรวจไฟล์ .docx ที่รับเข้ามาทาง multipart — ใช้ร่วมทั้ง upload version และ template</summary>
|
||
|
|
public static class DocxUploadValidation
|
||
|
|
{
|
||
|
|
public const string DocxContentType =
|
||
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||
|
|
|
||
|
|
public static bool IsValidDocx(IFormFile file) =>
|
||
|
|
file.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase)
|
||
|
|
&& IsAcceptableContentType(file.ContentType);
|
||
|
|
|
||
|
|
// spec: reject content-type ที่ไม่ใช่ docx — ยอมรับ octet-stream/zip ด้วยเพราะ client บางตัว
|
||
|
|
// (เช่น curl -F) ไม่ set ชนิดไฟล์ให้ ส่วนชื่อไฟล์ต้องลง .docx เสมอ (เช็คข้างบน)
|
||
|
|
public static bool IsAcceptableContentType(string contentType) =>
|
||
|
|
string.IsNullOrWhiteSpace(contentType)
|
||
|
|
|| contentType.Equals(DocxContentType, StringComparison.OrdinalIgnoreCase)
|
||
|
|
|| contentType.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase)
|
||
|
|
|| contentType.Equals("application/zip", StringComparison.OrdinalIgnoreCase);
|
||
|
|
}
|