def unset(self, name, instance, **kwargs): self.log(name, instance) try: ExternalStorage.unset(self, name, instance, **kwargs) except Unauthorized: sm = getSecurityManager() manager.id = sm.getUser().getId() newSecurityManager(instance.REQUEST, manager) ExternalStorage.unset(self, name, instance, **kwargs) setSecurityManager(sm) pass if instance.isTemporary(): return append(UnsetAction(instance, os.path.join(self.getRootPath(), self.getFilepath(instance, name))))
def setStorageItem(self, instance, name, value): """Sets an item into ES storage area. """ if instance.isTemporary(): return # check for old item to perform a move/copy filepath = os.path.join(self.getRootPath(), value.get('filepath')) old_item = self.getStorageItem(instance, name) if old_item: old_filepath = os.path.join(self.getRootPath(), old_item.get('filepath')) if not old_filepath == filepath and os.path.exists(old_filepath): if not getattr(instance, '_v_copying', False): os.remove(old_filepath) ExternalStorage.setStorageItem(self, instance, name, value) self.recursiveDelete(os.path.dirname(old_filepath)) return ExternalStorage.setStorageItem(self, instance, name, value)
def getFileSystemPath(self, instance, item): """Returns the file system path (with filename) where to store a instance field. """ path = ExternalStorage.getFileSystemPath(self, instance, item) # try to find an appropriate extension try: basename, extension = os.path.splitext(getattr(instance.REQUEST.form.get('%s_file' % item), 'filename', self.getStorageItem(instance, item).get('filepath'))) path = '%s%s' % (path, extension) except: pass return path
def initializeInstance(self, instance, item=None, container=None): """ """ path = '/'.join(instance.getPhysicalPath()) rename_or_copy = False if self.isInitialized(instance) and \ self.getInstancePath(instance) != path: rename_or_copy = True instance._v_renaming = True if not self.hasTempData(instance): instance._v_copying = True if rename_or_copy: try: sm = getSecurityManager() manager.id = sm.getUser().getId() newSecurityManager(instance.REQUEST, manager) ExternalStorage.initializeInstance(self, instance, item, container) setSecurityManager(sm) except Unauthorized: pass else: ExternalStorage.initializeInstance(self, instance, item, container) instance._v_renaming, instance._v_copying = False, False
def _discovering_dist_ids(project): # for each file in the project we # extract the distutils name project_path = '/'.join(project.getPhysicalPath()) files = cat(**{ 'portal_type': ['PSCFile', 'PSCFileLink'], 'path': project_path }) ids = [] for file_ in files: portal_type = file_.portal_type if portal_type == 'PSCFileLink': # the file is somewhere else, let's scan it file_ = file_.getObject() file_ = DistantFile(file_.getExternalURL()) else: file_ = file_.getObject() # trying to get back from old # storage # ExternalStorage here # if WAS_EXTERNAL_STORAGE and portal_type != 'PSCFileLink': from Products.ExternalStorage.ExternalStorage import\ ExternalStorage storage = ExternalStorage( prefix=EXTERNAL_STORAGE_PATHS[0], archive=False, rename=False, ) # transferring old data to new AT container fs = file_.schema['downloadableFile'] old = fs.storage fs.storage = storage portal_url = getToolByName(file_, 'portal_url') real_file = os.path.join( *portal_url.getRelativeContentPath(file_)) for path in EXTERNAL_STORAGE_PATHS: final_file = os.path.join(path, real_file) if os.path.exists(final_file): break if not os.path.exists(final_file): logging.info( '******** could not get %s on the file system !!' % real_file) continue #raise ValueError('File not found %s' % final_file) fs.storage = old dfile = file_.getDownloadableFile() filename = dfile.filename data = open(final_file).read() if data == '': logging.info('empty file ! %s' % final_file) #f = File(filename, filename, open(final_file)) elif portal_type != 'PSCFileLink': storage = AttributeStorage() fs = file_.schema['downloadableFile'] old = fs.storage fs.storage = storage dfile = file_.getDownloadableFile() data = dfile.get_data() filename = dfile.filename fs.storage = old #file_.getDownloadableFile().data = data #f = File(filename, filename, StringIO(data)) if portal_type != 'PSCFileLink': #file_.setDownloadableFile(f) file_.schema = PSCFileSchema if filename == '' and data == '': logging.info('file empty for %s' % file_) else: if filename is None: filename = file_.getId() file_.setDownloadableFile( File(filename, filename, StringIO(data))) id_ = extract_distutils_id(file_) if id_ is not None and id_ not in ids: ids.append(id_) return ids
def unset(self, name, instance, **kwargs): self._checkStorage(instance) return _E.unset(self, name, instance, **kwargs)
def set(self, name, instance, value, **kwargs): self._checkStorage(instance) return _E.set(self, name, instance, value, **kwargs)
def __init__(self, context): _E.__init__(self, archive=False, rename=False) storage = grab_utility(context) if storage is not None: self.prefix = storage.path self.context = context
def __init__(self, prefix='files', archive=False, rename=False, suffix='', path_method='getExternalPath'): """Initializes default values. """ ExternalStorage.__init__(self, prefix, archive, rename, suffix, path_method) storage_registry.append(self)
def _set(self, name, instance, value, **kwargs): """ """ ExternalStorage.set(self, name, instance, value, **kwargs)