Bundle Files with Compose for Desktop - kotlin

I am an Android developer, I have experience with Jetpack Compose. Now I am trying to build desktop apps, so I found this Compose for Desktop project. I want to store a JSON file with my desktop app so that I can read the file from my desktop app and show it to users.
In Android, I could use the raw or assets folders, but I don't know how to do that in Compose for Desktop.
If someone can point me in the right direction, I will appreciate it.
Edit
Not only JSON files, but I also want to store some other files like HTML, so I really need a way to store files within the application.

You can put any assets files inside src\jvmMain\resources access them using useResource.
for example to read json file as text:
useResource("data.json") { stream ->
val textJson = stream.bufferedReader().use { it.readText() }
}

Related

Capacitor 3 and VueJS, open saved file (or download)

I am struggling with something I thought will be simple.
Ideally I want to save my base64 file with prompt same as you get when downloading from browser, however that seems impossible with capacitor app.
So I used FileSystem, to save file on Documents directory, which went fine, I also can get uri file. But because there is no indicator that file was saved, I would like to at least try to open the file with native apps. I've noticed there is cordova plugin cordova-plugin-file-opener2 but I can't seem to make it work with capacitor 3 and VueJs, without cordova.
Maybe I am overthinking it ? is there any way download file or open file with Capacitor?
For web view (not native app) I am using hidden download button, but that does not work at all in capacitor app
here is my current code
Filesystem.writeFile({
data: pdf,
path: `${this.fileName}.pdf`,
directory: Directory.Documents,
}).then(() => {
Filesystem.getUri({
path: `${this.fileName}.pdf`,
directory: Directory.Documents,
}).then(res => {
// open file here with my res.uri ?
);
});
I had the exakt same problem. I solved/workarounded it by using the official capacitor "Share" plugin, so the user can choose what she wants to do with the file. https://capacitorjs.com/docs/apis/share
I did not find any other solution to show or download those privately stored files. FileOpener seems to have a lot of problems on Android and does not work either on iOS.

export and maintain vue application

I have developed a vue application and did run npm run build
After that I uploaded the content in the dist file to my webpage but it returned a blank page.
Since I did this for testing I uploaded it to a folder in my public_html/mypage.com/vueapplication To get all the paths right I added a vue.config.js with this content:
// vue.config.js
module.exports = {
publicPath: '/vueapplication/'
}
The application now works but I wounder however:
how do I best publish/upload the application to my site? Just by simply dragging the content inte the right folder?
how can I best maintain my site? Do I need to build again and upload, overwriting my files when everytime I make an update on my site?
And what is the difference between build and deploy your application?
Drag and dropping your code should work. But as your app grows you may want to look into automating this. For instance if you use an S3 bucket you can use the aws cli to automate the upload.
Yes, you should overwrite your deploy folder(s). You need to also take care of deploying different binary files, that have the same name. An example is if you have a global css file (main.css for instance). The file will probably change content between deployments, but keep the same name. Browsers may cache the file so users that downloaded older versions of the file will not use the new one. There are different techniques to handle this, but if you use webpack, it uses cache busting techniques and you should be fine.
Build is the process of transforming source code into an artifact(s). Exactly what this means differs from language to language, platform to platform. In the vuejs world this usually means a couple of js files, a couple of css files and some assets.
Deploying means taking the output of a build and making it available to your users. Again this differs from project to project. In the vuejs world this usually means taking the artifacts from the build and uploading them to an http enabled web server.

NWJS access external files on FlashDrive

I have a very particular case and I don't know if this is possible to be done.
I'm using NWJS to run a web app as a desktop app. I need to zip/package the source files because my code should not be available to eavesdroppers. This package will be delivered on a flash drive. And this is were my trouble begins.
There are a lot of .pdf file that must be shipped together with the package. The user can browse which pdf he wants to open, and when he clicks it, the pdf is "downloaded" to his pc. The content of the pdf is NOT available on the application. I have a list with the name of each pdf file.
If I zip/package the .pdf together with the source files it becomes a huge .nw file and it takes forever for my application to load. I need to mantain the pdf on a separate folder and they need to be accessible through the source code. This is easy if i run the application directly without packaging it, as nw uses the relative url to it's root, but when I do package nw uses a temp folder for the source files and I can't use relative url to access the pdf folder.
The only approach i can think of is to write a js script to identify where the flash drive was mounted but i don't know if this is possible.
I have to support Windows and Mac for this case.
Searching on NWJS google group i found that the answer was quite simple. These two lines returns the path where the nw bin is running. From there is quite simple to get the pdf folder.
var path = require("path");
pathstr = path.dirname(process.execPath);

Extra files in Windows Store app package

just wondering if it's possible to include some files (one txt file in this case) in the app package that I need in the application folder. The thing is that I might use a piece of code that requires the license to be included in the app as a text file, and I think this would be one way to do it.
Thanks in advance.
Absolutely, it's really no different than including images for instance. And if you need to process the file within your app you can access it via its local path or explicitly use the ms-appx:/// protocol.
See How to reference content and How to load file resources for more details.
Just include the file in your project with Build Action set to Content. You can put it in any folder you like.
The file can then be accessed from the app either using the ms-appx: protocol or using the StorageFolder API:
var license = await Package.Current.InstalledLocation.GetFileAsync("license.txt");

Windows 8 metro application access arbitrary file path

In metro, the codes like following will throw exception:
String fileName = #"C:\Test\dd\ccc.jpg";
StorageFile file = await StorageFile.GetFileFromPathAsync(fileName);
However even if I check everything in capabilities, also File Picker was added and all file types allowed. I still can't access this file, the same exception will be thrown.
Does someone know how to read file in arbitrary file path? Is that possible in metro style application.
Not possible. You can get to the Libraries - pictures, documents, videos - and if the user puts that folder into one of those libraries (using Windows Explorer on the desktop side) you're all set. You can even write a desktop exe that will put the folder into the library, but you can't launch that exe yourself or be sure that the user hasn't changed the libraries by hand.
Look up SHCreateItemInKnownFolder for a starting point to the shell APIs for library work. I haven't tried calling those APIs from the Metro side; you can see if they help but my bet is they will not be available. If you don't like the COM interop to the shell APIs you could look at the source code to the Windows API Code Pack - I wouldn't want to bundle all of it with a Metro app, but you could copy parts of it to your application.