Ejemplo n.º 1
0
def copy(ds_object, mount_point):
    """Copy a datastore entry

    Keyword arguments:
    ds_object -- DSObject to copy
    mount_point -- mount point of the new datastore entry

    """
    new_ds_object = ds_object.copy()
    new_ds_object.metadata['mountpoint'] = mount_point

    if 'title' in ds_object.metadata:
        filename = ds_object.metadata['title']

        if 'mime_type' in ds_object.metadata:
            mime_type = ds_object.metadata['mime_type']
            extension = mime.get_primary_extension(mime_type)
            if extension:
                filename += '.' + extension

        new_ds_object.metadata['suggested_filename'] = filename

    # this will cause the file be retrieved from the DS
    new_ds_object.file_path = ds_object.file_path

    write(new_ds_object)
Ejemplo n.º 2
0
def copy(ds_object, mount_point):
    """Copy a datastore entry

    Keyword arguments:
    ds_object -- DSObject to copy
    mount_point -- mount point of the new datastore entry

    Returns:
    new_ds_object -- DSObject copied

    """
    new_ds_object = ds_object.copy()
    new_ds_object.metadata['mountpoint'] = mount_point

    if 'title' in ds_object.metadata:
        filename = ds_object.metadata['title']

        if 'mime_type' in ds_object.metadata:
            mime_type = ds_object.metadata['mime_type']
            extension = mime.get_primary_extension(mime_type)
            if extension:
                filename += '.' + extension

        new_ds_object.metadata['suggested_filename'] = filename

    # this will cause the file be retrieved from the DS
    new_ds_object.file_path = ds_object.file_path

    write(new_ds_object)

    return new_ds_object
Ejemplo n.º 3
0
    def __add_cover_response_cb(self, alert, response_id, operation_function):
        if response_id == Gtk.ResponseType.YES:
            try:
                chooser = ObjectChooser(self, what_filter='Image',
                                        filter_type=FILTER_TYPE_GENERIC_MIME,
                                        show_preview=True)
            except:
                # for compatibility with older versions
                chooser = ObjectChooser(self, what_filter='Image')

            try:
                result = chooser.run()
                if result == Gtk.ResponseType.ACCEPT:
                    logging.error('ObjectChooser: %r' %
                                  chooser.get_selected_object())
                    jobject = chooser.get_selected_object()
                    if jobject and jobject.file_path:
                        logging.error("imagen seleccionada: %s",
                                      jobject.file_path)
                        mime_type = mime.get_for_file(jobject.file_path)
                        extension = mime.get_primary_extension(mime_type)
                        tempfile_name = \
                            os.path.join(
                                self.get_activity_root(), 'instance',
                                'tmp%i.%s' % (time.time(), extension))
                        os.link(jobject.file_path, tempfile_name)
                        operation_function(tempfile_name)
            finally:
                chooser.destroy()
                del chooser

        elif response_id == Gtk.ResponseType.NO:
            self._save_epub()
        self.remove_alert(alert)
Ejemplo n.º 4
0
    def __add_cover_response_cb(self, alert, response_id, operation_function):
        if response_id == Gtk.ResponseType.YES:
            try:
                chooser = ObjectChooser(self,
                                        what_filter='Image',
                                        filter_type=FILTER_TYPE_GENERIC_MIME,
                                        show_preview=True)
            except:
                # for compatibility with older versions
                chooser = ObjectChooser(self, what_filter='Image')

            try:
                result = chooser.run()
                if result == Gtk.ResponseType.ACCEPT:
                    logging.error('ObjectChooser: %r' %
                                  chooser.get_selected_object())
                    jobject = chooser.get_selected_object()
                    if jobject and jobject.file_path:
                        logging.error("imagen seleccionada: %s",
                                      jobject.file_path)
                        mime_type = mime.get_for_file(jobject.file_path)
                        extension = mime.get_primary_extension(mime_type)
                        tempfile_name = \
                            os.path.join(
                                self.get_activity_root(), 'instance',
                                'tmp%i.%s' % (time.time(), extension))
                        os.link(jobject.file_path, tempfile_name)
                        operation_function(tempfile_name)
            finally:
                chooser.destroy()
                del chooser

        elif response_id == Gtk.ResponseType.NO:
            self._save_epub()
        self.remove_alert(alert)
Ejemplo n.º 5
0
    def __accept_activate_cb(self, menu_item):
        #TODO: figure out the best place to get rid of that temp file
        extension = mime.get_primary_extension(self.file_transfer.mime_type)
        if extension is None:
            extension = '.bin'
        fd, file_path = tempfile.mkstemp(suffix=extension,
                prefix=self._sanitize(self.file_transfer.title),
                dir=os.path.join(env.get_profile_path(), 'data'))
        os.close(fd)
        os.unlink(file_path)

        self.file_transfer.accept(file_path)
Ejemplo n.º 6
0
    def __accept_activate_cb(self, menu_item):
        #TODO: figure out the best place to get rid of that temp file
        extension = mime.get_primary_extension(self.file_transfer.mime_type)
        if extension is None:
            extension = '.bin'
        fd, file_path = tempfile.mkstemp(suffix=extension,
                prefix=self._sanitize(self.file_transfer.title),
                dir=os.path.join(env.get_profile_path(), 'data'))
        os.close(fd)
        os.unlink(file_path)

        self.file_transfer.accept(file_path)
Ejemplo n.º 7
0
    def _copy_file(original_uri):
        uri = urllib.parse.urlparse(original_uri)
        path = uri.path  # pylint: disable=E1101
        directory_, file_name = os.path.split(path)

        root, ext = os.path.splitext(file_name)
        if not ext or ext == '.':
            mime_type = mime.get_for_file(path)
            ext = '.' + mime.get_primary_extension(mime_type)

        f_, new_file_path = tempfile.mkstemp(ext, root)
        del f_
        shutil.copyfile(path, new_file_path)
        os.chmod(new_file_path, 0o644)

        return 'file://' + new_file_path
Ejemplo n.º 8
0
    def _copy_file(self, original_uri):
        uri = urlparse.urlparse(original_uri)
        path = uri.path  # pylint: disable=E1101
        directory_, file_name = os.path.split(path)

        root, ext = os.path.splitext(file_name)
        if not ext or ext == '.':
            mime_type = mime.get_for_file(path)
            ext = '.' + mime.get_primary_extension(mime_type)

        f_, new_file_path = tempfile.mkstemp(ext, root)
        del f_
        shutil.copyfile(path, new_file_path)
        os.chmod(new_file_path, 0644)

        return 'file://' + new_file_path
Ejemplo n.º 9
0
def get_file_name(title, mime_type):
    file_name = title

    extension = mime.get_primary_extension(mime_type)
    if extension is not None and extension:
        extension = '.' + extension
        if not file_name.endswith(extension):
            file_name += extension

    # Invalid characters in VFAT filenames. From
    # http://en.wikipedia.org/wiki/File_Allocation_Table
    invalid_chars = ['/', '\\', ':', '*', '?', '"', '<', '>', '|', '\x7F']
    invalid_chars.extend([chr(x) for x in range(0, 32)])
    for char in invalid_chars:
        file_name = file_name.replace(char, '_')

    # FAT limit is 255, leave some space for uniqueness
    max_len = 250
    if len(file_name) > max_len:
        name, extension = os.path.splitext(file_name)
        file_name = name[0:max_len - len(extension)] + extension

    return file_name
Ejemplo n.º 10
0
def get_file_name(title, mime_type):
    file_name = title

    extension = mime.get_primary_extension(mime_type)
    if extension is not None and extension:
        extension = '.' + extension
        if not file_name.endswith(extension):
            file_name += extension

    # Invalid characters in VFAT filenames. From
    # http://en.wikipedia.org/wiki/File_Allocation_Table
    invalid_chars = ['/', '\\', ':', '*', '?', '"', '<', '>', '|', '\x7F']
    invalid_chars.extend([chr(x) for x in range(0, 32)])
    for char in invalid_chars:
        file_name = file_name.replace(char, '_')

    # FAT limit is 255, leave some space for uniqueness
    max_len = 250
    if len(file_name) > max_len:
        name, extension = os.path.splitext(file_name)
        file_name = name[0:max_len - len(extension)] + extension

    return file_name
Ejemplo n.º 11
0
 def _get_extension(self, uid):
     mime_type = self._metadata_store.get_property(uid, 'mime_type')
     if mime_type is None or not mime_type:
         return ''
     return mime.get_primary_extension(mime_type)
Ejemplo n.º 12
0
 def _get_extension(self, uid):
     mime_type = self._metadata_store.get_property(uid, 'mime_type')
     if mime_type is None or not mime_type:
         return ''
     return mime.get_primary_extension(mime_type)