@setoelkahfi / svara / commits / 61ad854

feat: handling of unsupported file types

mellbacon committed Nov 3, 2023 at 19:35 UTC 61ad854cf06a0b960598ba557b4b3f37e792c6de
5 files changed +87 -3
src-tauri/Cargo.lock
+8 -1
@@ -1301,6 +1301,12 @@ dependencies = [
1301 "serde",
1302 ]
1303
1304 +[[package]]
1305 +name = "infer"
1306 +version = "0.3.7"
1307 +source = "registry+https://github.com/rust-lang/crates.io-index"
1308 +checksum = "865e8a58ae8e24d2c4412c31344afa1d302a3740ad67528c10f50d6876cdcf55"
1309 +
1310 [[package]]
1311 name = "infer"
1312 version = "0.12.0"
@@ -1675,6 +1681,7 @@ name = "nucleus"
1681 version = "0.3.0"
1682 dependencies = [
1683 "encoding_rs",
1684 + "infer 0.3.7",
1685 "log",
1686 "serde",
1687 "serde_json",
@@ -2946,7 +2953,7 @@ dependencies = [
2953 "glob",
2954 "heck 0.4.1",
2955 "html5ever",
2949 - "infer",
2956 + "infer 0.12.0",
2957 "json-patch",
2958 "kuchiki",
2959 "memchr",
src-tauri/Cargo.toml
+1
@@ -23,6 +23,7 @@ encoding_rs = "0.8.32"
23 tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
24 time = "0.3.28"
25 log = "^0.4"
26 +infer = "0.3"
27
28 [dev-dependencies]
29 windows = "0.32.0"
src-tauri/src/main.rs
+19 -1
@@ -187,6 +187,23 @@ fn write_file(path: &str, content: &str, enc: &str, has_bom: bool) {
187 }
188 }
189
190 +#[tauri::command]
191 +fn is_supported(path: &str) -> bool {
192 + match infer::get_from_path(path) {
193 + Ok(Some(info)) => {
194 + info!("File type detected: {}. Must be binary.", info.mime_type());
195 + false
196 + }
197 + Ok(None) => {
198 + true
199 + }
200 + Err(e) => {
201 + error!("{}", e);
202 + false
203 + }
204 + }
205 +}
206 +
207 fn configure_log() -> TauriPlugin<Wry> {
208 tauri_plugin_log::Builder::default()
209 .format(move |out, message, record| {
@@ -307,7 +324,8 @@ fn main() {
324 is_folder,
325 read_file,
326 write_file,
310 - open_in_default
327 + open_in_default,
328 + is_supported
329 ])
330 .plugin(tauri_plugin_fs_watch::init())
331 .plugin(tauri_plugin_store::Builder::default().build())
src/lib/EditorTabList.svelte
+7 -1
@@ -2,10 +2,12 @@
2 import VerticalDots from "carbon-icons-svelte/lib/OverflowMenuVertical.svelte";
3 import Dropdown from "./utility/Dropdown.svelte";
4 import TabList from "./Tab/TabList.svelte";
5 + import NotSupported from "./utility/NotSupported.svelte";
6 import { Tab } from "./Tab/Tab";
7 import { afterUpdate, onMount } from "svelte";
8 import { appWindow } from "@tauri-apps/api/window";
9 import { writable } from "svelte/store";
10 + import { invoke } from "@tauri-apps/api";
11
12 onMount(() => {
13 appWindow.onResized(() => {
@@ -60,7 +62,11 @@
62 export function addTab(path?: string, label?: string, content = null) {
63 editorTab.addTab(path, label, content);
64 }
63 - export function addEditorTab(path?: string, label?: string) {
65 + export async function addEditorTab(path?: string, label?: string) {
66 + if (path && !await invoke("is_supported", {path: path})) {
67 + addTab(path, label, new NotSupported({target: document.getElementById("tabview"), props: {path: path}}));
68 + return;
69 + }
70 editorTab.addEditorTab(path, label);
71 }
72 export async function closeTab(tabid: number) {
src/lib/utility/NotSupported.svelte new
+52
@@ -0,0 +1,52 @@
1 +<script lang="ts">
2 + import { invoke } from "@tauri-apps/api";
3 + import EditOff from "carbon-icons-svelte/lib/EditOff.svelte";
4 + export let hidden = false;
5 + export let path = "";
6 +</script>
7 +
8 +<div class="container" class:hidden>
9 + <div class="unsupported">
10 + <EditOff size={32} />
11 + <span class="desc">The file cannot be displayed because it is either binary or uses an unsupported text encoding.</span>
12 + <span class="link" on:click={() => {invoke("open_in_explorer", {path: path})}}>Open in File Explorer</span>
13 + </div>
14 +</div>
15 +
16 +<style lang="scss">
17 + .container {
18 + background-color: #171717;
19 + height: 100%;
20 + display: flex;
21 + .unsupported {
22 + display: flex;
23 + flex-direction: column;
24 + align-items: center;
25 + justify-content: center;
26 + height: 60%;
27 + width: 100%;
28 + :global(svg) {
29 + width: 48px;
30 + height: 48px;
31 + color: #376ba3;
32 + padding: 20px 0;
33 + }
34 + .desc {
35 + color: #939393;
36 + }
37 + .link {
38 + font-size: 0.9rem;
39 + color: #939393;
40 + cursor: pointer;
41 + text-decoration: underline;
42 + &:hover {
43 + color: #4d73ad;
44 + }
45 + padding: 5px 0;
46 + }
47 + }
48 + }
49 + .hidden {
50 + display: none !important;
51 + }
52 +</style>
\ No newline at end of file