def return_file (self, typ, path, delete_on_close=false, filename=None):
     stats = os.stat(path)
     if delete_on_close:
         fp = self_deleting_file(path, 'rb')
     else:
         fp = open(path, 'rb')
     self.request['Content-Type'] = (isinstance(typ, unicode) and typ.encode("ASCII", "replace")) or typ
     self.request['Content-Length'] = stats.st_size
     self.request['Last-Modified'] = http_date.build_http_date(stats.st_mtime)
     add_last_modified_headers(self.request, self.repo)
     if filename:
         filename = (isinstance(filename, unicode) and filename.encode("ASCII", "replace")) or filename
         self.request["Content-Disposition"] = "inline; filename=%s" % filename
     elif not delete_on_close:
         filename = os.path.split(path)[1]
         filename = (isinstance(filename, unicode) and filename.encode("ASCII", "replace")) or filename
         self.request["Content-Disposition"] = "inline; filename=%s" % filename
     if (self.request.command != "HEAD"):
         self.request.push(producers.file_producer(fp))
Example #2
0
    def cmd_retr (self, line):
        'retrieve a file'
        if len(line) < 2:
            self.command_not_understood (string.join (line))
        else:
            file = line[1]
            if not self.filesystem.isfile (file):
                self.log_info ('checking %s' % file)
                self.respond ('550 No such file')
            else:
                try:
                    # FIXME: for some reason, 'rt' isn't working on win95
                    mode = 'r'+self.type_mode_map[self.current_mode]
                    fd = self.open (file, mode)
                except IOError, why:
                    self.respond ('553 could not open file for reading: %s' % (repr(why)))
                    return
                self.respond (
                        "150 Opening %s mode data connection for file '%s'" % (
                                self.type_map[self.current_mode],
                                file
                                )
                        )
                self.make_xmit_channel()

                if self.restart_position:
                    # try to position the file as requested, but
                    # give up silently on failure (the 'file object'
                    # may not support seek())
                    try:
                        fd.seek (self.restart_position)
                    except:
                        pass
                    self.restart_position = 0

                self.client_dc.push_with_producer (
                        file_producer (fd)
                        )
                self.client_dc.close_when_done()
Example #3
0
    def cmd_retr (self, line):
        'retrieve a file'
        if len(line) < 2:
            self.command_not_understood (string.join (line))
        else:
            file = line[1]
            if not self.filesystem.isfile (file):
                self.log_info ('checking %s' % file)
                self.respond ('550 No such file')
            else:
                try:
                    # FIXME: for some reason, 'rt' isn't working on win95
                    mode = 'r'+self.type_mode_map[self.current_mode]
                    fd = self.open (file, mode)
                except IOError, why:
                    self.respond ('553 could not open file for reading: %s' % (repr(why)))
                    return
                self.respond (
                        "150 Opening %s mode data connection for file '%s'" % (
                                self.type_map[self.current_mode],
                                file
                                )
                        )
                self.make_xmit_channel()

                if self.restart_position:
                    # try to position the file as requested, but
                    # give up silently on failure (the 'file object'
                    # may not support seek())
                    try:
                        fd.seek (self.restart_position)
                    except:
                        pass
                    self.restart_position = 0

                self.client_dc.push_with_producer (
                        file_producer (fd)
                        )
                self.client_dc.close_when_done()
Example #4
0
 def check_file (self):
     f = StringIO.StringIO(test_string)
     p = producers.file_producer(f)
     self._check_all(p, test_string)