Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsModFileUpload.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2006-Today Guy Deleeuw
3 *
4 * See the LICENSE file for terms of use.
5 */
6 
7 #include <dlfcn.h>
8 #include <stdio.h>
9 
10 #include <iostream>
11 
12 #include <boost/filesystem.hpp>
13 
14 #include <Wt/WLogger>
15 #include <Wt/WText>
16 
17 #include <gdcore/gdCore.h>
18 
19 #include <Logger/WsLogger.h>
20 
21 #include <Main/WsApplication.h>
22 
23 #include "WsModFileUpload.h"
24 
25 extern "C" {
27  {
28  void* hndl = dlopen("libwt.so", RTLD_NOW | RTLD_GLOBAL);
29  if ( hndl == NULL ) {
30  fprintf(stderr, "cannot load libwt.so shared library! %s\n", dlerror());
31  return;
32  }
33  }
34 }
35 
36 // Class WsFileUpload
37 // ==================
38 WsFileUpload::WsFileUpload(WContainerWidget* parent)
39  : WContainerWidget(parent), m_pFU(0)
40 {
41  // addStyleClass("WsFileUpload");
42 }
43 
45 {
46  WContainerWidget::load();
47  m_dialog = new WDialog();
48  m_dialog->addStyleClass("WsFileUpload");
49  m_dialog->setTitleBarEnabled(false);
50  m_dialog->contents()->addWidget(new WText("Upload a file with a maximum size of 8Mb"));
51  m_pFU = new WFileUpload();
52  m_pFU->setProgressBar(new WProgressBar());
53  m_pFU->changed().connect(this, &WsFileUpload::doUpload);
54  m_pFU->uploaded().connect(this, &WsFileUpload::doUploaded);
55  m_pFU->fileTooLarge().connect(this, &WsFileUpload::doFileTooLarge);
56  m_dialog->contents()->addWidget(m_pFU);
57  WPushButton* cancel = new WPushButton("Cancel ", m_dialog->contents());
58  cancel->clicked().connect(SLOT(this, WsFileUpload::doCancel));
59  m_dialog->show();
60  WApplication::instance()->log("notice") << "WsFileUpload::load : end !";
61 }
62 
64 {
65 }
66 
68 {
69  m_pFU->upload();
70 }
71 
73 {
74  m_dialog->hide();
75  delete m_dialog;
76  string internal = wApp->internalPath();
77  boost::algorithm::replace_all(internal, "/FileUpload", "");
78  wApp->setInternalPath(internal, false);
79 }
80 
82 {
83  // TODO : Verif des paths, quid si multiples file ?, optimiser les strings, progressbar marche pas, , ajout d'un champ : nom desire a l'arrivee ?, test si file existe : replace ?
84  std::string sCurUrl = wApp->internalPath();
85  std::string sWithoutPrefix = WsApp->WsModules().pathWithoutPrefix(sCurUrl);
86  WsUser* pUser = WsApp->wsUser();
87  std::string m_sDocumentRoot = pUser->getRootPath(); // /var/www/demo_site
88  std::string sCurPath = m_sDocumentRoot + sWithoutPrefix;
89  std::string sNewFile;
90  if ( gdcore_isPathFile(sCurPath) )
91  sNewFile = boost::filesystem::path(sCurPath).parent_path().string() + "/" + m_pFU->clientFileName().toUTF8();
92  else
93  sNewFile = sCurPath + "/" + m_pFU->clientFileName().toUTF8();
94  if (exists(sNewFile)) {
95  LOG(DEBUG) << "WsFileUpload::doUploaded(): sNewFile already exist " << sNewFile;
96  m_dialog->hide();
97  StandardButton
98  result = WMessageBox::show("Error", "File already exist in current directory. Replace ?", Ok | Cancel);
99  if (result != Ok) {
100  //delete m_pFU;
101  //delete m_dialog;
102  string internal = wApp->internalPath();
103  boost::algorithm::replace_all(internal, "/FileUpload", "");
104  wApp->setInternalPath(internal, false);
105  return;
106  }
107  }
108  try {
109  boost::filesystem::copy_file(m_pFU->spoolFileName(), sNewFile, copy_option::overwrite_if_exists);
110  // The remove is make by the Wt::WFileUpload class
111  // boost::filesystem::remove(m_pFU->spoolFileName());
112  boost::algorithm::replace_first(sNewFile, m_sDocumentRoot, "/Edit");
113  sleep(1);
114  std::string newNode = sNewFile;
115  boost::algorithm::replace_all(newNode, "/Edit", "");
116  boost::algorithm::replace_first(newNode, "//", "/");
117  boost::algorithm::replace_first(sNewFile, "//", "/");
118  LOG(DEBUG)<<"WsFileUpload::doUploaded(): will now insert node "<<newNode;
119  pUser->createNode(newNode, WsUser::File);
120  m_dialog->hide();
121  delete m_dialog;
122  wApp->setInternalPath(sNewFile, true);
123  } catch (boost::filesystem::filesystem_error& e) {
124  wApp->log("ERROR") << " WsFileUpload::doUploaded() cannot move " << m_pFU->spoolFileName() << " to " << sNewFile << " error = " << e.what();
125  }
126 }
127 
128 void WsFileUpload::doFileTooLarge(int64_t nSize)
129 {
130  wApp->log("notice") << " WsFileUpload::doFileTooLarge() : size " << nSize;
131 }
132 
133 
134 // Class WsModFileUpload
135 // =====================
137  : WsModule()
138 {
139 }
140 
142 {
143 }
144 
145 WWidget* WsModFileUpload::createContentsMenuBar(WContainerWidget* parent) const
146 {
147  return 0;
148 }
149 
150 WWidget* WsModFileUpload::createContents(WContainerWidget* parent) const
151 {
152  WsFileUpload* pFU = new WsFileUpload(parent);
153  pFU->setOptions(options());
154  pFU->outOptions("WsModFileUpload::createContents()");
155  return pFU;
156 }
157 
158 WsEditorWidget* WsModFileUpload::createEditor(WContainerWidget* parent) const
159 {
160  return 0;
161 }
162 
163 WWidget* WsModFileUpload::createAdmin(WContainerWidget* parent) const
164 {
165  return 0;
166 }
167 
168 std::string WsModFileUpload::description() const
169 {
170  return "WsModFileUpload wittyShare module";
171 }
172 
void WsModFileUploadInit(void)
#define DEBUG
Definition: WsLogger.h:27
a wittyShare module
WDialog * m_dialog
WsFileUpload(Wt::WContainerWidget *parent=0)
CTor.
const std::vector< WsOption > & options() const
Get all options.
Definition: WsOption.cpp:70
WsEditorWidget * createEditor(Wt::WContainerWidget *parent=0) const
Create the contents for an editor (create a view of options).
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
#define LOG
Definition: WsLogger.h:22
Wt::WFileUpload * m_pFU
std::string description() const
Return the description of the module.
void doFileTooLarge(int64_t nSize)
Wt::WWidget * createContents(Wt::WContainerWidget *parent=0) const
Create the contents.
const std::string getRootPath()
return the root path of the filesystem tree, example : /var/www/demo_site
Definition: WsUser.cpp:55
void outOptions(const std::string message)
Output all options.
Definition: WsOption.cpp:75
int createNode(const string &path, NodeType type)
create a directory or File. If the node is a WsDirNode than it will be only accessible to the Admin a...
Definition: WsUser.cpp:105
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
void setOptions(const std::vector< WsOption > &vOptions)
Set all options.
Definition: WsOption.cpp:57
Wt::WWidget * createAdmin(Wt::WContainerWidget *parent=0) const
Create the contents for an administrator.
Wt::WWidget * createContentsMenuBar(Wt::WContainerWidget *parent=0) const
Create the functionalities.