Skip to content

Commit a73c0dc

Browse files
committed
added database functionality
1 parent 34f4eeb commit a73c0dc

4 files changed

Lines changed: 155 additions & 2 deletions

File tree

src/main/db.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const sqlite3 = require('sqlite3')
2+
sqlite3.verbose()
3+
const db = new sqlite3.Database('history.db')
4+
5+
// Create table if not exists
6+
db.serialize(() => {
7+
db.run(`CREATE TABLE IF NOT EXISTS conversions (
8+
id INTEGER PRIMARY KEY AUTOINCREMENT,
9+
filename TEXT NOT NULL,
10+
format TEXT NOT NULL,
11+
resolution TEXT,
12+
size INTEGER,
13+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
14+
)`)
15+
})
16+
17+
export const saveConversion = (filename, format, resolution = null, size = null) => {
18+
return new Promise((resolve, reject) => {
19+
const stmt = db.prepare(`
20+
INSERT INTO conversions (filename, format, resolution, size)
21+
VALUES (?, ?, ?, ?)
22+
`)
23+
24+
stmt.run([filename, format, resolution, size], function(err) {
25+
if (err) {
26+
reject(err)
27+
return
28+
}
29+
resolve(this.lastID)
30+
})
31+
stmt.finalize()
32+
})
33+
}
34+
35+
export const getAllConversions = () => {
36+
return new Promise((resolve, reject) => {
37+
db.all('SELECT * FROM conversions ORDER BY created_at DESC', (err, rows) => {
38+
if (err) {
39+
reject(err)
40+
return
41+
}
42+
resolve(rows)
43+
})
44+
})
45+
}

src/main/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import sharp from 'sharp'
77
import fs from 'fs'
88
import path from 'path'
99
import { checkPluginInstalled, getSupportedFormats,documentConvert,isFormatSupported } from './initPlugin'
10+
import {db,saveConversion,getAllConversions} from './db'
1011
function createWindow() {
1112
const primaryDisplay = screen.getPrimaryDisplay();
1213
const {width,height} = primaryDisplay.workAreaSize
@@ -234,4 +235,28 @@ ipcMain.handle('plugin:uninstall', async (event,pluginId)=>{
234235
console.error('Failed to delete plugin: ',error)
235236
throw error
236237
}
238+
})
239+
//database operations
240+
ipcMain.handle('db:save-history',async(_,conversionData)=>{
241+
try {
242+
const id = await saveConversion(
243+
conversionData.filename,
244+
conversionData.format,
245+
conversionData.resolution || 'NaN',
246+
conversionData.size
247+
)
248+
return { success: true, id }
249+
} catch (error) {
250+
console.error('Error saving conversion:', error)
251+
return { success: false, error: error.message }
252+
}
253+
})
254+
ipcMain.handle('db:get-data',async ()=>{
255+
try {
256+
const conversions = await getAllConversions()
257+
return { success: true, conversions }
258+
} catch (error) {
259+
console.error('Error getting conversions:', error)
260+
return { success: false, error: error.message }
261+
}
237262
})
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
1+
<script setup>
2+
import { ref, onMounted } from 'vue'
3+
4+
const { ipcRenderer } = window.electron
5+
const conversions = ref([])
6+
const isLoading = ref(false)
7+
// Function to fetch history data
8+
const refreshHistory = async () => {
9+
isLoading.value = true
10+
try {
11+
const result = await ipcRenderer.invoke('db:get-data')
12+
if (result.success) {
13+
conversions.value = result.conversions
14+
} else {
15+
console.error('Failed to load conversions:', result.error)
16+
}
17+
} catch (error) {
18+
console.error('Error loading conversion history:', error)
19+
} finally {
20+
isLoading.value = false
21+
}
22+
}
23+
onMounted(async () => {
24+
await refreshHistory()
25+
})
26+
</script>
27+
128
<template>
2-
<img src="../assets/not-ready.svg" alt="">
3-
<h2 class="text-orange-600">History Under construction</h2>
29+
<div class="mt-2 flex justify-end">
30+
<button
31+
class="btn btn-sm btn-primary btn-outline"
32+
@click="refreshHistory"
33+
:disabled="isLoading"
34+
>
35+
{{ isLoading ? 'Loading...' : 'Refresh History' }}
36+
</button>
37+
</div>
38+
<div class="container p-4">
39+
<div v-if="isLoading" class="w-full">
40+
<div class="skeleton h-8 w-full mb-4"></div>
41+
<div class="skeleton h-8 w-full mb-2"></div>
42+
<div class="skeleton h-8 w-full mb-2"></div>
43+
<div class="skeleton h-8 w-full mb-2"></div>
44+
</div>
45+
46+
<div v-else class="overflow-x-auto rounded-box border border-base-content/5 bg-base-100 max-h-[calc(100vh-9rem)] overflow-y-auto">
47+
<table class="table w-full">
48+
<thead>
49+
<tr>
50+
<th>Filename</th>
51+
<th>Format</th>
52+
<th>Resolution</th>
53+
<th>Size</th>
54+
<th>Date</th>
55+
</tr>
56+
</thead>
57+
<tbody>
58+
<tr v-if="conversions.length === 0">
59+
<td colspan="5" class="text-center py-4">No conversion history found</td>
60+
</tr>
61+
<tr v-for="conv in conversions" :key="conv.id" class="hover:bg-base-200">
62+
<td>{{ conv.filename }}</td>
63+
<td>{{ conv.format?.toUpperCase() }}</td>
64+
<td>{{ conv.resolution || 'N/A' }}</td>
65+
<td>{{ conv.size }}</td>
66+
<td>{{ new Date(conv.created_at).toLocaleString() }}</td>
67+
</tr>
68+
</tbody>
69+
</table>
70+
</div>
71+
</div>
472
</template>

src/renderer/src/components/Upload.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ async function processFiles() {
207207
message: result.message,
208208
outputPath: result.outputPath
209209
})
210+
if (result.success) {
211+
await ipcRenderer.invoke('db:save-history', {
212+
filename: file.name,
213+
format: options.format,
214+
resolution: `${options.width}x${options.height}`,
215+
size: formatFileSize(file.size)
216+
})
217+
}
210218
} else {
211219
try {
212220
const r = await ipcRenderer.invoke('document:convert',{
@@ -223,6 +231,13 @@ async function processFiles() {
223231
message: r.message,
224232
outputPath: r.outputPath
225233
})
234+
if (r.success) {
235+
await ipcRenderer.invoke('db:save-history', {
236+
filename: file.name,
237+
format: fileOptions.value[i].format,
238+
size: formatFileSize(file.size)
239+
})
240+
}
226241
} catch (error) {
227242
processedFiles.value.push({
228243
name: file.name,

0 commit comments

Comments
 (0)