Remove locks from MIMETypeLibrary

This commit is contained in:
sabrina-shanman 2018-12-03 11:04:29 -08:00
parent bf1c5f2fa5
commit 6ec0e42ded
2 changed files with 42 additions and 53 deletions

View file

@ -12,74 +12,65 @@
#include "MIMETypeLibrary.h" #include "MIMETypeLibrary.h"
MIMETypeLibrary::ID MIMETypeLibrary::registerMIMEType(const MIMEType& mimeType) { MIMETypeLibrary::ID MIMETypeLibrary::registerMIMEType(const MIMEType& mimeType) {
ID id; ID id = nextID++;
withWriteLock([&](){ _mimeTypes.emplace_back(id, mimeType);
id = nextID++;
_mimeTypes.emplace_back(id, mimeType);
});
return id; return id;
} }
void MIMETypeLibrary::unregisterMIMEType(const MIMETypeLibrary::ID& id) { void MIMETypeLibrary::unregisterMIMEType(const MIMETypeLibrary::ID& id) {
withWriteLock([&](){ for (auto it = _mimeTypes.begin(); it != _mimeTypes.end(); it++) {
for (auto it = _mimeTypes.begin(); it != _mimeTypes.end(); it++) { if ((*it).id == id) {
if ((*it).id == id) { _mimeTypes.erase(it);
_mimeTypes.erase(it); break;
break;
}
} }
}); }
} }
MIMEType MIMETypeLibrary::getMIMEType(const MIMETypeLibrary::ID& id) const { MIMEType MIMETypeLibrary::getMIMEType(const MIMETypeLibrary::ID& id) const {
return resultWithReadLock<MIMEType>([&](){ for (auto& supportedFormat : _mimeTypes) {
for (auto& supportedFormat : _mimeTypes) { if (supportedFormat.id == id) {
if (supportedFormat.id == id) { return supportedFormat.mimeType;
return supportedFormat.mimeType;
}
} }
return MIMEType::NONE; }
}); return MIMEType::NONE;
} }
MIMETypeLibrary::ID MIMETypeLibrary::findMatchingMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { MIMETypeLibrary::ID MIMETypeLibrary::findMatchingMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const {
return resultWithReadLock<MIMETypeLibrary::ID>([&](){ // Check file contents
// Check file contents for (auto& mimeType : _mimeTypes) {
for (auto& mimeType : _mimeTypes) { for (auto& fileSignature : mimeType.mimeType.fileSignatures) {
for (auto& fileSignature : mimeType.mimeType.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 mimeType.id; }
}
}
// Check file extension
std::string urlString = url.path().toStdString();
std::size_t extensionSeparator = urlString.rfind('.');
if (extensionSeparator != std::string::npos) {
std::string detectedExtension = urlString.substr(extensionSeparator + 1);
for (auto& supportedFormat : _mimeTypes) {
for (auto& extension : supportedFormat.mimeType.extensions) {
if (extension == detectedExtension) {
return supportedFormat.id;
} }
} }
} }
}
// Check file extension // Check web media type
std::string urlString = url.path().toStdString(); if (webMediaType != "") {
std::size_t extensionSeparator = urlString.rfind('.'); for (auto& supportedFormat : _mimeTypes) {
if (extensionSeparator != std::string::npos) { for (auto& candidateWebMediaType : supportedFormat.mimeType.webMediaTypes) {
std::string detectedExtension = urlString.substr(extensionSeparator + 1); if (candidateWebMediaType == webMediaType) {
for (auto& supportedFormat : _mimeTypes) { return supportedFormat.id;
for (auto& extension : supportedFormat.mimeType.extensions) {
if (extension == detectedExtension) {
return supportedFormat.id;
}
} }
} }
} }
}
// Check web media type // Supported file type not found.
if (webMediaType != "") { return INVALID_ID;
for (auto& supportedFormat : _mimeTypes) {
for (auto& candidateWebMediaType : supportedFormat.mimeType.webMediaTypes) {
if (candidateWebMediaType == webMediaType) {
return supportedFormat.id;
}
}
}
}
// Supported file type not found.
return INVALID_ID;
});
} }

View file

@ -19,8 +19,6 @@
#include "HifiTypes.h" #include "HifiTypes.h"
#include "ReadWriteLockable.h"
// A short sequence of bytes, typically at the beginning of the file, which identifies the file format // A short sequence of bytes, typically at the beginning of the file, which identifies the file format
class FileSignature { class FileSignature {
public: public:
@ -61,7 +59,7 @@ public:
MIMEType MIMEType::NONE = MIMEType(""); MIMEType MIMEType::NONE = MIMEType("");
class MIMETypeLibrary : ReadWriteLockable { class MIMETypeLibrary {
public: public:
using ID = unsigned int; using ID = unsigned int;
static const ID INVALID_ID { 0 }; static const ID INVALID_ID { 0 };