#Can't get Tauri filesystem to detect my public directory

8 messages · Page 1 of 1 (latest)

brazen ocean
#

I am using Next.js (TS) + Tauri v2

It looks for files in a different path
main/src-tauri/target/debug/stuff2
instead of main/public/stuff2

src-tauri/tauri.conf.json

{
  "$schema": "https://schema.tauri.app/config/2",
  "productName": "main",
  "version": "0.1.0",
  "identifier": "com.main.app",
  "build": {
    "beforeDevCommand": "npm run dev",
    "devUrl": "http://localhost:3000",
    "beforeBuildCommand": "npm run build",
    "frontendDist": "../out"
  },
  "app": {
    "withGlobalTauri": true,
    "windows": [
      {
        "title": "main",
        "width": 1280,
        "height": 720,
        "maximized": true
      }
    ],
    "security": {
      "csp": null
    }
  },
  "bundle": {
    "active": true,
    "targets": "all",
    "icon": [
      "icons/32x32.png",
      "icons/128x128.png",
      "icons/[email protected]",
      "icons/icon.icns",
      "icons/icon.ico"
    ],
    "resources": [
      "../public/stuff1/*",
      "../public/stuff2/**/*"
    ]
  }
}

src/tauri/capabilities/default.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "default",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": [
    "core:default",
    "shell:allow-open",
    
    "fs:allow-exists",
    "fs:allow-read-file",
    "fs:allow-write-file",
    "fs:allow-read-dir",
    
    {
      "identifier": "fs:scope",
      "allow": [
        { "path": "$APPDATA" },
        { "path": "$APPDATA/**" },
        { "path": "$APPLOCALDATA" },
        { "path": "$APPLOCALDATA/**" },
        { "path": "$APPCONFIG" },
        { "path": "$APPCONFIG/**" },
        { "path": "$RESOURCE" },
        { "path": "$RESOURCE/**" }
      ]
    }
  ]
}
#

src/utils/fileUtils.ts

import { readDir, readFile, BaseDirectory } from '@tauri-apps/plugin-fs';
import { Theme, Set } from '@/types/types';

export async function getThemes(): Promise<Theme[]> {
  const themesDir = 'stuff1';
  const entries = await readDir(themesDir, { baseDir: BaseDirectory.Resource });

  const themePromises = entries.map(async entry => {
    const ext = entry.name.split('.').pop()?.toLowerCase();
    const name = entry.name.replace(`.${ext}`, '');

    if (ext === 'json') {
      const fileContent = await readFile(`${themesDir}/${entry.name}`, { baseDir: BaseDirectory.Resource });
      const content = JSON.parse(new TextDecoder().decode(fileContent));
      return { name, type: 'color', ...content };
    } else {
      return { name, type: 'image', imageUrl: `/stuff1/${entry.name}` };
    }
  });

  return Promise.all(themePromises);
}

export async function getSets(): Promise<Set[]> {
  const setDir = 'stuff2';
  const entries = await readDir(setDir, { baseDir: BaseDirectory.Resource });

  return entries.map(dir => {
    const hasSvg = (dir as any).files?.some((file: any) => file.name.endsWith('.svg'));
    return { name: dir.name, type: hasSvg ? 'svg' : 'png' as 'png' | 'svg' };
  });
} 
#

Also, using BaseDirectory.App throws an error in code "Property 'App' does not exist on type 'typeof BaseDirectory'.ts(2339)"

candid shore
#

It looks for files in a different path
main/src-tauri/target/debug/stuff2
instead of main/public/stuff2
This is correct, resources get copied into the target dir. ../ get's converted to _up_/

#

Also, using BaseDirectory.App throws an error in code "Property 'App' does not exist on type 'typeof BaseDirectory'.ts(2339)"
Well yeah, App indeed does not exist. It did in v1 but it was an alias for AppConfig.

brazen ocean
candid shore
#

Well, from tauri's POV it is using the right path. Also, have in mind that the public path doesn't exist after building your app, which is part of the reason why it also behaves like this in dev.

In this case you could try using resolveResource("../public/") as your base path (remove the baseDir config then)

#

If that doesn't work then we're back to the _up_ stuff from above: const themesDir = '_up_/public/stuff1';