toga icon indicating copy to clipboard operation
toga copied to clipboard

Local environment configuration is not respected for app file storage

Open rmartin16 opened this issue 1 year ago • 8 comments

Describe the bug

Toga makes assumptions about which file paths to use for toga.Paths; this can conflict with local machine configuration that should affect arbitrary application storage. For instance, on GTK, if I configure my XDG env vars to something other than the default values, then Toga will ignore this.

Steps to reproduce

For the system, configure application storage with values other than the default and run a Toga app that creates filesystem content.

Expected behavior

Toga should respect local machine configuration for application storage. Given this configuration can be complex on any given platform, the platformdirs package provides nice abstractions. In the least, their logic should be used on Desktop, I think. Outside of Linux distros going rogue....this is also probably important for enterprise environments where IT wants apps to work in a certain way.

Screenshots

No response

Environment

  • Operating System: all
  • Python version: all
  • Software versions:
    • Toga: 0.4.7

Logs

No response

Additional context

I see that https://github.com/beeware/toga/pull/1964 confirms the same default behavior as platformdirs....but it seemed to have been a foregone conclusion that platformdirs would not become a dependency for Toga.

rmartin16 avatar Sep 22 '24 16:09 rmartin16

This would remove the need to give flatpaks whole file system access, making it a lot more secure.

MrStickyPiston avatar Sep 22 '24 16:09 MrStickyPiston

FWIW: The primary reason for avoiding a platformdirs dependency was that platformdirs didn't have any support for iOS or Android - and it was problematic to submit patches for those platforms, because iOS and Android weren't officially supported platforms.

Now that isn't the case, adding a platformdirs dependency would make more sense (assuming patches upstream have been submitted and accepted).

However, this bug notwithstanding, we've essentially got an implementation of platformdirs functionality, so I'm not sure we gain much by replacing a (mostly) working implementation with an external dependency.

So - my inclination would be to add the handling necessary to honour XDG variables natively to Toga, and leave it at that.

freakboy3742 avatar Sep 22 '24 18:09 freakboy3742

It may also be worth evaluating the situation on Windows; it can be a bit more complicated than even the XDG environment variables on Linux since the Windows registry is involved as well....and Windows is likely more relevant to enterprise.

rmartin16 avatar Sep 22 '24 19:09 rmartin16

Hello I have gone through the issue and we have provided you the source file also for the app file storage. file storage.docx

This typically happens when the environment variables related to storage paths or services (e.g., local storage vs. cloud storage) are not being read or correctly utilized in the code. we have provided the steps for how to do the configuration for file storage properly detects and respects the local environment settings.

  • Check Environment Configuration:
  • Modify the Code to Respect Environment Variables
  • Conditional File Storage Logic

HARSHC1503 avatar Oct 06 '24 16:10 HARSHC1503

@HARSHC1503 Thanks, but a DOCX file isn't something we can use - if only because it's a format that can't be edited or opened from the GitHub interface.

If the document contains a text description, you can provide the text as a comment on this ticket. If it's a set of code changes, a pull request is the most useful way to contribute that change.

freakboy3742 avatar Oct 06 '24 23:10 freakboy3742

Its a js file for cloud storage and idk why its even here

MrStickyPiston avatar Oct 07 '24 07:10 MrStickyPiston

I'm working on this.

Update (2025-05-20): The issue itself will be easy – I've found the code that needs updating – but I've been working on getting Toga and Briefcase working (and the test suites passing) on Nix/NixOS. Once I do I can make a PR for that as well, which should be useful for people using Nix on any distro (and perhaps even macOS). As an aside, see this: https://github.com/NixOS/nixpkgs/issues/157525

cyphase avatar May 19 '25 18:05 cyphase

From @HARSHC1503's docx file:

Image
// fileStorageService.js
const fs = require('fs');
const path = require('path');
const { uploadToCloudStorage } = require('./cloudStorageService'); // cloud storage logic
const config = require('./config');
 
class FileStorageService {
  static async storeFile(file) {
    if (config.storageType === 'local') {
      return this.storeLocally(file);
    } else {
      return uploadToCloudStorage(file);
    }
  }
 
  static async storeLocally(file) {
    const localStoragePath = config.localStoragePath;
    
    // Ensure the directory exists
    if (!fs.existsSync(localStoragePath)) {
      fs.mkdirSync(localStoragePath, { recursive: true });
    }
 
    // Define the local path for storing the file
    const filePath = path.join(localStoragePath, file.name);
 
    // Write file to local storage
    await fs.promises.writeFile(filePath, file.data);
 
    return {
      message: 'File stored locally',
      path: filePath,
    };
  }
}
 
module.exports = FileStorageService;
 

Not sure whta it's supposed to do though... I'll leave that as a question for the original poster to answer if applicable.

johnzhou721 avatar Oct 18 '25 17:10 johnzhou721