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"
MIMETypeLibrary::ID MIMETypeLibrary::registerMIMEType(const MIMEType& mimeType) {
ID id;
withWriteLock([&](){
id = nextID++;
_mimeTypes.emplace_back(id, mimeType);
});
ID id = nextID++;
_mimeTypes.emplace_back(id, mimeType);
return id;
}
void MIMETypeLibrary::unregisterMIMEType(const MIMETypeLibrary::ID& id) {
withWriteLock([&](){
for (auto it = _mimeTypes.begin(); it != _mimeTypes.end(); it++) {
if ((*it).id == id) {
_mimeTypes.erase(it);
break;
}
for (auto it = _mimeTypes.begin(); it != _mimeTypes.end(); it++) {
if ((*it).id == id) {
_mimeTypes.erase(it);
break;
}
});
}
}
MIMEType MIMETypeLibrary::getMIMEType(const MIMETypeLibrary::ID& id) const {
return resultWithReadLock<MIMEType>([&](){
for (auto& supportedFormat : _mimeTypes) {
if (supportedFormat.id == id) {
return supportedFormat.mimeType;
}
for (auto& supportedFormat : _mimeTypes) {
if (supportedFormat.id == id) {
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 {
return resultWithReadLock<MIMETypeLibrary::ID>([&](){
// Check file contents
for (auto& mimeType : _mimeTypes) {
for (auto& fileSignature : mimeType.mimeType.fileSignatures) {
auto testBytes = data.mid(fileSignature.byteOffset, (int)fileSignature.bytes.size()).toStdString();
if (testBytes == fileSignature.bytes) {
return mimeType.id;
// Check file contents
for (auto& mimeType : _mimeTypes) {
for (auto& fileSignature : mimeType.mimeType.fileSignatures) {
auto testBytes = data.mid(fileSignature.byteOffset, (int)fileSignature.bytes.size()).toStdString();
if (testBytes == fileSignature.bytes) {
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
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 web media type
if (webMediaType != "") {
for (auto& supportedFormat : _mimeTypes) {
for (auto& candidateWebMediaType : supportedFormat.mimeType.webMediaTypes) {
if (candidateWebMediaType == webMediaType) {
return supportedFormat.id;
}
}
}
}
// Check web media type
if (webMediaType != "") {
for (auto& supportedFormat : _mimeTypes) {
for (auto& candidateWebMediaType : supportedFormat.mimeType.webMediaTypes) {
if (candidateWebMediaType == webMediaType) {
return supportedFormat.id;
}
}
}
}
// Supported file type not found.
return INVALID_ID;
});
// Supported file type not found.
return INVALID_ID;
}

View file

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