Ejemplo n.º 1
0
    def save(self, **kwargs):
        from ecs.documents.tasks import encrypt_and_upload_to_storagevault
              
        if not self.file:
            raise ValueError('no file')

        if not self.uuid_document: 
            self.uuid_document = uuid4().get_hex() # generate a new random uuid
            content_type, encoding = mimetypes.guess_type(self.file.name) # look what kind of mimetype we would guess

            if self.mimetype == 'application/pdf' or content_type == 'application/pdf':
                if not pdf_isvalid(self.file):
                    raise ValidationError('no valid pdf')

        if not self.hash:
            m = hashlib.md5() # calculate hash sum
            self.file.seek(0)
            while True:
                data= self.file.read(8192)
                if not data: break
                m.update(data)
            self.file.seek(0)
            self.hash = m.hexdigest()
                       
        if self.mimetype == 'application/pdf':
            self.pages = pdf_page_count(self.file) # calculate number of pages

        first_save = True if self.pk is None else False
        super(Document, self).save(**kwargs)
        
        if first_save:
            #print("doc file %s , path %s, original %s" % (str(self.file.name), str(self.file.path), str(self.original_file_name)))
            # upload it via celery to the storage vault
            encrypt_and_upload_to_storagevault.apply_async(args=[self.pk], countdown=3)