def _kp_write_ref(self, path, reference, data, uuid=None, revision=None): path = os.path.join(self.path, path) if os.path.isfile(path): kp = KnowledgePost.from_file(path, format='kp') kp._write_ref(reference, data) kp.to_file(path, format='kp') else: ref_path = os.path.join(path, reference) ref_dir = os.path.dirname(ref_path) if not os.path.exists(ref_dir): os.makedirs(ref_dir) write_binary(ref_path, data)
def copy_image(self, kp, img_path, is_ref=False, repo_name='knowledge'): # Copy image data to new file if is_ref: _, tmp_path = tempfile.mkstemp() write_binary(tmp_path, kp._read_ref(img_path)) else: tmp_path = img_path try: # Get image type img_ext = os.path.splitext(img_path)[1] # Make random filename for image random_name = ''.join( random.choice(string.ascii_lowercase) for i in range(6)) timestamp = int(round(time.time() * 100)) fname_img = (f'{repo_name}_{timestamp}_' f'{random_name}{img_ext}').strip().replace(' ', '-') # See if a static file directory exists, if not, let's create if not os.path.exists(self.image_dir): os.makedirs(self.image_dir) # Copy images to local http server directory new_path = os.path.join(self.image_dir, fname_img) logger.info(f'Copying image {tmp_path} to {new_path}') try: shutil.copyfile(tmp_path, new_path) except Exception as e: raise Exception('Problem copying image: {}'.format(e)) finally: # Clean up temporary file if is_ref: os.remove(tmp_path) # return uploaded path of file return urljoin(self.http_image_root, fname_img)
def copy_image(self, kp, img_path, is_ref=False, repo_name='knowledge'): # Copy image data to new file if is_ref: _, tmp_path = tempfile.mkstemp() write_binary(tmp_path, kp._read_ref(img_path)) else: tmp_path = img_path try: # Get image type img_ext = posixpath.splitext(img_path)[1] # Make random filename for image random_name = ''.join( random.choice(string.ascii_lowercase) for i in range(6)) timestamp = int(round(time.time() * 100)) fname_img = (f'{repo_name}_{timestamp}_' f'{random_name}{img_ext}').strip().replace(' ', '-') # Copy image to accessible folder on S3 fname_s3 = posixpath.join(self.s3_image_root, repo_name, fname_img) # Note: The following command may need to be prefixed with a # login agent; # for example, to handle multi-factor authentication. cmd = f"aws s3 cp '{tmp_path}' {fname_s3}" logger.info(f'Uploading images to S3: {cmd}') retval = os.system(cmd) if retval != 0: raise Exception('Problem uploading images to s3') finally: # Clean up temporary file if is_ref: os.remove(tmp_path) # return uploaded path of file return posixpath.join(self.http_image_root, repo_name, fname_img)
def _kp_write_ref(self, path, reference, data, uuid=None, revision=None): ref_path = os.path.join(self.path, path, reference) ref_dir = os.path.dirname(ref_path) if not os.path.exists(ref_dir): os.makedirs(ref_dir) write_binary(ref_path, data)
def to_file(self, filename, **opts): write_binary(filename, self.to_string())