Giter Site home page Giter Site logo

Comments (3)

nolze avatar nolze commented on May 16, 2024 1

Thanks for your suggestion!
I think adding a utility function (as a context manager) similar to your decrypt_office_file() to the library is a good idea and will do it.
In the case of your code example, I might replace decrypted_file = file with decrypted_file = BytesIO(f) so that the function always returns BytesIO.

I also find it really odd that .decrypt() takes the object you want to inject the file into as an argument, rather than returning a BytesIO object? It makes following the code flow feel awkward to me, but that's a for another day!

I understand. The problem is that always creating a BytesIO object can consume unnecessary memory, especially if the document is large in file size.

from msoffcrypto-tool.

954-Ivory avatar 954-Ivory commented on May 16, 2024 1

Thanks for your suggestion! I think adding a utility function (as a context manager) similar to your decrypt_office_file() to the library is a good idea and will do it. In the case of your code example, I might replace decrypted_file = file with decrypted_file = BytesIO(f) so that the function always returns BytesIO.

Brilliant, I just threw that together to get what I needed, I'm sure you'll make it better 😄

I understand. The problem is that always creating a BytesIO object can consume unnecessary memory, especially if the document is large in file size.

~ snip ~ I wrote a load of nonsense, I'm sure you know what you're doing! Plus, this way can support alternatives to just BytesIO which is nice, I guess you could with open as f and pass f as ofile!

There will probably be a lot of people like me who forgot to f.seek(0) 😂.

with open(file_path, 'rb') as f:
    file = msoffcrypto.OfficeFile(f)
    if file.is_encrypted():
        file.load_key(password=password)  # Use password
        file.decrypt(decrypted_io)
    else:
        f.seek(0) # don't forget this
        decrypted_io.write(f.read())
return decrypted_io

from msoffcrypto-tool.

CDWimmer avatar CDWimmer commented on May 16, 2024

Thanks for your suggestion! I think adding a utility function (as a context manager) similar to your decrypt_office_file() to the library is a good idea and will do it. In the case of your code example, I might replace decrypted_file = file with decrypted_file = BytesIO(f) so that the function always returns BytesIO.

Brilliant, I just threw that together to get what I needed, I'm sure you'll make it better 😄

I understand. The problem is that always creating a BytesIO object can consume unnecessary memory, especially if the document is large in file size.

~ snip ~ I wrote a load of nonsense, I'm sure you know what you're doing! Plus, this way can support alternatives to just BytesIO which is nice, I guess you could with open as f and pass f as ofile!

from msoffcrypto-tool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.