// // Tizen Web Device API // Copyright (c) 2012 Samsung Electronics Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the License); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include #include "AttachmentFactory.h" #include "Attachments.h" #include "Attachment.h" #include #include #include using namespace std; using namespace WrtDeviceApis; using namespace WrtDeviceApis::Commons; using namespace DeviceAPI::Messaging; namespace { const char* PATH_DOWNLOADS = "/opt/usr/media/Downloads"; const char* PATH_DOCUMENTS = "/opt/usr/media/Documents"; const char* PATH_SOUNDS = "/opt/usr/media/Sounds"; const char* PATH_IMAGES = "/opt/usr/media/Images"; const char* PATH_VIDEOS = "/opt/usr/media/Videos"; const char* VPATH_DOWNLOADS = "download"; const char* VPATH_DOCUMENTS = "documents"; const char* VPATH_SOUNDS = "sounds"; const char* VPATH_IMAGES = "images"; const char* VPATH_VIDEOS = "videos"; const char* VPATH_WGT_PACKAGE = "wgt-package"; const char* VPATH_WGT_PRIVATE = "wgt-private"; const char* VPATH_WGT_PRIVATE_TEMP = "wgt-package-tmp"; const char* COMMAND_NAME = "/bin/cp"; const char* COMMAND_SWITCH_RECURSIVE = "-r"; const char* MKDIR_COMMAND_NAME = "/bin/mkdir"; const char* MKDIR_COMMAND_SWITCH_RECURSIVE = "-p"; } namespace DeviceAPI { namespace Messaging { const char AttachmentFactory::m_pathSeparator = '/'; IAttachmentPtr AttachmentFactory::createAttachment(const std::string& fullPath, bool isVirtualPath) { LoggerD("enter"); IAttachmentPtr attachment(new Attachment(fullPath, isVirtualPath)); if (!attachment->getIsCreatedProperly()) { ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Attachment couldn't be created"); } return attachment; } IAttachmentPtr AttachmentFactory::createAttachment(const std::string& path, const std::string& mimeType) { LoggerD("enter"); //check virtualPath bool isVirtualPath = true; IAttachmentPtr attachment(new Attachment(path, isVirtualPath)); if (!attachment->getIsCreatedProperly()) { ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Attachment couldn't be created"); } if ( attachment ) attachment->setMimeType(mimeType); return attachment; } std::string AttachmentFactory::getVirtualPathFromRealPath(JSContextRef context, const std::string& real_path) { LoggerD("path = " << real_path); //find virtual root std::string root; std::string tail; std::string::size_type virtualrootpathposition; //download virtualrootpathposition = real_path.find(PATH_DOWNLOADS); if (virtualrootpathposition != std::string::npos) { std::string vroot(PATH_DOWNLOADS); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_DOWNLOADS; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; return virtualpath; } //document virtualrootpathposition = real_path.find(PATH_DOCUMENTS); if (virtualrootpathposition != std::string::npos) { std::string vroot(PATH_DOCUMENTS); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_DOCUMENTS; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; return virtualpath; } //sound virtualrootpathposition = real_path.find(PATH_SOUNDS); if (virtualrootpathposition != std::string::npos) { std::string vroot(PATH_SOUNDS); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_SOUNDS; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; return virtualpath; } //image virtualrootpathposition = real_path.find(PATH_IMAGES); if (virtualrootpathposition != std::string::npos) { std::string vroot(PATH_IMAGES); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_IMAGES; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; LoggerD("virtualpath = " << virtualpath); return virtualpath; } //video virtualrootpathposition = real_path.find(PATH_VIDEOS); if (virtualrootpathposition != std::string::npos) { std::string vroot(PATH_VIDEOS); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_VIDEOS; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; LoggerD("virtualpath = " << virtualpath); return virtualpath; } int widgetId = WrtAccessSingleton::Instance().getWidgetId(); WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); //wgt-package virtualrootpathposition = real_path.find(widgetDB->getWidgetInstallationPath()); if (virtualrootpathposition != std::string::npos) { std::string vroot(widgetDB->getWidgetInstallationPath()); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_WGT_PACKAGE; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; LoggerD("virtualpath = " << virtualpath); return virtualpath; } //wgt-private virtualrootpathposition = real_path.find(widgetDB->getWidgetPersistentStoragePath()); if (virtualrootpathposition != std::string::npos) { std::string vroot(widgetDB->getWidgetPersistentStoragePath()); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_WGT_PRIVATE; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; LoggerD("virtualpath = " << virtualpath); return virtualpath; } //wgt-package-tmp virtualrootpathposition = real_path.find(widgetDB->getWidgetTemporaryStoragePath()); if (virtualrootpathposition != std::string::npos) { std::string vroot(widgetDB->getWidgetTemporaryStoragePath()); tail = real_path.substr(virtualrootpathposition + vroot.size() + 1, real_path.size() - 1); LoggerD("tail = " << tail); vroot=VPATH_WGT_PRIVATE_TEMP; std::string virtualpath = vroot + AttachmentFactory::m_pathSeparator + tail ; LoggerD("virtualpath = " << virtualpath); return virtualpath; } return ""; } std::string AttachmentFactory::getVirtualPathFromEmailServiceFolder(JSContextRef context, const std::string& real_path) { //another folder //copy LoggerD("path = " << real_path); int widgetId = WrtAccessSingleton::Instance().getWidgetId(); int error = 0; WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); #if 0 IWrtWrapperPtr wrt = WrtWrappersMgr::getInstance().getWrtWrapper(context); Assert(wrt && "WrtWrapper not found, not a GLOBAL context supplied?"); WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(wrt->getWidgetId()); #endif size_t pos = real_path.find_last_of("/"); if ((pos + 1) >= real_path.size()) { LoggerE("Problem with short name creation"); Throw(InvalidArgumentException); } std::string attachShortName = real_path.substr(pos + 1); std::string privat_dir = widgetDB->getWidgetPersistentStoragePath(); if ( privat_dir.empty()) { LoggerE("Problem with short name creation"); Throw(InvalidArgumentException); } char buf[] = "/email/XXXXXX"; mode_t mask = umask(S_IWGRP | S_IWOTH); error = mkstemp(buf); umask(mask); LoggerD("mkstemp =" << error); LoggerD("temp file=" << buf << AttachmentFactory::m_pathSeparator << attachShortName); std::stringstream mkdir_cmd; mkdir_cmd << MKDIR_COMMAND_NAME; mkdir_cmd << " " << MKDIR_COMMAND_SWITCH_RECURSIVE; mkdir_cmd << " \"" << privat_dir << buf << AttachmentFactory::m_pathSeparator << "\""; LoggerD("mkdir path=" << mkdir_cmd.str()); int result = system(mkdir_cmd.str().c_str()); if (-1 != result) { std::stringstream cp_cmd; cp_cmd << COMMAND_NAME; cp_cmd << " " << COMMAND_SWITCH_RECURSIVE; cp_cmd << " \"" << real_path << "\""; cp_cmd << " \"" << privat_dir << buf << AttachmentFactory::m_pathSeparator << attachShortName << "\""; LoggerD("cp path=" << cp_cmd.str()); result = system(cp_cmd.str().c_str()); if (-1 != result) { if (0 != WIFEXITED(result)) { if (0 != WEXITSTATUS(result)) { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed."); } } else { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command terminated abnormally."); } } else { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command."); } } else { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command."); } std::string vrootpath(VPATH_WGT_PRIVATE); return vrootpath+ buf + AttachmentFactory::m_pathSeparator + attachShortName ; } std::string AttachmentFactory::getDummyFileFromRealPath(JSContextRef context, const std::string& real_path) { //This file is not in list of root locations LoggerD("path = " << real_path); int widgetId = WrtAccessSingleton::Instance().getWidgetId(); int error = 0; WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); size_t pos = real_path.find_last_of("/"); if ((pos + 1) >= real_path.size()) { LoggerE("Problem with short name creation"); Throw(InvalidArgumentException); } std::string attachShortName = real_path.substr(pos + 1); std::string privat_dir = widgetDB->getWidgetPersistentStoragePath(); if ( privat_dir.empty()) { LoggerE("Problem with short name creation"); Throw(InvalidArgumentException); } char buf[] = "/email/XXXXXX"; mode_t mask = umask(S_IWGRP | S_IWOTH); error = mkstemp(buf); umask(mask); LoggerD("mkstemp =" << error); LoggerD("temp file=" << buf << AttachmentFactory::m_pathSeparator << attachShortName); std::stringstream mkdir_cmd; mkdir_cmd << MKDIR_COMMAND_NAME; mkdir_cmd << " " << MKDIR_COMMAND_SWITCH_RECURSIVE; mkdir_cmd << " \"" << privat_dir << buf << AttachmentFactory::m_pathSeparator << "\""; LoggerD("mkdir path=" << mkdir_cmd.str()); int result = system(mkdir_cmd.str().c_str()); //make folder in the wgt-private. if (-1 != result) { std::stringstream cp_cmd; cp_cmd << COMMAND_NAME; cp_cmd << " " << COMMAND_SWITCH_RECURSIVE; cp_cmd << " \"" << real_path << "\""; cp_cmd << " \"" << privat_dir << buf << AttachmentFactory::m_pathSeparator << attachShortName << "\""; LoggerD("cp path=" << cp_cmd.str()); result = system(cp_cmd.str().c_str()); //copy if (-1 != result) { if (0 != WIFEXITED(result)) { if (0 != WEXITSTATUS(result)) { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command failed."); } } else { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Command terminated abnormally."); } } else { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command."); } } else { ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't launch command."); } return privat_dir + buf + AttachmentFactory::m_pathSeparator + attachShortName ; } std::string AttachmentFactory::getRealPathFromVirtualPath(JSContextRef context, const std::string& path) { LoggerD("path = " << path); std::string root; std::string tail; std::string::size_type separatorPosition = path.find(AttachmentFactory::m_pathSeparator); if (separatorPosition != std::string::npos) { root = path.substr(0, separatorPosition); tail = path.substr(separatorPosition + 1, path.size() - 1); } else { root = path; } LoggerD("root = " << root); LoggerD("tail = " << tail); int widgetId = WrtAccessSingleton::Instance().getWidgetId(); WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); std::string realroot; if ( root == VPATH_DOWNLOADS ) { realroot = PATH_DOWNLOADS; } else if ( root == VPATH_DOCUMENTS ) { realroot = PATH_DOCUMENTS; } else if ( root == VPATH_SOUNDS ) { realroot = PATH_SOUNDS; } else if ( root == VPATH_IMAGES ) { realroot = PATH_IMAGES; } else if ( root == VPATH_VIDEOS ) { realroot = PATH_VIDEOS; } else if ( root == VPATH_WGT_PACKAGE ) { realroot = widgetDB->getWidgetInstallationPath(); } else if ( root == VPATH_WGT_PRIVATE ) { realroot = widgetDB->getWidgetPersistentStoragePath(); } else if ( root == VPATH_WGT_PRIVATE_TEMP ) { realroot = widgetDB->getWidgetTemporaryStoragePath(); } else { //exception LoggerD("Can't find root path"); ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Attachment couldn't be created"); } std::string ret = realroot + AttachmentFactory::m_pathSeparator + tail; return ret; } bool AttachmentFactory::removeDummyFile(const std::string& dummyFilePath) { //remove dummy filePath. if (dummyFilePath.length()>0) { //remove file if ( 0 == unlink(dummyFilePath.c_str())) { //remove folder size_t pos = dummyFilePath.find_last_of("/"); if ((pos + 1) >= dummyFilePath.size()) { LoggerE("Problem with short name creation"); } std::string folder = dummyFilePath.substr(0, pos); LoggerD("remove folder : " << folder); if ( 0 != remove(folder.c_str())) { LoggerE("Remove Error"); } } else { LoggerD("remove file error : " << dummyFilePath); } } return true; } } }