mirror of
https://github.com/YACReader/yacreader
synced 2025-07-14 02:54:46 -04:00
a?adido soporte para contenido est?tico multilenguaje al servidor
This commit is contained in:
@ -56,11 +56,20 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
|
|||||||
if (QFileInfo(docroot+path).isDir()) {
|
if (QFileInfo(docroot+path).isDir()) {
|
||||||
path+="/index.html";
|
path+="/index.html";
|
||||||
}
|
}
|
||||||
QFile file(docroot+path);
|
|
||||||
|
//TODO(DONE) carga sensible a la localizaci<63>n
|
||||||
|
QString stringPath = path;
|
||||||
|
QStringList paths = QString(path).split('/');
|
||||||
|
QString fileName = paths.last();
|
||||||
|
stringPath.remove(fileName);
|
||||||
|
fileName = getLocalizedFileName(fileName, request.getHeader("Accept-Language"), stringPath);
|
||||||
|
QString newPath = stringPath.append(fileName);
|
||||||
|
//END_TODO
|
||||||
|
QFile file(docroot+newPath);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
qDebug("StaticFileController: Open file %s",qPrintable(file.fileName()));
|
qDebug("StaticFileController: Open file %s",qPrintable(file.fileName()));
|
||||||
if (file.open(QIODevice::ReadOnly)) {
|
if (file.open(QIODevice::ReadOnly)) {
|
||||||
setContentType(path,response);
|
setContentType(newPath,response);
|
||||||
response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
||||||
if (file.size()<=maxCachedFileSize) {
|
if (file.size()<=maxCachedFileSize) {
|
||||||
// Return the file content and store it also in the cache
|
// Return the file content and store it also in the cache
|
||||||
@ -112,3 +121,45 @@ void StaticFileController::setContentType(QString fileName, HttpResponse& respon
|
|||||||
}
|
}
|
||||||
// Todo: add all of your content types
|
// Todo: add all of your content types
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool StaticFileController::exists(QString localizedName, QString path) const
|
||||||
|
{
|
||||||
|
QString fileName=docroot+"/"+path + localizedName;
|
||||||
|
QFile file(fileName);
|
||||||
|
return file.exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
//retorna fileName si no se encontr<74> alternativa traducida <20> fileName-locale.extensi<73>n si se encontr<74>
|
||||||
|
QString StaticFileController::getLocalizedFileName(QString fileName, QString locales, QString path) const
|
||||||
|
{
|
||||||
|
QSet<QString> tried; // used to suppress duplicate attempts
|
||||||
|
QStringList locs=locales.split(',',QString::SkipEmptyParts);
|
||||||
|
QStringList fileNameParts = fileName.split('.');
|
||||||
|
QString file = fileNameParts.first();
|
||||||
|
QString extension = fileNameParts.last();
|
||||||
|
// Search for exact match
|
||||||
|
foreach (QString loc,locs) {
|
||||||
|
loc.replace(QRegExp(";.*"),"");
|
||||||
|
loc.replace('-','_');
|
||||||
|
QString localizedName=file+"-"+loc.trimmed()+"."+extension;
|
||||||
|
if (!tried.contains(localizedName)) {
|
||||||
|
if(exists(localizedName, path))
|
||||||
|
return localizedName;
|
||||||
|
tried.insert(localizedName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search for correct language but any country
|
||||||
|
foreach (QString loc,locs) {
|
||||||
|
loc.replace(QRegExp("[;_-].*"),"");
|
||||||
|
QString localizedName=file+"-"+loc.trimmed()+"."+extension;
|
||||||
|
if (!tried.contains(localizedName)) {
|
||||||
|
if(exists(localizedName, path))
|
||||||
|
return localizedName;
|
||||||
|
tried.insert(localizedName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
@ -76,6 +76,10 @@ private:
|
|||||||
|
|
||||||
/** Set a content-type header in the response depending on the ending of the filename */
|
/** Set a content-type header in the response depending on the ending of the filename */
|
||||||
void setContentType(QString file, HttpResponse& response) const;
|
void setContentType(QString file, HttpResponse& response) const;
|
||||||
|
|
||||||
|
QString getLocalizedFileName(QString fileName, QString locales, QString path) const;
|
||||||
|
|
||||||
|
bool StaticFileController::exists(QString localizedName, QString path) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // STATICFILECONTROLLER_H
|
#endif // STATICFILECONTROLLER_H
|
||||||
|
Reference in New Issue
Block a user