def upload_version_output(self, version, file_object, filename): """Uploads a file as an output for the given :class:`.Version` instance. Will store the file in {{Version.absolute_path}}/Outputs/Stalker_Pyramid/ folder. It will also generate a thumbnail in {{Version.absolute_path}}/Outputs/Stalker_Pyramid/Thumbs folder and a web friendly version (PNG for images, WebM for video files) under {{Version.absolute_path}}/Outputs/Stalker_Pyramid/ForWeb folder. :param version: A :class:`.Version` instance that the output is uploaded for. :type version: :class:`.Version` :param file_object: The file like object holding the content of the uploaded file. :param str filename: The original filename. :returns: :class:`.Link` instance. """ ############################################################ # ORIGINAL ############################################################ file_path = os.path.join(os.path.join(version.absolute_path), self.version_output_path) # upload it version_output_file_full_path = \ self.upload_file(file_object, file_path, filename) version_output_file_name = \ os.path.basename(version_output_file_full_path) version_output_base_name = \ os.path.splitext(version_output_file_name)[0] # create a Link instance and return it. # use a Repository relative path repo = version.task.project.repository from stalker import Link full_path = str(version_output_file_full_path) link = Link(full_path=repo.to_os_independent_path(full_path), original_filename=str(filename)) # create a thumbnail for the given version output # don't forget that the first thumbnail is the Web viewable version # and the second thumbnail is the thumbnail ############################################################ # WEB VERSION ############################################################ web_version_link = None try: web_version_temp_full_path = \ self.generate_media_for_web(version_output_file_full_path) web_version_extension = \ os.path.splitext(web_version_temp_full_path)[-1] web_version_full_path = \ os.path.join( os.path.dirname(version_output_file_full_path), 'ForWeb', version_output_base_name + web_version_extension ) web_version_link = Link( full_path=repo.to_os_independent_path(web_version_full_path), original_filename=filename) # move it to repository try: os.makedirs(os.path.dirname(web_version_full_path)) except OSError: # path exists pass shutil.move(web_version_temp_full_path, web_version_full_path) except RuntimeError: # not an image or video so skip it pass ############################################################ # THUMBNAIL ############################################################ # finally generate a Thumbnail thumbnail_link = None try: thumbnail_temp_full_path = \ self.generate_thumbnail(version_output_file_full_path) thumbnail_extension = os.path.splitext( thumbnail_temp_full_path)[-1] thumbnail_full_path = \ os.path.join( os.path.dirname(version_output_file_full_path), 'Thumbnail', version_output_base_name + thumbnail_extension ) thumbnail_link = Link( full_path=repo.to_os_independent_path(thumbnail_full_path), original_filename=filename) # move it to repository try: os.makedirs(os.path.dirname(thumbnail_full_path)) except OSError: # path exists pass shutil.move(thumbnail_temp_full_path, thumbnail_full_path) except RuntimeError: # not an image or video so skip it pass ############################################################ # LINK Objects ############################################################ # link them # assign it as an output to the given version version.outputs.append(link) if web_version_link: link.thumbnail = web_version_link if thumbnail_link: web_version_link.thumbnail = thumbnail_link return link
def upload_reference(self, task, file_object, filename): """Uploads a reference for the given task to Task.path/References/Stalker_Pyramid/ folder and create a Link object to there. Again the Link object will have a Repository root relative path. It will also create a thumbnail under {{Task.absolute_path}}/References/Stalker_Pyramid/Thumbs folder and a web friendly version (PNG for images, WebM for video files) under {{Task.absolute_path}}/References/Stalker_Pyramid/ForWeb folder. :param task: The task that a reference is uploaded to. Should be an instance of :class:`.Task` class. :type task: :class:`.Task` :param file_object: The file like object holding the content of the uploaded file. :param str filename: The original filename. :returns: :class:`.Link` instance. """ ############################################################ # ORIGINAL ############################################################ file_path = os.path.join(os.path.join(task.absolute_path), self.reference_path) # upload it reference_file_full_path = \ self.upload_file(file_object, file_path, filename) reference_file_file_name = os.path.basename(reference_file_full_path) reference_file_base_name = \ os.path.splitext(reference_file_file_name)[0] # create a Link instance and return it. # use a Repository relative path repo = task.project.repository from stalker import Repository, Link assert isinstance(repo, Repository) relative_full_path = repo.make_relative(reference_file_full_path) link = Link(full_path=relative_full_path, original_filename=filename) # create a thumbnail for the given reference # don't forget that the first thumbnail is the Web viewable version # and the second thumbnail is the thumbnail ############################################################ # WEB VERSION ############################################################ web_version_temp_full_path = \ self.generate_media_for_web(reference_file_full_path) web_version_extension = \ os.path.splitext(web_version_temp_full_path)[-1] web_version_file_name = '%s%s' % (reference_file_base_name, web_version_extension) web_version_full_path = \ os.path.join( os.path.dirname(reference_file_full_path), 'ForWeb', web_version_file_name ) web_version_repo_relative_full_path = \ repo.make_relative(web_version_full_path) web_version_link = Link(full_path=web_version_repo_relative_full_path, original_filename=web_version_file_name) # move it to repository try: os.makedirs(os.path.dirname(web_version_full_path)) except OSError: # path exists pass shutil.move(web_version_temp_full_path, web_version_full_path) ############################################################ # THUMBNAIL ############################################################ # finally generate a Thumbnail thumbnail_temp_full_path = \ self.generate_thumbnail(reference_file_full_path) thumbnail_extension = os.path.splitext(thumbnail_temp_full_path)[-1] thumbnail_file_name = '%s%s' % (reference_file_base_name, thumbnail_extension) thumbnail_full_path = \ os.path.join( os.path.dirname(reference_file_full_path), 'Thumbnail', thumbnail_file_name ) thumbnail_repo_relative_full_path = \ repo.make_relative(thumbnail_full_path) thumbnail_link = Link(full_path=thumbnail_repo_relative_full_path, original_filename=thumbnail_file_name) # move it to repository try: os.makedirs(os.path.dirname(thumbnail_full_path)) except OSError: # path exists pass shutil.move(thumbnail_temp_full_path, thumbnail_full_path) ############################################################ # LINK Objects ############################################################ # link them # assign it as a reference to the given task task.references.append(link) link.thumbnail = web_version_link web_version_link.thumbnail = thumbnail_link return link