Exemple #1
0
/* 取当前KEY对应的数据(包)
 *   注意,当数据有修改时,可能需是更新包的内容,所以
 *   不返回const类型;
 */
Conf *Page::GetCurrentPack()
{
    // 空,则先加载其值;
    if(NULL == m_pack)
    {
        const string &key = GetCurrentKey();
        const string &username = m_request->GetCurrentUser();
        UserData data( username );
        Conf *pack = new Conf;
        if(NULL != pack)
        {
            Ini ini;
            // 填充一个空Conf,以便插入key对应的所有值;
            ini.Set(key, *pack);
            data.Get( ini );
            pack->Set( *(ini.Get(key)) );
            m_pack = pack;
            LOG_DEBUG("Pack[key:%s] loading... ok", key.c_str());
        }
    }
    if(NULL == m_pack)
    {
        // 返回一空值,避免外部做过多的NULL检测;
        static Conf pack;
        return &pack;
    }
    return m_pack;
}
// 删除一条数据
int Submit_DeleteData::Deal(Page *page)
{
    const string &key = page->GetCurrentKey();
    Conf *pack = page->GetCurrentPack();

    // 设为删除,并调用处理函数:
    pack->Set("status", "delete");
    pack->Set("text", "");
    pack->Set("modify", NowTime("%Y%m%d%H%M%S"));

    Ini data;
    data.Set(key, *pack);

    const string &username = page->GetRequest()->GetCurrentUser();
    UserData del( username );

    /*
     * 保存(删除数据)
     */
    if(del.Set(data, true) < 0)
    {
        LOG_ERROR("Detete error: [%s]", key.c_str());
        return ERR;
    }

    /*
     * 删除附件
     */
    vector<string> attachs;
    vector<string>::iterator it;
    del.GetAttachList(key, attachs);   // 取附件列表
    for(it = attachs.begin(); attachs.end() != it; it++)
    {
        LOG_INFO("Delete attach: [%s]", it->c_str());
        DeleteFile(*it);
    }

    LOG_INFO("Detete: [%s]", key.c_str());

    return OK;
}
// 输出http协议头部
int Page_DataExport::OutHead()
{
    FUNCTION_TRACK(); // 函数轨迹跟综

    Connect * const connect = m_request->GetConnect();
    const string &username = m_request->GetCurrentUser();
    const string &backup_range = m_request->GetField("backup_range"); // 导出范围
    const string &include_attach = m_request->GetField("include_attach");
    DataPack pack(username);
    string fullpath;
    string filename; // 传到客户端的文件名

    // 是否需是导出附件
    if("yes" != include_attach)
    {
        // 排除附件
        pack.SetAttrib(DataPack::A_EXCLUDE_ATTACH);
    }

    if("all" == backup_range)
    {
        /*
         * 导出所有数据
         */
        if( pack.MakePack() == ERR
            || pack.AddAll() == ERR
          )
        {
            LOG_ERROR("Export data error");
            return ERR;
        }
    }
    else if("select" == backup_range)
    {
        /*
         * 导出选定的数据
         */
        int i;
        vector<string> keys;
        vector<string>::iterator it;
        UserData data( username );
        Split(m_request->GetField("select_key"), " ", keys); // 分割串为数组

        LOG_DEBUG("keys.size=[%d]", keys.size());
        LOG_DEBUG("keys=[%s]", m_request->GetField("select_key").c_str());

        if( pack.MakePack() == ERR )
        {
            LOG_ERROR("Export data error");
            return ERR;
        }

        it = keys.begin();
        while(1)
        {
            if(keys.end() == it)
            {
                break;
            }

            Ini group;

            // 以50个为一组
            for(i=0; i<50 && keys.end() != it; it++)
            {
                const string &key = *it;
                if("" == key)
                {
                    continue;
                }
                group.Set(key, Conf()); // 先填充一空的Conf()
                LOG_DEBUG("key=[%s]", key.c_str());
            }

            // 取key组对应数据
            if( data.Get( group ) < 0 )
            {
                LOG_ERROR("Get data error, username=[%s]", username.c_str());
                return ERR;
            }

            if( pack.Add( group ) == ERR )
            {
                LOG_ERROR("Pack Add error");
                return ERR;
            }
        }// end of while(1...
    }// end of else if("select" ==...
    else
    {
        LOG_ERROR("Range error: [%s]", backup_range.c_str());
        return OK;
    }

    filename = username + "." + GetCurrentKey() + ".pkg";   // 发送到客户端的文件名
    fullpath = pack.PackFinish(); // 打包完成,取包名;
    LOG_DEBUG("fullpath=[%s] filename=[%s]", fullpath.c_str(), filename.c_str());

    // 再次打开打包文件
    if( !m_file.Open(fullpath) )
    {
        Page::OutHead();
        LOG_ERROR("Export error, Can't open file: [%s]", fullpath.c_str());
        return OK;
    }

    const string &size = IntToString(m_file.Size());

    // 文件下载头部格式
    const string html = ""
                        "HTTP/1.1 200 OK\n"
                        "Accept-Ranges: bytes\n"
                        "Content-Disposition: attachment; filename=\"" + filename + "\"\n"
                        "Content-length: " + size + "\n"
                        "Connection: Keep-Alive\n"
                        "Content-Type: application/ms-excel\n"
                        "\n";

    // 发送
    return connect->Send(html) == html.length() ? OK : ERR;
}