Skip to content

Commit 5a2846a

Browse files
committed
Add audio player and update media types
1 parent f2d1848 commit 5a2846a

10 files changed

+180
-97
lines changed

src/backend/electron.cjs

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const dev = !app.isPackaged
2727
const { loadHugin, loadWallet } = require('./wallet.cjs')
2828
const { Hugin } = require('./account.cjs')
2929
const { Storage } = require('./storage.cjs')
30+
const { check_if_media } = require('./utils.cjs')
3031

3132
let mainWindow
3233

@@ -61,9 +62,9 @@ function createWindow() {
6162
minWidth: 1100,
6263
transparent: true,
6364
webPreferences: {
64-
enableRemoteModule: true,
65+
enableRemoteModule: false,
6566
contextIsolation: true,
66-
nodeIntegration: true,
67+
nodeIntegration: false,
6768
spellcheck: false,
6869
devTools: dev,
6970
preload: path.join(__dirname, 'preload.cjs'),
@@ -303,7 +304,12 @@ const startCheck = async () => {
303304
}
304305

305306
ipcMain.handle('load-stored-file', async (e, hash, topic) => {
306-
return await Storage.load(hash, topic)
307+
const file = await Storage.load(hash, topic)
308+
const info = Hugin.get_files().find(a => a.hash === hash)
309+
if (!info) return
310+
const [media ,type] = check_if_media(info.fileName, info.size, true)
311+
return [file, type]
312+
307313
})
308314

309315
async function shareScreen(start, conference) {

src/backend/storage.cjs

+27-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@ const Corestore = require('corestore')
33
const Hyperdrive = require('hyperdrive')
44
const fs = require('fs');
55
const { app } = require('electron');
6-
const MEDIA_TYPES = ['.png','.jpg','.gif', '.jpeg', '.jfif', '.mp4', '.webm', '.avi', '.webp', '.mov','.wmv', '.mkv', '.mpeg']
7-
const { hash } = require('hyperdht');
6+
const MEDIA_TYPES = [
7+
{ file: '.png', type: 'image' },
8+
{ file: '.jpg', type: 'image' },
9+
{ file: '.gif', type: 'image' },
10+
{ file: '.jfif', type: 'image' },
11+
{ file: '.jpeg', type: 'image' },
12+
{ file: '.mp4', type: 'video' },
13+
{ file: '.webm', type: 'video' },
14+
{ file: '.avi', type: 'video' },
15+
{ file: '.webp', type: 'video' },
16+
{ file: '.mov', type: 'video' },
17+
{ file: '.wmv', type: 'video' },
18+
{ file: '.mkv', type: 'video' },
19+
{ file: '.mpeg', type: 'video' },
20+
{ file: '.m4a', type: 'audio' },
21+
{ file: '.mp3', type: 'audio' },
22+
{ file: '.wav', type: 'audio' },
23+
];
824
const { get_new_peer_keys } = require('./crypto.cjs');
925
const userDataDir = app.getPath('userData')
1026
const Huginbeam = require("huginbeam");
@@ -83,7 +99,8 @@ async save(topic, address, name ,hash, size, time, fileName, path, signature, in
8399
console.log("****Save file to drive****")
84100
if (this.saved > this.limit) return
85101
if (downloaded) {
86-
if (!this.check(size, downloaded, fileName)) return
102+
const [media, type] = this.check(size, downloaded, fileName)
103+
if (!media) return
87104
}
88105
this.saved = this.saved + size
89106
console.log("Saved thus far:", this.saved)
@@ -93,7 +110,6 @@ async save(topic, address, name ,hash, size, time, fileName, path, signature, in
93110
if (!downloaded) {
94111
buf = await this.read(path)
95112
} else buf = downloaded
96-
97113
if (!buf) return
98114
await drive.put(hash, buf, {metadata: {name ,topic, time, size, hash, fileName, address, signature, info, type}})
99115
Hugin.file_info({fileName, time, size, path, hash, topic})
@@ -113,15 +129,16 @@ async read(path) {
113129
});
114130
}
115131

132+
116133
check(size, buf, name) {
117-
if (buf.length > size) return false
118-
if (size > this.limit) return false
134+
if (buf.length > size) return false;
135+
if (size > this.limit) return false;
119136
for (const a of MEDIA_TYPES) {
120-
if (name.toLowerCase().endsWith(a)) {
121-
return true
122-
}
137+
if (name.toLowerCase().endsWith(a.file)) {
138+
return [true, a.type];
139+
}
123140
}
124-
return false
141+
return [false];
125142
}
126143

127144
async start_beam(upload, key, file, topic, room) {

src/backend/swarm.cjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const HyperSwarm = require("hyperswarm-hugin");
22

3-
const {sleep, sanitize_join_swarm_data, sanitize_voice_status_data, sanitize_file_message, sanitize_group_message, check_hash, toHex, randomKey, check_if_image_or_video} = require('./utils.cjs');
3+
const {sleep, sanitize_join_swarm_data, sanitize_voice_status_data, sanitize_file_message, sanitize_group_message, check_hash, toHex, randomKey, check_if_media} = require('./utils.cjs');
44
const {saveGroupMsg, getChannels, loadRoomKeys, removeRoom, printGroup, groupMessageExists, getLatestRoomHashes, roomMessageExists, getGroupReply} = require("./database.cjs")
55
const { app,
66
ipcMain
@@ -927,8 +927,8 @@ const check_file_message = async (data, topic, address, con) => {
927927
const active = get_active_topic(topic)
928928
if (!active) return
929929
if (data.info === 'file-shared') {
930-
931-
if (check_if_image_or_video(data.fileName, data.size, true) && Hugin.syncImages.some(a => a === topic)) {
930+
const [media, type] = check_if_media(data.fileName, data.size, true)
931+
if (media && Hugin.syncImages.some(a => a === topic)) {
932932
//A file is shared and we have auto sync images on.
933933
//Request to download the file
934934
const file = {

src/backend/utils.cjs

+32-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@ const { Crypto, Keys } = require('kryptokrona-utils')
44
const {ipcMain, dialog} = require('electron')
55
const crypto = new Crypto()
66
const {createReadStream} = require("fs");
7-
const MEDIA_TYPES = ['.png','.jpg','.gif', '.jpeg', '.jfif', '.mp4', '.webm', '.avi', '.webp', '.mov','.wmv', '.mkv', '.mpeg']
7+
const MEDIA_TYPES = [
8+
{ file: '.png', type: 'image' },
9+
{ file: '.jpg', type: 'image' },
10+
{ file: '.gif', type: 'image' },
11+
{ file: '.jfif', type: 'image' },
12+
{ file: '.jpeg', type: 'image' },
13+
{ file: '.mp4', type: 'video' },
14+
{ file: '.webm', type: 'video' },
15+
{ file: '.avi', type: 'video' },
16+
{ file: '.webp', type: 'video' },
17+
{ file: '.mov', type: 'video' },
18+
{ file: '.wmv', type: 'video' },
19+
{ file: '.mkv', type: 'video' },
20+
{ file: '.mpeg', type: 'video' },
21+
{ file: '.m4a', type: 'audio' },
22+
{ file: '.mp3', type: 'audio' },
23+
{ file: '.wav', type: 'audio' },
24+
];
825

926
ipcMain.handle('load-file', async (e, path, size) => {
1027
return await load_file(path, size)
@@ -20,21 +37,21 @@ ipcMain.handle('select-directory', () => {
2037

2138

2239
//Check if it is an image or video with allowed type
23-
function check_if_image_or_video(path, size, check = false) {
40+
function check_if_media(path, size, check = false) {
2441
if (path === undefined && !check) return false
25-
if (size >= 50000000) return false
26-
const types = MEDIA_TYPES
27-
for (a in types) {
28-
if (path.toLowerCase().endsWith(types[a])) {
29-
return true
42+
if (size >= 10000000) return false
43+
for (const a of MEDIA_TYPES) {
44+
if (path.toLowerCase().endsWith(a.file)) {
45+
return [true, a.type];
46+
}
3047
}
31-
}
32-
return false
48+
return [false];
3349
}
3450

3551
async function load_file(path, size) {
3652
let imgArray = []
37-
if (check_if_image_or_video(path, size)) {
53+
const [media, type] = check_if_media(path, size)
54+
if (media) {
3855
//Read the file as an image
3956
return new Promise((resolve, reject) => {
4057
try {
@@ -44,18 +61,18 @@ async function load_file(path, size) {
4461
imgArray.push(data)
4562
})
4663
stream.on('error', (data) => {
47-
return "File not found"
64+
return ["File not found"]
4865
})
4966

5067
stream.on('end', () => {
51-
resolve(Buffer.concat(imgArray))
68+
resolve([Buffer.concat(imgArray), type])
5269
})
5370
} catch {
54-
return "File not found"
71+
return ["File not found"]
5572
}
5673
})
5774
} else {
58-
return "File"
75+
return ["File"]
5976
}
6077
}
6178

@@ -398,4 +415,4 @@ const sanitize_join_swarm_data = (data) => {
398415

399416

400417

401-
module.exports = {sleep, check_hash, trimExtra, fromHex, nonceFromTimestamp, randomKey, hexToUint, toHex, parse_call, sanitize_join_swarm_data, sanitize_voice_status_data, hash, sanitize_pm_message, sanitize_file_message, sanitize_group_message, check_if_image_or_video, MEDIA_TYPES}
418+
module.exports = {sleep, check_hash, trimExtra, fromHex, nonceFromTimestamp, randomKey, hexToUint, toHex, parse_call, sanitize_join_swarm_data, sanitize_voice_status_data, hash, sanitize_pm_message, sanitize_file_message, sanitize_group_message, check_if_media, MEDIA_TYPES}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
<script>
3+
export let src
4+
5+
</script>
6+
7+
<div>
8+
<audio
9+
{src}
10+
controls>
11+
<track />
12+
</audio>
13+
</div>

src/lib/components/chat/DownloadFile.svelte

+46-30
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@
77
import { groups, user } from '$lib/stores/user.js'
88
import Progress from "$lib/components/chat/Progress.svelte"
99
import { sleep } from "$lib/utils/utils"
10+
import AudioPlayer from "./AudioPlayer.svelte"
1011
1112
export let file
1213
export let group = false
1314
export let rtc = false
15+
1416
15-
let video = false
16-
let videoTypes = ['.mp4', '.webm', '.avi', '.mov','.wmv', '.mkv', '.mpeg']
1717
let downloadDone = false
1818
let downloading = false
19-
let thisFile
2019
let clicked = false
2120
let downloaders = []
22-
onMount(() => {
23-
if (group) return
24-
if (videoTypes.some(a => file.fileName.endsWith(a) && file.size < 50000000))
25-
{
26-
video = true
27-
return
28-
}
21+
22+
let video = false
23+
let audio = false
24+
let image = false
25+
let data
26+
27+
const NOT_FOUND = "File not found"
28+
const OTHER = "File"
29+
30+
31+
onMount(async () => {
32+
await loadFile(file)
2933
})
3034
3135
$: {
@@ -35,16 +39,26 @@
3539
3640
3741
$: if (downloadDone) {
38-
if (!video) awaitLoad(file)
42+
awaitLoad(file)
3943
}
4044
4145
async function awaitLoad(file) {
42-
await sleep(200)
43-
loadFile(file)
46+
await sleep(100)
47+
await loadFile(file)
4448
}
4549
4650
$: if (file?.saved) {
47-
if (!video) loadFile(file)
51+
awaitLoad(file)
52+
}
53+
54+
const checkType = (type) => {
55+
switch (type){
56+
case 'audio': audio = true
57+
break;
58+
case 'video': video = true
59+
break;
60+
case 'image': image = true
61+
}
4862
}
4963
5064
const focusImage = (image) => {
@@ -56,14 +70,18 @@
5670
}
5771
5872
async function loadFile(file) {
59-
let arr
73+
let load = []
6074
if (file.path === "storage") {
61-
arr = await window.api.loadStoredFile(file.hash, file.topic)
62-
} else arr = await window.api.loadFile(file.path, file.size)
63-
if (arr === "File" || arr === "File not found") return arr
75+
load = await window.api.loadStoredFile(file.hash, file.topic)
76+
} else {
77+
load = await window.api.loadFile(file.path, file.size)
78+
}
79+
if (load[0] === OTHER || load[0] === NOT_FOUND) return false
80+
const [arr, type] = load
6481
let blob = new Blob( [ arr ]);
65-
let imageUrl = URL.createObjectURL( blob );
66-
thisFile = imageUrl
82+
data = URL.createObjectURL( blob );
83+
console.log("Load ", data)
84+
checkType(type)
6785
}
6886
6987
const downloadFile = (file) => {
@@ -92,29 +110,27 @@
92110
<div in:fade>
93111
<Progress file={file} send={false}/>
94112
</div>
95-
<!-- {:else if downloadDone && group}
96-
<div style="cursor: pointer" in:fade on:click={() => window.api.openFolder()}>
97-
<Progress file={file} send={false}/>
98-
</div>
99-
<p class="message done">File downloaded!</p> -->
100113
101114
{:else if downloadDone || file?.saved}
102115
{#if !video}
103-
{#if thisFile === "File" && !file?.saved}
116+
{#if data === OTHER && !file?.saved}
104117
<div in:fade>
105118
<Progress file={file} send={false}/>
106119
</div>
107120
<p class="message done">File downloaded!</p>
108-
{:else if thisFile === "File not found"}
109-
<p class="message error">File not found</p>
110-
{:else}
121+
<p>{file.fileName}</p>
122+
{:else if data === NOT_FOUND}
123+
<p class="message error">{NOT_FOUND}</p>
124+
{:else if image}
111125
<div style="-webkit-user-drag: none;" on:click={focusImage}>
112126
<img
113127
in:fade="{{ duration: 150 }}"
114-
src="{thisFile}"
128+
src="{data}"
115129
alt=""
116130
/>
117131
</div>
132+
{:else if audio}
133+
<AudioPlayer src={data} />
118134
{/if}
119135
{:else if video}
120136
<VideoPlayer src={file} />

0 commit comments

Comments
 (0)