Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' of https://github.com/hexahigh/yapc
Browse files Browse the repository at this point in the history
  • Loading branch information
hexahigh committed Feb 29, 2024
2 parents 0efc751 + f77cb7d commit ab13c27
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Go report card](https://goreportcard.com/badge/github.com/hexahigh/yapc/backend)](https://goreportcard.com/report/github.com/hexahigh/yapc/backend)
[![Api docs](https://img.shields.io/badge/Api_docs-gray?style=flat&logo=swagger
)](https://pomf-api.080609.xyz/)
![Maintained](https://img.shields.io/badge/status-maintained-lime.svg)<br>
![Maintained](https://img.shields.io/badge/status-maintained-green.svg)<br>
Yapc is yet another file sharing site inspired by pomf.
The frontend is written using svelte and tailwind and the backend is written using Go.
It is simplistic by design and is designed to be fast, easy to use and easy to maintain.
Expand Down
47 changes: 25 additions & 22 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"hash/crc32"
"hash/crc64"
"io"
"io/fs"
"log"
Expand All @@ -28,20 +29,21 @@ import (
"github.com/klauspost/compress/zstd"
)

const version = "2.2.0"
const version = "2.3.0"

var (
dataDir = flag.String("d", "./data", "Folder to store files")
port = flag.Int("p", 8080, "Port to listen on")
compress = flag.Bool("c", false, "Enable compression")
level = flag.Int("l", 3, "Compression level")
dbType = flag.String("db", "sqlite", "Database type (sqlite or mysql)")
dbPass = flag.String("db:pass", "", "Database password (Unused for sqlite)")
dbUser = flag.String("db:user", "root", "Database user (Unused for sqlite)")
dbHost = flag.String("db:host", "localhost:3306", "Database host (Unused for sqlite)")
dbDb = flag.String("db:db", "yapc", "Database name (Unused for sqlite)")
dbFile = flag.String("db:file", "./data/yapc.db", "SQLite database file")
cleanDb = flag.Bool("cleandb", false, "Clean the database")
dataDir = flag.String("d", "./data", "Folder to store files")
port = flag.Int("p", 8080, "Port to listen on")
compress = flag.Bool("c", false, "Enable compression")
level = flag.Int("l", 3, "Compression level")
dbType = flag.String("db", "sqlite", "Database type (sqlite or mysql)")
dbPass = flag.String("db:pass", "", "Database password (Unused for sqlite)")
dbUser = flag.String("db:user", "root", "Database user (Unused for sqlite)")
dbHost = flag.String("db:host", "localhost:3306", "Database host (Unused for sqlite)")
dbDb = flag.String("db:db", "yapc", "Database name (Unused for sqlite)")
dbFile = flag.String("db:file", "./data/yapc.db", "SQLite database file")
fixDb = flag.Bool("fixdb", false, "Fix the database")
fixDb_dry = flag.Bool("fixdb:dry", false, "Dry run fixdb")
)

var downloadSpeeds []float64
Expand Down Expand Up @@ -74,8 +76,8 @@ func main() {
os.Exit(1)
}

if *cleanDb {
dbClean()
if *fixDb {
dbFixer()
}

onStart()
Expand Down Expand Up @@ -568,9 +570,9 @@ func handleShorten(w http.ResponseWriter, r *http.Request) {
return
}

hasher := crc32.NewIEEE()
hasher := crc64.New(crc64.MakeTable(crc64.ISO))
hasher.Write([]byte(request.URL))
id := fmt.Sprintf("%x", hasher.Sum32())
id := fmt.Sprintf("%x", hasher.Sum64())

// Check if the Url is valid
if len(request.URL) > 2048 {
Expand Down Expand Up @@ -670,7 +672,7 @@ func handlePing(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}

func dbClean() {
func dbFixer() {
log.Println("Cleaning database")

log.Println("Looking for missing files...")
Expand Down Expand Up @@ -698,12 +700,13 @@ func dbClean() {
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
// If the file does not exist, delete the entry from the database
_, err := db.Exec("DELETE FROM data WHERE id = ?", id)
if err != nil {
log.Printf("Failed to delete entry with ID %s: %v", id, err)
} else {
log.Printf("Deleted entry with ID %s because the file does not exist", id)
if !*fixDb_dry {
_, err := db.Exec("DELETE FROM data WHERE id = ?", id)
if err != nil {
log.Printf("Failed to delete entry with ID %s: %v", id, err)
}
}
log.Printf("Deleted entry with ID %s because the file does not exist", id)
}
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@sveltejs/adapter-cloudflare": "^3.0.1",
"axios": "^1.6.5",
"mime": "^4.0.1",
"pretty-bytes": "^6.1.1"
"pretty-bytes": "^6.1.1",
"svelte-dnd-action": "^0.9.40"
}
}
11 changes: 11 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 28 additions & 7 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,26 @@
await getStats();
});
function handleDragOver(event) {
event.preventDefault(); // Prevent default browser behavior like opening the file
event.dataTransfer.dropEffect = 'copy'; // Indicate allowed drop effect
}
function handleDrop(event) {
event.preventDefault();
const droppedFiles = event.dataTransfer.files;
files = [...droppedFiles];
handleSubmit();
}
async function handleSubmit(event) {
uploadCount = 0;
status = 'Uploading...';
uploadProgress = 0;
errorMessage = '';
event.preventDefault();
if (event) {
event.preventDefault();
}
for (let i = 0; i < files.length; i++) {
let filename = files[i].name || 'file.bin';
Expand All @@ -136,7 +150,7 @@
uploadCount++;
status = `Uploaded ${uploadCount}/${files.length} files. You can download the latest file from the link below:`;
let link;
if (!direct) {
if (useS256) {
// Use SHA256 hash
Expand All @@ -156,10 +170,10 @@
}
if (shortenUrl) {
link = await shortenLink(link);
} else {
shortenLink(link);
}
link = await shortenLink(link);
} else {
shortenLink(link);
}
if (doArchive) {
archive(link);
Expand Down Expand Up @@ -319,7 +333,14 @@
<form on:submit={handleSubmit} class="p-6 mt-10 rounded shadow-md shadow-white w-80">
<div class="flex flex-col">
<label for="file" class="mb-2 font-bold text-lg">Upload Files</label>
<input id="file" type="file" bind:files multiple required class="p-2 border rounded-md" />
<input id="file" type="file" bind:files multiple class="p-2 border rounded-md" />
<div
on:dragover={handleDragOver}
on:drop={handleDrop}
class="mt-4 border dashed border-gray-400 rounded p-4 text-center hover:bg-gray-100"
>
Drag and drop files here
</div>
</div>
<button
type="submit"
Expand Down

0 comments on commit ab13c27

Please sign in to comment.