コード例 #1
0
 def save_passport(self, file: InMemoryUploadedFile):
     if self.passport_path:
         self.passport_path.unlink()
     default_storage.save(
         self.folder_path /
         f"{PAYER_PASSPORT_SLUG}{Path(file.name).suffix}",
         ContentFile(file.open().read()))
コード例 #2
0
 def save_study_document(self, file: InMemoryUploadedFile):
     if self.study_document_path:
         self.study_document_path.unlink()
     default_storage.save(
         self.folder_path /
         f"{STUDY_DOCUMENT_SLUG}{Path(file.name).suffix}".replace(" ", "_"),
         ContentFile(file.open().read()))
コード例 #3
0
def check_type_file(file_path=None, file_in_memory: InMemoryUploadedFile = None):
    first_bytes = b''
    if file_path:
        first_bytes = open(file_path).read(2048)
    elif file_in_memory:
        first_bytes = file_in_memory.open().read(2048)
    type_file = magic.from_buffer(first_bytes).lower()
    return "pdf" in type_file or "jpeg" in type_file
コード例 #4
0
    def split_text(self, textfile: uploadedfile.InMemoryUploadedFile,
                   max_lines):
        """
        Splits the textfile into smaller files with at most max_lines sentences. 
        A list of SimpleUploadedFile objects is returned.
        """
        filename = textfile.name
        textfiles = []
        # get encoding
        textfile.open(mode='rb')
        encoding = chardet.detect(textfile.read())['encoding']

        # put all sentences in a list
        filecontent = []  # list of all sentences in the textfile
        sentence = ''  # one sentence in the textfile that is resetted after every \n\n and added to filecontent
        # the open method simply does seek(0). This needs to be done, because the file was already opened to find the encoding
        textfile.open()
        for line in textfile:
            line = line.decode(encoding=encoding)
            # this will not work if the newline character is just '\r'
            line = line.replace('\r', '')

            if line == '\n':
                if sentence != '':
                    filecontent.append(sentence)
                    sentence = ''
            else:
                sentence += line.replace('\n', '')
        if sentence != '':
            filecontent.append(sentence)
        # end of gathering filecontent
        # validate max_lines
        self.check_max_lines(max_lines, len(filecontent))
        # create SimpleUploadedFiles with max_lines of content from the textfile
        for i in range(math.ceil(len(filecontent) / max_lines)):
            filesentences, filecontent = filecontent[:max_lines], filecontent[
                max_lines:]
            content = ''
            for sentence in filesentences:
                content += sentence + '\n\n'
            new_filename = f'{filename[:-4]}_{i + 1}{filename[-4:]}'
            textfiles.append(
                uploadedfile.SimpleUploadedFile(new_filename,
                                                content.encode('utf-8-sig')))

        return textfiles
コード例 #5
0
ファイル: fields.py プロジェクト: Jbonnett/awesome_imagefield
    def _file_save(self, new_pil_obj, version, format, model_instance, file):
        """Save our version files to disk"""

        img_version_buffer = StringIO.StringIO()
        new_pil_obj.save(img_version_buffer, format)

        img_version_buffer = InMemoryUploadedFile(
            file=img_version_buffer,
            field_name=None,
            name='foo',
            content_type='image/%s' % format,
            size=img_version_buffer.len,
            charset=None)
        img_version_buffer.open()  # reopen just in case, which sets Django's File-like object to seek=0

        filename = version['upload_to'](model_instance, file.name)

        # delete first to prevent save() from possibily creating a new uniquely named file
        # we want to replace the file, which save() may not do natively
        file.field.storage.delete(filename)
        file.field.storage.save(filename, img_version_buffer)
コード例 #6
0
 def test_open_resets_file_to_start_and_returns_context_manager(self):
     uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
     uf.read()
     with uf.open() as f:
         self.assertEqual(f.read(), '1')
コード例 #7
0
ファイル: tests.py プロジェクト: LouisAmon/django
 def test_open_resets_file_to_start_and_returns_context_manager(self):
     uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
     uf.read()
     with uf.open() as f:
         self.assertEqual(f.read(), '1')
コード例 #8
0
    def mutate(self, info, file: InMemoryUploadedFile, **kwargs):
        with file.open('rb') as fd:
            content = fd.read().decode('utf8')

        return UploadMutation(success=True, content=content)
コード例 #9
0
 def test_open_resets_file_to_start_and_returns_context_manager(self):
     uf = InMemoryUploadedFile(StringIO("1"), "", "test", "text/plain", 1,
                               "utf8")
     uf.read()
     with uf.open() as f:
         self.assertEqual(f.read(), "1")