Commit f658a3f4 authored by jan.koester's avatar jan.koester
Browse files

partial load implemented

parent d1801a3f
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -40,8 +40,11 @@ namespace blogi {
        Store(){};
        virtual ~Store(){};

        virtual size_t getSize(const char *key)=0;

        virtual void save(const char *key, const char *data,size_t datalen)=0;
        virtual void load(libhttppp::HttpRequest *req,const char *key,const char *ctype) =0;
        virtual void load(libhttppp::HttpRequest *req,const char *key,std::vector<char> &data,size_t pos,size_t blocksize) =0;

    };

    class RedisStore : public Store {
@@ -49,8 +52,10 @@ namespace blogi {
        RedisStore(const char *host,int port,const char *password=nullptr,int millitout=0);
        ~RedisStore();

        size_t getSize(const char *key);

        void save(const char *key,const char *data,size_t datalen) override;
        void load(libhttppp::HttpRequest *req,const char *key,const char *ctype) override;
        void load(libhttppp::HttpRequest *req,const char *key,std::vector<char> &data,size_t pos,size_t blocksize) override;
    private:
        redisContext      *_RedisCTX;
        std::string        _RedisPassword;
+53 −23
Original line number Diff line number Diff line
@@ -506,31 +506,15 @@ namespace blogi {
            const char *ccurl=req->getRequestURL();

            if (strncmp(ccurl,Args->config->buildurl("media/getimage/",url,512),strlen(Args->config->buildurl("media/getimage",url,512)))==0){
                int plen=strlen(Args->config->buildurl("media/getimage/",url,512));
                int mlen=strlen(ccurl);
                size_t plen=strlen(Args->config->buildurl("media/getimage/",url,512));
                size_t mlen=strlen(ccurl);

                std::cout << "test" << std::endl;

                if(mlen-plen<0)
                if( int(mlen-plen) <0)
                    return false;

                std::vector<char> suuid;
                size_t tpos=std::string::npos;

                suuid.resize(mlen-plen);

                for(size_t i = mlen; i>plen; --i){
                    if(ccurl[i]=='.'){
                        tpos=i;
                        break;
                    }
                }

                std::copy(ccurl+plen,ccurl+tpos,suuid.begin());

                suuid.push_back('\0');

                std::cout << suuid.data() << std::endl;
                _getSuuid(ccurl,plen,suuid);

                blogi::SQL sql;
                blogi::DBResult res;
@@ -540,12 +524,16 @@ namespace blogi {

                int n = Args->database->exec(&sql,res);

                std::cout << res[0][0] << std::endl;
                libhttppp::HttpResponse curres;

                if(n>0){
                    _store->load(req,suuid.data(),res[0][0]);
                    req->RecvData.pos=0;
                    curres.setState(HTTP200);
                    curres.setContentType(res[0][0]);
                    curres.setContentLength(_store->getSize(suuid.data()));
                    curres.send(req,nullptr,-1);
                }else{
                    libhttppp::HttpResponse curres;
                    std::cout << "test 404" << std::endl;
                    curres.setVersion(HTTPVERSION(1.1));
                    curres.setState(HTTP404);
                    curres.send(req,nullptr,0);
@@ -555,7 +543,49 @@ namespace blogi {
            }
            return false;
        }

        bool Response(libhttppp::HttpRequest * req){
            char url[512];
            const char *ccurl=req->getRequestURL();

            if (strncmp(ccurl,Args->config->buildurl("media/getimage/",url,512),strlen(Args->config->buildurl("media/getimage",url,512)))==0){
                size_t plen=strlen(Args->config->buildurl("media/getimage/",url,512));

                std::vector<char> suuid,data;
                _getSuuid(ccurl,plen,suuid);

                if(req->RecvData.pos<_store->getSize(suuid.data())){
                    _store->load(req,suuid.data(),data,req->RecvData.pos,BLOCKSIZE);
                    req->SendData.append(data.data(),data.size());
                    req->RecvData.pos+=data.size();
                }
            }
            return true;
        }

    private:
        void _getSuuid(const char *rurl,size_t plen,std::vector<char> &suuid){
            int mlen=strlen(rurl);
            size_t tpos=std::string::npos;

            for(size_t i = mlen; i>plen; --i){
                if(rurl[i]=='.'){
                    tpos=i;
                    break;
                }
            }

            if(tpos==std::string::npos){
                libhttppp::HTTPException e;
                e[libhttppp::HTTPException::Error] << "blogi media plugin: no valid media url!";
                throw e;
            }

            std::copy(rurl+plen,rurl+tpos,std::inserter<std::vector<char>>(suuid,suuid.begin()));

            suuid.push_back('\0');

        }
        Store *_store;
    };
};
+17 −6
Original line number Diff line number Diff line
@@ -56,14 +56,27 @@ blogi::RedisStore::~RedisStore(){
    redisFree(_RedisCTX);
}

size_t blogi::RedisStore::getSize(const char* key){
        redisReply *rep=(redisReply*)redisCommand(_RedisCTX,"STRLEN %s",key);

        if(_RedisCTX->err!=REDIS_OK){
            libhttppp::HTTPException e;
            e[libhttppp::HTTPException::Error] << "media plugin err: " << _RedisCTX->errstr;
            throw e;
        }

        return rep->integer;
}


void blogi::RedisStore::save(const char *key, const char *data,size_t datalen){
    redisCommand(_RedisCTX,"SET %s %b",key,data,datalen);
    redisCommand(_RedisCTX,"save");
}

void blogi::RedisStore::load(libhttppp::HttpRequest *req,const char *key,const char *ctype) {
void blogi::RedisStore::load(libhttppp::HttpRequest *req,const char *key,std::vector<char> &data,size_t pos,size_t blocksize) {
    try{
        redisReply *rep=(redisReply*)redisCommand(_RedisCTX,"GET %s",key);
        redisReply *rep=(redisReply*)redisCommand(_RedisCTX,"GETRANGE %s %d %d",key,pos,pos+blocksize);

        if(_RedisCTX->err!=REDIS_OK){
            libhttppp::HTTPException e;
@@ -71,10 +84,8 @@ void blogi::RedisStore::load(libhttppp::HttpRequest *req,const char *key,const c
            throw e;
        }

        libhttppp::HttpResponse curres;
        curres.setContentType(ctype);
        curres.setState(HTTP200);
        curres.send(req,rep->str,rep->len);
        std::copy(rep->str,rep->str+rep->len,std::inserter<std::vector<char>>(data,data.begin()));

        freeReplyObject(rep);
    }catch(libhttppp::HTTPException &e){
        throw e;
+17 −0
Original line number Diff line number Diff line
@@ -380,6 +380,23 @@ RETRY_REQUEST:
    }
}

void blogi::Blogi::ResponseEvent(libhttppp::HttpRequest* curreq){
    if(PlgArgs->theme->Response(curreq))
        return;

    for(blogi::Plugin::PluginData *curplg=BlogiPlg->getFirstPlugin(); curplg; curplg=curplg->getNextPlg()){
        PluginApi *api=curplg->getInstace();
        std::string url=PlgArgs->config->getprefix();
        url+="/";
        url+=api->getName();
        if(strncmp(curreq->getRequestURL(),url.c_str(),url.length())==0){
            if(api->Response(curreq)){
                return;
            }
        }
    }
}


class HttpConD : public libhttppp::HttpD {
public:
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ namespace blogi {
        void logoutPage(libhttppp::HttpRequest *curreq);
        void settingsPage(libhttppp::HttpRequest *curreq);
        void RequestEvent(libhttppp::HttpRequest *curreq);
        void ResponseEvent(libhttppp::HttpRequest *curreq);
    private:
        Plugin                 *BlogiPlg;
        PluginArgs             *PlgArgs;
Loading