add server handling for no extension requests

This commit is contained in:
Stephen Birarda 2016-03-07 12:15:22 -08:00
parent 267ed3af82
commit e7124d4b10
2 changed files with 10 additions and 35 deletions

View file

@ -130,34 +130,15 @@ void AssetServer::completeSetup() {
} }
} }
qDebug() << "Serving files from: " << _resourcesDirectory.path(); qInfo() << "Serving files from: " << _resourcesDirectory.path();
// Scan for new files // Check the asset directory to output some information about what we have
qDebug() << "Looking for new files in asset directory"; auto files = _resourcesDirectory.entryList(QDir::Files);
auto files = _resourcesDirectory.entryInfoList(QDir::Files);
QRegExp filenameRegex { "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}(\\..+)?$" };
for (const auto& fileInfo : files) {
auto filename = fileInfo.fileName();
if (!filenameRegex.exactMatch(filename)) {
qDebug() << "Found file: " << filename;
if (!fileInfo.isReadable()) {
qDebug() << "\tCan't open file for reading: " << filename;
continue;
}
// Read file QRegExp hashFileRegex { "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}$" };
QFile file { fileInfo.absoluteFilePath() }; auto hashedFiles = files.filter(hashFileRegex);
file.open(QFile::ReadOnly);
QByteArray data = file.readAll();
auto hash = hashData(data); qInfo() << "There are" << hashedFiles.size() << "asset files in the asset directory.";
auto hexHash = hash.toHex();
qDebug() << "\tMoving " << filename << " to " << hexHash;
file.rename(_resourcesDirectory.absoluteFilePath(hexHash) + "." + fileInfo.suffix());
}
}
nodeList->addNodeTypeToInterestSet(NodeType::Agent); nodeList->addNodeTypeToInterestSet(NodeType::Agent);
} }
@ -216,17 +197,14 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer<ReceivedMessage> me
void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) { void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
QByteArray assetHash; QByteArray assetHash;
MessageID messageID; MessageID messageID;
uint8_t extensionLength;
if (message->getSize() < qint64(SHA256_HASH_LENGTH + sizeof(messageID) + sizeof(extensionLength))) { if (message->getSize() < qint64(SHA256_HASH_LENGTH + sizeof(messageID))) {
qDebug() << "ERROR bad file request"; qDebug() << "ERROR bad file request";
return; return;
} }
message->readPrimitive(&messageID); message->readPrimitive(&messageID);
assetHash = message->readWithoutCopy(SHA256_HASH_LENGTH); assetHash = message->readWithoutCopy(SHA256_HASH_LENGTH);
message->readPrimitive(&extensionLength);
QByteArray extension = message->read(extensionLength);
auto replyPacket = NLPacket::create(PacketType::AssetGetInfoReply); auto replyPacket = NLPacket::create(PacketType::AssetGetInfoReply);
@ -235,7 +213,7 @@ void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, Sh
replyPacket->writePrimitive(messageID); replyPacket->writePrimitive(messageID);
replyPacket->write(assetHash); replyPacket->write(assetHash);
QString fileName = QString(hexHash) + "." + extension; QString fileName = QString(hexHash);
QFileInfo fileInfo { _resourcesDirectory.filePath(fileName) }; QFileInfo fileInfo { _resourcesDirectory.filePath(fileName) };
if (fileInfo.exists() && fileInfo.isReadable()) { if (fileInfo.exists() && fileInfo.isReadable()) {
@ -253,7 +231,7 @@ void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, Sh
void AssetServer::handleAssetGet(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) { void AssetServer::handleAssetGet(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode) {
auto minSize = qint64(sizeof(MessageID) + SHA256_HASH_LENGTH + sizeof(uint8_t) + sizeof(DataOffset) + sizeof(DataOffset)); auto minSize = qint64(sizeof(MessageID) + SHA256_HASH_LENGTH + sizeof(DataOffset) + sizeof(DataOffset));
if (message->getSize() < minSize) { if (message->getSize() < minSize) {
qDebug() << "ERROR bad file request"; qDebug() << "ERROR bad file request";

View file

@ -33,13 +33,10 @@ SendAssetTask::SendAssetTask(QSharedPointer<ReceivedMessage> message, const Shar
void SendAssetTask::run() { void SendAssetTask::run() {
MessageID messageID; MessageID messageID;
uint8_t extensionLength;
DataOffset start, end; DataOffset start, end;
_message->readPrimitive(&messageID); _message->readPrimitive(&messageID);
QByteArray assetHash = _message->read(SHA256_HASH_LENGTH); QByteArray assetHash = _message->read(SHA256_HASH_LENGTH);
_message->readPrimitive(&extensionLength);
QByteArray extension = _message->read(extensionLength);
// `start` and `end` indicate the range of data to retrieve for the asset identified by `assetHash`. // `start` and `end` indicate the range of data to retrieve for the asset identified by `assetHash`.
// `start` is inclusive, `end` is exclusive. Requesting `start` = 1, `end` = 10 will retrieve 9 bytes of data, // `start` is inclusive, `end` is exclusive. Requesting `start` = 1, `end` = 10 will retrieve 9 bytes of data,
@ -61,7 +58,7 @@ void SendAssetTask::run() {
if (end <= start) { if (end <= start) {
writeError(replyPacketList.get(), AssetServerError::InvalidByteRange); writeError(replyPacketList.get(), AssetServerError::InvalidByteRange);
} else { } else {
QString filePath = _resourcesDir.filePath(QString(hexHash) + "." + QString(extension)); QString filePath = _resourcesDir.filePath(QString(hexHash));
QFile file { filePath }; QFile file { filePath };