CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

UploadedFileImpl.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh                  *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <UploadedFileImpl.h>
00021 #include <unistd.h>
00022 #include <stdio.h>
00023 
00024 namespace container {
00025 
00026 UploadedFileImpl::UploadedFileImpl(const std::string& path,
00027                                    bool empty,
00028                                    const std::string& mime,
00029                                    const std::string& name)
00030     : servlet::UploadedFile()
00031     , m_fpath(path)
00032     , m_empty(empty)
00033     , m_mimeType(mime)
00034     , m_name(name)
00035 {
00036 }
00037 
00038 
00039 UploadedFileImpl::~UploadedFileImpl()
00040 {
00041     if(m_stream.is_open()) {
00042         m_stream.close();
00043     }
00044     if(!m_moved) {
00045         killTmp();
00046     }
00047 }
00048 
00055 bool UploadedFileImpl::isEmpty() const
00056 {
00057     return m_empty;
00058 }
00059 
00067 const std::string& UploadedFileImpl::getName() const
00068 {
00069     return m_name;
00070 }
00071 const std::string& UploadedFileImpl::mimeType() const
00072 {
00073     return m_mimeType;
00074 }
00081 std::iostream& UploadedFileImpl::getStream() throw(servlet::IOError)
00082 {
00083     if(!m_stream.is_open()) {
00084         m_stream.open(m_fpath.c_str());
00085         if(!m_stream.is_open()) {
00086             throw servlet::IOError();
00087         }
00088     }
00089     return m_stream;
00090 }
00091 
00092 }
00093 
00094 
00099 void container::UploadedFileImpl::killTmp()
00100 {
00101     ::unlink(m_fpath.c_str());
00102 }
00103 
00104 
00110 void container::UploadedFileImpl::doMoveTo(const std::string& path)
00111     throw(servlet::IOError)
00112 {
00113     long pos;
00114     {
00115         std::iostream& in = getStream();
00116         std::ofstream out(path.c_str());
00117         if(!out)
00118             throw servlet::IOError("Moving temporary file");
00119         pos=in.tellg();
00120         in.seekg(0);
00121         out<<in.rdbuf();
00122         killTmp();
00123         out.close();
00124         m_stream.close();
00125     }
00126     m_fpath=path;
00127     {
00128         std::iostream& in=getStream();
00129         in.seekg(pos);
00130     }
00131 }

SourceForge.net Logo