Re-name MIMETypeLibrary to MediaTypeLibrary and update related references

This commit is contained in:
sabrina-shanman 2018-12-06 13:38:57 -08:00
parent ed1684967f
commit 6ea4769173
9 changed files with 97 additions and 97 deletions

View file

@ -1834,14 +1834,14 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr
return hfmModelPtr; return hfmModelPtr;
} }
MIMEType getFBXMIMEType() { MediaType getFBXMediaType() {
MIMEType mimeType("fbx"); MediaType mediaType("fbx");
mimeType.extensions.push_back("fbx"); mediaType.extensions.push_back("fbx");
mimeType.fileSignatures.emplace_back("Kaydara FBX Binary \x00", 0); mediaType.fileSignatures.emplace_back("Kaydara FBX Binary \x00", 0);
return mimeType; return mediaType;
} }
std::shared_ptr<hfm::Format> FBXSerializer::FORMAT = std::make_shared<hfm::SimpleFormat<FBXSerializer>>(getFBXMIMEType()); std::shared_ptr<hfm::Format> FBXSerializer::FORMAT = std::make_shared<hfm::SimpleFormat<FBXSerializer>>(getFBXMediaType());
HFMModel::Pointer FBXSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { HFMModel::Pointer FBXSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
QBuffer buffer(const_cast<QByteArray*>(&data)); QBuffer buffer(const_cast<QByteArray*>(&data));

View file

@ -906,14 +906,14 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const QUrl& url) {
return true; return true;
} }
MIMEType getGLTFMIMEType() { MediaType getGLTFMediaType() {
MIMEType mimeType("gltf"); MediaType mediaType("gltf");
mimeType.extensions.push_back("gltf"); mediaType.extensions.push_back("gltf");
mimeType.webMediaTypes.push_back("model/gltf+json"); mediaType.webMediaTypes.push_back("model/gltf+json");
return mimeType; return mediaType;
} }
std::shared_ptr<hfm::Format> GLTFSerializer::FORMAT = std::make_shared<hfm::SimpleFormat<GLTFSerializer>>(getGLTFMIMEType()); std::shared_ptr<hfm::Format> GLTFSerializer::FORMAT = std::make_shared<hfm::SimpleFormat<GLTFSerializer>>(getGLTFMediaType());
HFMModel::Pointer GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { HFMModel::Pointer GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {

View file

@ -652,13 +652,13 @@ done:
return result; return result;
} }
MIMEType getOBJMIMEType() { MediaType getOBJMediaType() {
MIMEType mimeType("obj"); MediaType mediaType("obj");
mimeType.extensions.push_back("obj"); mediaType.extensions.push_back("obj");
return mimeType; return mediaType;
} }
std::shared_ptr<hfm::Format> OBJSerializer::FORMAT = std::make_shared<hfm::SimpleFormat<OBJSerializer>>(getOBJMIMEType()); std::shared_ptr<hfm::Format> OBJSerializer::FORMAT = std::make_shared<hfm::SimpleFormat<OBJSerializer>>(getOBJMediaType());
HFMModel::Pointer OBJSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { HFMModel::Pointer OBJSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
PROFILE_RANGE_EX(resource_parse, __FUNCTION__, 0xffff0000, nullptr); PROFILE_RANGE_EX(resource_parse, __FUNCTION__, 0xffff0000, nullptr);

View file

@ -13,53 +13,53 @@
namespace hfm { namespace hfm {
FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, std::unique_ptr<Serializer::Factory> supportedFactory) { FormatRegistry::MediaTypeID FormatRegistry::registerMediaType(const MediaType& mediaType, std::unique_ptr<Serializer::Factory> supportedFactory) {
std::lock_guard<std::mutex> lock(_libraryLock); std::lock_guard<std::mutex> lock(_libraryLock);
MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType); MediaTypeID id = _mediaTypeLibrary.registerMediaType(mediaType);
_supportedFormats.emplace_back(id, supportedFactory); _supportedFormats.emplace_back(id, supportedFactory);
return id; return id;
} }
void FormatRegistry::unregisterMIMEType(const MIMETypeID& mimeTypeID) { void FormatRegistry::unregisterMediaType(const MediaTypeID& mediaTypeID) {
std::lock_guard<std::mutex> lock(_libraryLock); std::lock_guard<std::mutex> lock(_libraryLock);
for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
if ((*it).mimeTypeID == mimeTypeID) { if ((*it).mediaTypeID == mediaTypeID) {
_supportedFormats.erase(it); _supportedFormats.erase(it);
break; break;
} }
} }
_mimeTypeLibrary.unregisterMIMEType(mimeTypeID); _mediaTypeLibrary.unregisterMediaType(mediaTypeID);
} }
std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const { std::shared_ptr<Serializer> FormatRegistry::getSerializerForMediaTypeID(FormatRegistry::MediaTypeID mediaTypeID) const {
// TODO: shared_lock in C++14 // TODO: shared_lock in C++14
std::lock_guard<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock)); std::lock_guard<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock));
for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
if ((*it).mimeTypeID == mimeTypeID) { if ((*it).mediaTypeID == mediaTypeID) {
return (*it).factory->get(); return (*it).factory->get();
} }
} }
return std::shared_ptr<Serializer>(); return std::shared_ptr<Serializer>();
} }
std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMEType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const { std::shared_ptr<Serializer> FormatRegistry::getSerializerForMediaType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const {
MIMETypeID id; MediaTypeID id;
{ {
// TODO: shared_lock in C++14 // TODO: shared_lock in C++14
std::lock_guard<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock)); std::lock_guard<std::mutex> lock(*const_cast<std::mutex*>(&_libraryLock));
id = _mimeTypeLibrary.findMIMETypeForData(data); id = _mediaTypeLibrary.findMediaTypeForData(data);
if (id == INVALID_MIME_TYPE_ID) { if (id == INVALID_MEDIA_TYPE_ID) {
id = _mimeTypeLibrary.findMIMETypeForURL(url); id = _mediaTypeLibrary.findMediaTypeForURL(url);
} }
if (id == INVALID_MIME_TYPE_ID) { if (id == INVALID_MEDIA_TYPE_ID) {
id = _mimeTypeLibrary.findMIMETypeForMediaType(webMediaType); id = _mediaTypeLibrary.findMediaTypeForWebID(webMediaType);
} }
} }
return getSerializerForMIMETypeID(id); return getSerializerForMediaTypeID(id);
} }
}; };

View file

@ -13,32 +13,32 @@
#define hifi_HFMFormatRegistry_h #define hifi_HFMFormatRegistry_h
#include "HFMSerializer.h" #include "HFMSerializer.h"
#include <shared/MIMETypeLibrary.h> #include <shared/MediaTypeLibrary.h>
#include <shared/ReadWriteLockable.h> #include <shared/ReadWriteLockable.h>
namespace hfm { namespace hfm {
class FormatRegistry { class FormatRegistry {
public: public:
using MIMETypeID = MIMETypeLibrary::ID; using MediaTypeID = MediaTypeLibrary::ID;
static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID }; static const MediaTypeID INVALID_MEDIA_TYPE_ID { MediaTypeLibrary::INVALID_ID };
MIMETypeID registerMIMEType(const MIMEType& mimeType, std::unique_ptr<Serializer::Factory> supportedFactory); MediaTypeID registerMediaType(const MediaType& mediaType, std::unique_ptr<Serializer::Factory> supportedFactory);
void unregisterMIMEType(const MIMETypeID& id); void unregisterMediaType(const MediaTypeID& id);
std::shared_ptr<Serializer> getSerializerForMIMEType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const; std::shared_ptr<Serializer> getSerializerForMediaType(const hifi::ByteArray& data, const hifi::URL& url, const std::string& webMediaType) const;
std::shared_ptr<Serializer> getSerializerForMIMETypeID(MIMETypeID id) const; std::shared_ptr<Serializer> getSerializerForMediaTypeID(MediaTypeID id) const;
protected: protected:
MIMETypeLibrary _mimeTypeLibrary; MediaTypeLibrary _mediaTypeLibrary;
std::mutex _libraryLock; std::mutex _libraryLock;
class SupportedFormat { class SupportedFormat {
public: public:
SupportedFormat(const MIMETypeID& mimeTypeID, std::unique_ptr<Serializer::Factory>& factory) : SupportedFormat(const MediaTypeID& mediaTypeID, std::unique_ptr<Serializer::Factory>& factory) :
mimeTypeID(mimeTypeID), mediaTypeID(mediaTypeID),
factory(std::move(factory)) { factory(std::move(factory)) {
} }
MIMETypeID mimeTypeID; MediaTypeID mediaTypeID;
std::unique_ptr<Serializer::Factory> factory; std::unique_ptr<Serializer::Factory> factory;
}; };
std::vector<SupportedFormat> _supportedFormats; std::vector<SupportedFormat> _supportedFormats;

View file

@ -26,21 +26,21 @@ namespace hfm {
template<typename T> // T is an implementation of hfm::Serializer template<typename T> // T is an implementation of hfm::Serializer
class SimpleFormat : public Format { class SimpleFormat : public Format {
public: public:
SimpleFormat(const MIMEType& mimeType) : Format(), SimpleFormat(const MediaType& mediaType) : Format(),
_mimeType(mimeType) { _mediaType(mediaType) {
} }
void registerFormat(FormatRegistry& registry) override { void registerFormat(FormatRegistry& registry) override {
_mimeTypeID = registry.registerMIMEType(_mimeType, std::make_unique<SimpleFactory<T>>()); _mediaTypeID = registry.registerMediaType(_mediaType, std::make_unique<SimpleFactory<T>>());
} }
void unregisterFormat(FormatRegistry& registry) override { void unregisterFormat(FormatRegistry& registry) override {
registry.unregisterMIMEType(_mimeTypeID); registry.unregisterMediaType(_mediaTypeID);
_mimeTypeID = hfm::FormatRegistry::INVALID_MIME_TYPE_ID; _mediaTypeID = hfm::FormatRegistry::INVALID_MEDIA_TYPE_ID;
} }
protected: protected:
MIMEType _mimeType; MediaType _mediaType;
hfm::FormatRegistry::MIMETypeID _mimeTypeID; hfm::FormatRegistry::MediaTypeID _mediaTypeID;
}; };
}; };

View file

@ -16,7 +16,7 @@
hfm::Model::Pointer ModelLoader::load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { hfm::Model::Pointer ModelLoader::load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const {
auto serializer = DependencyManager::get<ModelFormatRegistry>()->getSerializerForMIMEType(data, url, webMediaType); auto serializer = DependencyManager::get<ModelFormatRegistry>()->getSerializerForMediaType(data, url, webMediaType);
if (!serializer) { if (!serializer) {
return hfm::Model::Pointer(); return hfm::Model::Pointer();
} }

View file

@ -1,5 +1,5 @@
// //
// MIMETypeLibrary.cpp // MediaTypeLibrary.cpp
// libraries/shared/src/shared // libraries/shared/src/shared
// //
// Created by Sabrina Shanman on 2018/11/29. // Created by Sabrina Shanman on 2018/11/29.
@ -9,41 +9,41 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
#include "MIMETypeLibrary.h" #include "MediaTypeLibrary.h"
MIMEType MIMEType::NONE = MIMEType(""); MediaType MediaType::NONE = MediaType("");
MIMETypeLibrary::ID MIMETypeLibrary::registerMIMEType(const MIMEType& mimeType) { MediaTypeLibrary::ID MediaTypeLibrary::registerMediaType(const MediaType& mediaType) {
ID id = nextID++; ID id = nextID++;
_mimeTypes.emplace_back(id, mimeType); _mediaTypes.emplace_back(id, mediaType);
return id; return id;
} }
void MIMETypeLibrary::unregisterMIMEType(const MIMETypeLibrary::ID& id) { void MediaTypeLibrary::unregisterMediaType(const MediaTypeLibrary::ID& id) {
for (auto it = _mimeTypes.begin(); it != _mimeTypes.end(); it++) { for (auto it = _mediaTypes.begin(); it != _mediaTypes.end(); it++) {
if ((*it).id == id) { if ((*it).id == id) {
_mimeTypes.erase(it); _mediaTypes.erase(it);
break; break;
} }
} }
} }
MIMEType MIMETypeLibrary::getMIMEType(const MIMETypeLibrary::ID& id) const { MediaType MediaTypeLibrary::getMediaType(const MediaTypeLibrary::ID& id) const {
for (auto& supportedFormat : _mimeTypes) { for (auto& supportedFormat : _mediaTypes) {
if (supportedFormat.id == id) { if (supportedFormat.id == id) {
return supportedFormat.mimeType; return supportedFormat.mediaType;
} }
} }
return MIMEType::NONE; return MediaType::NONE;
} }
MIMETypeLibrary::ID MIMETypeLibrary::findMIMETypeForData(const hifi::ByteArray& data) const { MediaTypeLibrary::ID MediaTypeLibrary::findMediaTypeForData(const hifi::ByteArray& data) const {
// Check file contents // Check file contents
for (auto& mimeType : _mimeTypes) { for (auto& mediaType : _mediaTypes) {
for (auto& fileSignature : mimeType.mimeType.fileSignatures) { for (auto& fileSignature : mediaType.mediaType.fileSignatures) {
auto testBytes = data.mid(fileSignature.byteOffset, (int)fileSignature.bytes.size()).toStdString(); auto testBytes = data.mid(fileSignature.byteOffset, (int)fileSignature.bytes.size()).toStdString();
if (testBytes == fileSignature.bytes) { if (testBytes == fileSignature.bytes) {
return mimeType.id; return mediaType.id;
} }
} }
} }
@ -51,14 +51,14 @@ MIMETypeLibrary::ID MIMETypeLibrary::findMIMETypeForData(const hifi::ByteArray&
return INVALID_ID; return INVALID_ID;
} }
MIMETypeLibrary::ID MIMETypeLibrary::findMIMETypeForURL(const hifi::URL& url) const { MediaTypeLibrary::ID MediaTypeLibrary::findMediaTypeForURL(const hifi::URL& url) const {
// Check file extension // Check file extension
std::string urlString = url.path().toStdString(); std::string urlString = url.path().toStdString();
std::size_t extensionSeparator = urlString.rfind('.'); std::size_t extensionSeparator = urlString.rfind('.');
if (extensionSeparator != std::string::npos) { if (extensionSeparator != std::string::npos) {
std::string detectedExtension = urlString.substr(extensionSeparator + 1); std::string detectedExtension = urlString.substr(extensionSeparator + 1);
for (auto& supportedFormat : _mimeTypes) { for (auto& supportedFormat : _mediaTypes) {
for (auto& extension : supportedFormat.mimeType.extensions) { for (auto& extension : supportedFormat.mediaType.extensions) {
if (extension == detectedExtension) { if (extension == detectedExtension) {
return supportedFormat.id; return supportedFormat.id;
} }
@ -69,11 +69,11 @@ MIMETypeLibrary::ID MIMETypeLibrary::findMIMETypeForURL(const hifi::URL& url) co
return INVALID_ID; return INVALID_ID;
} }
MIMETypeLibrary::ID MIMETypeLibrary::findMIMETypeForMediaType(const std::string& webMediaType) const { MediaTypeLibrary::ID MediaTypeLibrary::findMediaTypeForWebID(const std::string& webMediaType) const {
// Check web media type // Check web media type
if (webMediaType != "") { if (webMediaType != "") {
for (auto& supportedFormat : _mimeTypes) { for (auto& supportedFormat : _mediaTypes) {
for (auto& candidateWebMediaType : supportedFormat.mimeType.webMediaTypes) { for (auto& candidateWebMediaType : supportedFormat.mediaType.webMediaTypes) {
if (candidateWebMediaType == webMediaType) { if (candidateWebMediaType == webMediaType) {
return supportedFormat.id; return supportedFormat.id;
} }

View file

@ -1,5 +1,5 @@
// //
// MIMETypeLibrary.h // MediaTypeLibrary.h
// libraries/shared/src/shared // libraries/shared/src/shared
// //
// Created by Sabrina Shanman on 2018/11/28. // Created by Sabrina Shanman on 2018/11/28.
@ -9,8 +9,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
#ifndef hifi_MIMETypeLibrary_h #ifndef hifi_MediaTypeLibrary_h
#define hifi_MIMETypeLibrary_h #define hifi_MediaTypeLibrary_h
#include <vector> #include <vector>
#include <string> #include <string>
@ -36,20 +36,20 @@ public:
}; };
// A named file extension with a list of known ways to positively identify the file type // A named file extension with a list of known ways to positively identify the file type
class MIMEType { class MediaType {
public: public:
MIMEType(const std::string& name) : MediaType(const std::string& name) :
name(name) { name(name) {
} }
MIMEType() {}; MediaType() {};
MIMEType(const MIMEType& mimeType) : MediaType(const MediaType& mediaType) :
name(mimeType.name), name(mediaType.name),
extensions(mimeType.extensions), extensions(mediaType.extensions),
webMediaTypes(mimeType.webMediaTypes), webMediaTypes(mediaType.webMediaTypes),
fileSignatures(mimeType.fileSignatures) { fileSignatures(mediaType.fileSignatures) {
} }
static MIMEType NONE; static MediaType NONE;
std::string name; std::string name;
std::vector<std::string> extensions; std::vector<std::string> extensions;
@ -57,34 +57,34 @@ public:
std::vector<FileSignature> fileSignatures; std::vector<FileSignature> fileSignatures;
}; };
class MIMETypeLibrary { class MediaTypeLibrary {
public: public:
using ID = unsigned int; using ID = unsigned int;
static const ID INVALID_ID { 0 }; static const ID INVALID_ID { 0 };
ID registerMIMEType(const MIMEType& mimeType); ID registerMediaType(const MediaType& mediaType);
void unregisterMIMEType(const ID& id); void unregisterMediaType(const ID& id);
MIMEType getMIMEType(const ID& id) const; MediaType getMediaType(const ID& id) const;
ID findMIMETypeForData(const hifi::ByteArray& data) const; ID findMediaTypeForData(const hifi::ByteArray& data) const;
ID findMIMETypeForURL(const hifi::URL& url) const; ID findMediaTypeForURL(const hifi::URL& url) const;
ID findMIMETypeForMediaType(const std::string& webMediaType) const; ID findMediaTypeForWebID(const std::string& webMediaType) const;
protected: protected:
ID nextID { 1 }; ID nextID { 1 };
class Entry { class Entry {
public: public:
Entry(const ID& id, const MIMEType& mimeType) : Entry(const ID& id, const MediaType& mediaType) :
id(id), id(id),
mimeType(mimeType) { mediaType(mediaType) {
} }
ID id; ID id;
MIMEType mimeType; MediaType mediaType;
}; };
std::vector<Entry> _mimeTypes; std::vector<Entry> _mediaTypes;
}; };
#endif // hifi_MIMETypeLibrary_h #endif // hifi_MeidaTypeLibrary_h