Commit 86882101 authored by Xiaowu Zhang's avatar Xiaowu Zhang

erp5_web_service: add ftp connector

only support putFile
parent 68c77afb
......@@ -66,8 +66,12 @@ class FTPConnector(XMLObject):
},
)
else:
# XXX Must manage in the future ftp and ftps protocol
raise NotImplementedError("Protocol %s is not yet implemented" %(self.getUrlProtocol(),))
return self.getPortalObject().portal_web_services.connect(
user_name=self.getUserId(),
password=self.getPassword(),
url=self.getUrlString(),
transport=self.getUrlProtocol()
)
security.declareProtected(Permissions.AccessContentsInformation, 'renameFile')
def renameFile(self, old_path, new_path):
......@@ -156,3 +160,17 @@ class FTPConnector(XMLObject):
conn.removeDirectory(path)
finally:
conn.logout()
security.declareProtected(Permissions.AccessContentsInformation, 'checkConnection')
def checkConnection(self):
conn = None
try:
conn = self.getConnection()
return True
except Exception:
return False
finally:
if conn:
conn.logout()
......@@ -10,6 +10,12 @@
<key> <string>default_reference</string> </key>
<value> <string>FTPConnector</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>document.erp5.FTPConnector</string> </value>
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2013 Nexedi SARL and Contributors. All Rights Reserved.
# Aurélien Calonne <aurel@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import os
from urlparse import urlparse
from xmlrpclib import Binary
from cStringIO import StringIO
from ftplib import FTP
from zLOG import LOG
class FTPError(Exception):
"""
Default exception for the connection
"""
pass
class FTPConnection:
"""
Handle a FTP Connection
"""
def __init__(self, url, user_name, password=None, bind_address=None):
self.url = url
self.user_name = user_name
self.password = password
self.bind_address = bind_address
def connect(self):
# Check URL
schema = urlparse(self.url)
if schema.scheme == 'ftp':
hostname = schema.hostname
port = int(schema.port)
self.ftp = FTP()
else:
raise FTPError('Not a valid ftp url %s, type is %s' %(self.url, schema.scheme))
try:
self.ftp.connect(hostname, port)
self.ftp.login(self.user_name, self.password)
except Exception as msg:
raise FTPError(str(msg) + ' while establishing connection to %s' % (self.url,))
return self
def writeFile(self, path, filename, data, confirm=True):
"""
Write data in provided filepath
"""
filepath = os.path.join(path, filename)
serialized_data = Binary(str(data))
LOG('write file', 0, filepath)
try:
self.ftp.storbinary('STOR ' + filepath, StringIO(str(serialized_data)))
except Exception as msg:
raise FTPError(str(msg) + ' while writing file %s on %s' % (filepath, path))
def _getFile(self, filepath):
pass
def readBinaryFile(self, filepath):
pass
def readAsciiFile(self, filepath):
pass
def getDirectoryContent(self, path, sort_on=None):
pass
def getDirectoryFileList(self, path):
pass
def removeFile(self, filepath):
pass
def renameFile(self, old_path, new_path):
pass
def createDirectory(self, path, mode=0o777):
pass
def removeDirectory(self, path):
pass
def logout(self):
self.ftp.quit()
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Module Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>default_reference</string> </key>
<value> <string>FTPConnection</string> </value>
</item>
<item>
<key> <string>default_source_reference</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>module.erp5.FTPConnection</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Module Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -52,6 +52,7 @@ handler_module_dict = {
'soap': 'SOAPConnection',
'soap_wsdl': 'SOAPWSDLConnection',
'sftp' : "SFTPConnection",
'ftp' : "FTPConnection",
'sql' : "SQLConnection",
'document' : "DocumentConnection",
}
......
module.erp5.DocumentConnection
module.erp5.FTPConnection
module.erp5.SFTPConnection
module.erp5.SOAPConnection
module.erp5.SOAPWSDLConnection
module.erp5.SQLConnection
module.erp5.XMLRPCConnection
\ No newline at end of file
module.erp5.DocumentConnection
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment