Files
core/ASCOfficeOdfFileW/OfficeOdfFileW.cpp
Elen.Subbotina 350c925085 начата конвертация docx->doct
git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@56748 954022d7-b5bf-4e40-9824-e11837661b57
2016-05-20 22:58:01 +03:00

204 lines
5.3 KiB
C++

// OfficeOdfFileW.cpp : Implementation of COfficeOdfFileW
#include "stdafx.h"
#include "OfficeOdfFileW.h"
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/algorithm/string.hpp>
#include "source\Oox2OdfConverter\Converter.h"
#import "../Redist/ASCOfficeOdfFile.dll" rename_namespace("ASCOfficeOdfFile"), raw_interfaces_only
#ifndef STANDALONE_USE
#define STANDALONE_USE 0// ÷òî íà âõîäå: ôàéë (1) èëè ïàïêà (0)
#endif
// èìÿ äèðåêòîðèè - uuid
boost::filesystem::wpath MakeTempDirectoryName(const std::wstring& Dst)
{
boost::uuids::random_generator gen;
boost::uuids::uuid u = gen();
boost::filesystem::wpath path = boost::filesystem::wpath(Dst) / boost::lexical_cast<std::wstring>(u);
return path;
}
std::wstring bstr2wstring(BSTR str)
{
return str ? std::wstring(&str[0], &str[::SysStringLen(str)]) : L"";
}
STDMETHODIMP COfficeOdfFileW::LoadFromFile(BSTR sSrcFileName, BSTR sDstPath, BSTR sXMLOptions)
{
ATL::CComPtr<IAVSOfficeFileTemplate> odfFile;
HRESULT hr = odfFile.CoCreateInstance(__uuidof(ASCOfficeOdfFile::COfficeOdfFile));
if (hr == S_OK && odfFile)
{
hr = odfFile->LoadFromFile(sSrcFileName, sDstPath, sXMLOptions);
}
return hr;
}
bool COfficeOdfFileW::initialized()
{
return (!!office_utils_);
}
STDMETHODIMP COfficeOdfFileW::SaveToFile(BSTR sDstFileName, BSTR sSrcPath, BSTR sXMLOptions)
{
HRESULT hr;
if (!initialized())
return E_FAIL;
if (!sSrcPath)
{
_ASSERTE(!!sSrcPath);
return E_FAIL;
}
#if defined(STANDALONE_USE) && (STANDALONE_USE == 1)
boost::filesystem::wpath inputDir = boost::filesystem::wpath(CString(sSrcPath)).parent_path();
#else
boost::filesystem::wpath inputDir = boost::filesystem::wpath(CString(sSrcPath));
#endif
boost::filesystem::wpath outputDir = boost::filesystem::wpath(CString(sDstFileName)).parent_path();
// ñîçäàåì å¸ â äèðåêòîðèè êóäà çàïèøåì ðåçóëüòàò
boost::filesystem::wpath dstTempPath = MakeTempDirectoryName(outputDir.string());
#if defined(STANDALONE_USE) && (STANDALONE_USE == 1)
boost::filesystem::wpath srcTempPath = MakeTempDirectoryName(BOOST_STRING_PATH(outputDir));
#else
boost::filesystem::wpath srcTempPath = inputDir.string();
#endif
try
{
boost::filesystem::create_directory(dstTempPath);
#if defined(STANDALONE_USE) && (STANDALONE_USE == 1)
boost::filesystem::create_directory(srcTempPath); // ñîçäàåì âðåìåííóþ äèðåêòîðèþ äëÿ ðåçóëüòèðóþùèõ ôàéëîâ
#endif
hr = SaveToFileImpl(bstr2wstring(sSrcPath),srcTempPath.string(), dstTempPath.string(), bstr2wstring(sDstFileName));
}
catch(...)
{
hr = E_FAIL;
}
// ñòèðàåì âðåìåííóþ äèðåêòîðèþ ñ ðàñïàêîâàííûì èñõîäíèêîì
try
{
boost::filesystem::remove_all(dstTempPath);
}
catch(...)
{
}
#if defined(STANDALONE_USE) && (STANDALONE_USE == 1)
// â ñëó÷àå åñëè íà âûõîäå ôàéë — ñòèðàåì âðåìåííóþ äèðåêòîðèþ (ìû ñàìè åå ñîçäàëè)
try
{
boost::filesystem::remove_all(srcTempPath);
}
catch(...)
{
}
#endif
return hr;
}
HRESULT COfficeOdfFileW::SaveToFileImpl(const std::wstring & srcPath,
const std::wstring & srcTempPath,
const std::wstring & dstTempPath,
const std::wstring & dstFileName)
{
HRESULT hr = E_FAIL;
// ðàñïàêîâûâàåì èñõîäíèê (åñëè îí ôàéë) âî âðåìåííóþ äèðåêòîðèþ
#if defined(STANDALONE_USE) && (STANDALONE_USE == 1)
if FAILED(hr = office_utils_->ExtractToDirectory(CComBSTR(srcPath.c_str()), CComBSTR(srcTempPath.c_str()), NULL, 0))
return hr;
#endif
try
{
std::wstring type = DetectTypeDocument(srcTempPath);
Oox2Odf::Converter converter(srcTempPath, type);
converter.convert();
converter.write(dstTempPath);
}
catch(...)
{
}
if FAILED(hr = office_utils_->CompressFileOrDirectory(CComBSTR(dstTempPath.c_str()), CComBSTR(dstFileName.c_str()), (-1)))
return hr;
return S_OK;
}
namespace fs = boost::filesystem;
std::wstring COfficeOdfFileW::DetectTypeDocument(const std::wstring & Path)
{
fs::wpath full_path(/* fs::initial_path<fs::wpath>()*/ Path);
//full_path = fs::system_complete( fs::wpath( Path ) );
unsigned long file_count = 0;
unsigned long dir_count = 0;
unsigned long other_count = 0;
unsigned long err_count = 0;
if (!fs::exists( full_path ) )return L"";
if (!fs::is_directory( full_path ) )return L"";
fs::wdirectory_iterator end_iter;
for ( fs::wdirectory_iterator dir_itr( full_path ); dir_itr != end_iter; ++dir_itr )
{
try
{
if ( fs::is_directory( dir_itr->status() ) )
{
++dir_count;
std::wstring tmp = dir_itr->path().filename();
boost::algorithm::to_lower(tmp);
if (tmp == L"word") return L"text";
if (tmp == L"xl") return L"spreadsheet";
}
else if ( fs::is_regular_file( dir_itr->status() ) )
{
++file_count;
}
else
{
++other_count;
}
}
catch ( const std::exception & ex )
{
++err_count;
}
}
return L"";
}