cvl-robot's diary

研究ノート メモメモ https://github.com/dotchang/

ファイルダウンロード用HTMLを自動生成

大量のファイルをWEBで公開したい時,リンクを一つ一つ付けていくのはとても面倒くさい作業です.そこで,ファイルをドラッグアンドドロップすると自動でHTMLを吐き出してくれるプログラムを作ります.ただし、日本語文字に対応していません.openframworksのexamples/utilsのdragDropExampleを改造して作ります.

//--------------------------------------------------------------

void testApp::dragEvent(ofDragInfo info){

  if( info.files.size() > 0 ){

    ofFile f(info.files[0]);

    string index_path = f.getEnclosingDirectory() + "/index.html";

    cout << index_path << endl;

    ofstream fout(index_path.data(), ios::out);

    fout << "<HTML>" << endl;

    fout << "<HEAD>" << endl;

    fout << "<META http-equiv=\"Content-Type\" content=\"text/html; charset=SHIFT_JIS\">" << endl;

    fout << "<META http-equiv=\"Content-Style-Type\" content=\"text/css\">" << endl;

    fout << "<style>" << endl;

    fout << "table {" << endl;

    fout << "border-collapse: collapse;" << endl;

    fout << "}" << endl;

    fout << "td {" << endl;

    fout << "border: solid 1px;" << endl;

    fout << "padding: 0.5em;" << endl;

    fout << "}" << endl;

    fout << "</style>" << endl;

 

    fout << "<TITLE>" << f.getEnclosingDirectory() << "</TITLE>" << endl;

    fout << "</HEAD>" << endl;

    fout << "<BODY>" << endl;

 

    fout << "<table>" << endl;

    fout << "  <caption>" << f.getEnclosingDirectory() << "</caption>" << endl;

    fout << "  <tr>" << endl;

    fout << "    <td>Name</td>" << endl;

    fout << "    <td>Size</td>" << endl;

    fout << "  </tr>" << endl;

 

    for(unsigned int k = 0; k < info.files.size(); k++){

      cout << info.files[k] << endl;

      ofFile file(info.files[k]);

 

      fout << "  <tr>" << endl;

      fout << "    <td>" << "<a href=\"" << file.getFileName().data() << "\">" << file.getFileName() << "</a>" << "</td>" << endl;

      fout << "    <td>" << ( (float)file.getSize() / (1024*1024) ) << "MBytes" << "</td>" << endl;

      fout << "  </tr>" << endl;

    }

    fout << "</table>" << endl;

 

    fout << "</BODY>" << endl;

    fout << "</HTML>" << endl;

    fout.close();

  }

}

 出力されたindex.htmlをWEBブラウザで開くとこんな感じ.

デザインはちょっとさびしいので,好きにデコレーションしてください.

f:id:cvl-robot:20140123223115p:plain