def add_attach(self, name): """Add name as an attachment and return the attachment as a Resource name - a string containing a filename or url that will be attached return: the resource object corresponding to the attached object """ dest = os.path.expanduser('~/.everpad/data/%d/' % self.note.id) try: os.mkdir(dest) except OSError: pass file_name = name.split('/')[-1] file_path = prepare_file_path(dest, file_name) if os.path.isfile(name): shutil.copyfile(name, file_path) else: with open(file_path, 'w') as res_file: res_file.write(urllib.urlopen(name).read()) res = Resource( id=NONE_ID, file_path=file_path, file_name=file_name, mime=self.mime.file(file_path.encode('utf8')), hash=hashlib.md5(open(file_path).read()).hexdigest(), ) self._resources.append(res) self._put(res) self.on_change() return res
def from_api(self, resource): """Fill data from api""" if resource.attributes.fileName: self.file_name = resource.attributes.fileName.decode('utf8') else: self.file_name = resource.guid.decode('utf8') self.hash = binascii.b2a_hex(resource.data.bodyHash) self.action = ACTION_NONE self.mime = resource.mime.decode('utf8') path = os.path.expanduser('~/.everpad/data/%s/' % self.note_id) try: os.mkdir(path) except OSError: pass self.file_path = prepare_file_path(path, self.file_name) with open(self.file_path, 'w') as data: data.write(resource.data.body)
def add_attach(self, name): dest = os.path.expanduser('~/.everpad/data/%d/' % self.note.id) try: os.mkdir(dest) except OSError: pass file_name = name.split('/')[-1] file_path = prepare_file_path(dest, file_name) if os.path.isfile(name): shutil.copyfile(name, file_path) else: with open(file_path, 'w') as res_file: res_file.write(urllib.urlopen(name).read()) res = Resource( id=NONE_ID, file_path=file_path, file_name=file_name, mime=self.mime.file(file_path.encode('utf8')), hash=hashlib.md5(open(file_path).read()).hexdigest(), ) self._resources.append(res) self._put(res) self.on_change() return res