def upload(self, filename, name=None, path=None, type=None, ofile=None, block_size=1024): """ Utility method to upload a file to the server. """ if not self.__sf: raise omero.ClientError("No session. Use createSession first.") import os, types if not filename or not isinstance(filename, types.StringType): raise omero.ClientError("Non-null filename must be provided") if not os.path.exists(filename): raise omero.ClientError("File does not exist: " + filename) from path import path as __path__ filepath = __path__(filename) file = open(filename, 'rb') try: size = os.path.getsize(file.name) if block_size > size: block_size = size if not ofile: ofile = omero.model.OriginalFileI() ofile.hash = omero.rtypes.rstring(self.sha1(file.name)) ofile.hasher = omero.model.ChecksumAlgorithmI() ofile.hasher.value = omero.rtypes.rstring("SHA1-160") abspath = filepath.normpath().abspath() if not ofile.name: if name: ofile.name = omero.rtypes.rstring(name) else: ofile.name = omero.rtypes.rstring(str(abspath.basename())) if not ofile.path: ofile.path = omero.rtypes.rstring( str(abspath.dirname()) + os.path.sep) if not ofile.mimetype: if type: # ofile.mimetype = 'application/octet-stream' by default ofile.mimetype = omero.rtypes.rstring(type) # Disabled with group permissions #1434 # if permissions: # ofile.details.permissions = permissions up = self.__sf.getUpdateService() ofile = up.saveAndReturnObject(ofile) prx = self.__sf.createRawFileStore() try: prx.setFileId(ofile.id.val) prx.truncate(size) # ticket:2337 self.write_stream(file, prx, block_size) finally: prx.close() finally: file.close() return ofile
def upload(self, filename, name = None, path = None, type = None, ofile = None, block_size = 1024): """ Utility method to upload a file to the server. """ if not self.__sf: raise omero.ClientError("No session. Use createSession first.") import os, types if not filename or not isinstance(filename, types.StringType): raise omero.ClientError("Non-null filename must be provided") if not os.path.exists(filename): raise omero.ClientError("File does not exist: " + filename) from path import path as __path__ filepath = __path__(filename) file = open(filename, 'rb') try: size = os.path.getsize(file.name) if block_size > size: block_size = size if not ofile: ofile = omero.model.OriginalFileI() ofile.hash = omero.rtypes.rstring(self.sha1(file.name)) ofile.hasher = omero.model.ChecksumAlgorithmI() ofile.hasher.value = omero.rtypes.rstring("SHA1-160") abspath = filepath.normpath().abspath() if not ofile.name: if name: ofile.name = omero.rtypes.rstring(name) else: ofile.name = omero.rtypes.rstring(str(abspath.basename())) if not ofile.path: ofile.path = omero.rtypes.rstring(str(abspath.dirname())+os.path.sep) if not ofile.mimetype: if type: # ofile.mimetype = 'application/octet-stream' by default ofile.mimetype = omero.rtypes.rstring(type) # Disabled with group permissions #1434 # if permissions: # ofile.details.permissions = permissions up = self.__sf.getUpdateService() ofile = up.saveAndReturnObject(ofile) prx = self.__sf.createRawFileStore() try: prx.setFileId(ofile.id.val) prx.truncate(size) # ticket:2337 self.write_stream(file, prx, block_size) finally: prx.close() finally: file.close() return ofile