diff --git a/wpiutil/src/main/native/cpp/MimeTypes.cpp b/wpiutil/src/main/native/cpp/MimeTypes.cpp new file mode 100644 index 0000000000..5bf9d29a22 --- /dev/null +++ b/wpiutil/src/main/native/cpp/MimeTypes.cpp @@ -0,0 +1,71 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2020 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "wpi/MimeTypes.h" + +#include "wpi/StringMap.h" + +namespace wpi { + +// derived partially from +// https://github.com/DEGoodmanWilson/libmime/blob/stable/0.1.2/mime/mime.cpp +StringRef MimeTypeFromPath(StringRef path) { + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types + static StringMap mimeTypes{ + // text + {"css", "text/css"}, + {"csv", "text/csv"}, + {"htm", "text/html"}, + {"html", "text/html"}, + {"js", "text/javascript"}, + {"json", "application/json"}, + {"map", "application/json"}, + {"md", "text/markdown"}, + {"txt", "text/plain"}, + {"xml", "text/xml"}, + + // images + {"apng", "image/apng"}, + {"bmp", "image/bmp"}, + {"gif", "image/gif"}, + {"cur", "image/x-icon"}, + {"ico", "image/x-icon"}, + {"jpg", "image/jpeg"}, + {"jpeg", "image/jpeg"}, + {"png", "image/png"}, + {"svg", "image/svg+xml"}, + {"tif", "image/tiff"}, + {"tiff", "image/tiff"}, + {"webp", "image/webp"}, + + // fonts + {"otf", "font/otf"}, + {"ttf", "font/ttf"}, + {"woff", "font/woff"}, + + // misc + {"pdf", "application/pdf"}, + {"zip", "application/zip"}, + }; + + static const char* defaultType = "application/octet-stream"; + + auto pos = path.find_last_of("/"); + if (pos != StringRef::npos) { + path = path.substr(pos + 1); + } + auto dot_pos = path.find_last_of("."); + if (dot_pos > 0 && dot_pos != StringRef::npos) { + auto type = mimeTypes.find(path.substr(dot_pos + 1)); + if (type != mimeTypes.end()) { + return type->getValue(); + } + } + return defaultType; +} + +} // namespace wpi diff --git a/wpiutil/src/main/native/include/wpi/MimeTypes.h b/wpiutil/src/main/native/include/wpi/MimeTypes.h new file mode 100644 index 0000000000..b3dc17b873 --- /dev/null +++ b/wpiutil/src/main/native/include/wpi/MimeTypes.h @@ -0,0 +1,19 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2020 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#ifndef WPIUTIL_WPI_MIMETYPES_H_ +#define WPIUTIL_WPI_MIMETYPES_H_ + +#include "wpi/StringRef.h" + +namespace wpi { + +StringRef MimeTypeFromPath(StringRef path); + +} // namespace wpi + +#endif // WPIUTIL_WPI_MIMETYPES_H_