-
-
{fileName}
+export const FileSharing = () => {
+ const style = useStyle()
+ return
-);
-
-const MiddlePanel = () => (
-
-);
-
-const UploadPanel = () => {
- const [selectedFile, setSelectedFile] = useState
(null);
- const fileInputRef = useRef(null);
-
- const handleFileSelect = () => {
- if (!selectedFile && fileInputRef.current) {
- fileInputRef.current.click();
- } else {
- // Handle upload logic
- if (selectedFile) {
- const formData = new FormData();
- formData.append('file', selectedFile);
-
- // Replace with actual API call
- fetch('https://your-upload-api.com/upload', {
- method: 'POST',
- body: formData
- })
- .then(response => response.json())
- .then(data => {
- alert(`Upload success! Code: ${data.code}`);
- setSelectedFile(null);
- })
- .catch(() => alert('Upload failed'));
- }
- }
- };
-
- return (
-
-
-
上传文件
- e.target.files?.[0] && setSelectedFile(e.target.files[0])}
- />
-
- {selectedFile && (
- setSelectedFile(null)}
- />
- )}
-
-
- );
-};
-
-const DownloadPanel = () => {
- const [downloadCode, setDownloadCode] = useState('');
-
- const handleDownload = () => {
- if (!downloadCode) {
- alert('请输入下载码');
- return;
- }
- // Replace with actual download logic
- window.location.href = `https://your-download-api.com/download?code=${downloadCode}`;
- };
-
- return (
-
-
-
下载文件
- setDownloadCode(e.target.value)}
- />
-
-
-
- );
-};
-
-export const FileSharing = () => (
-
-
-
-
-
-);
\ No newline at end of file
+};
\ No newline at end of file
diff --git a/frontend/src/store/share.ts b/frontend/src/store/share.ts
new file mode 100644
index 0000000..e6498c3
--- /dev/null
+++ b/frontend/src/store/share.ts
@@ -0,0 +1,23 @@
+import {create} from 'zustand'
+
+type Store = {
+ file: File | null,
+ setFile: (f: File | null) => void
+ code: string,
+ setCode: (value:string) => void
+}
+
+export const useStore = create()((set) => ({
+ file: null,
+ setFile: (f: File | null = null) => {
+ set(state => {
+ return {...state, file: f}
+ })
+ },
+ code: '',
+ setCode: (value:string= '') => {
+ set(state => {
+ return {...state, code: value}
+ })
+ }
+}))
\ No newline at end of file
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index c4069b7..c755c1e 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -1,8 +1,15 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
-import tailwindcss from '@tailwindcss/vite'
// https://vite.dev/config/
export default defineConfig({
- plugins: [react(), tailwindcss()],
+ plugins: [react()],
+ server: {
+ proxy: {
+ '/api': {
+ target: 'http://127.0.0.1:9119',
+ changeOrigin: true
+ }
+ }
+ }
})
diff --git a/go.mod b/go.mod
index 7267e35..56935b0 100644
--- a/go.mod
+++ b/go.mod
@@ -2,7 +2,11 @@ module github.com/loveuer/ushare
go 1.24.2
-require github.com/loveuer/nf v0.3.5
+require (
+ github.com/loveuer/nf v0.3.5
+ github.com/pkg/errors v0.9.1
+ github.com/spf13/viper v1.20.1
+)
require (
dario.cat/mergo v1.0.0 // indirect
@@ -12,24 +16,38 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.17.0 // indirect
+ github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
+ github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
+ github.com/matoous/go-nanoid/v2 v2.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
+ github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
+ github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
+ github.com/sourcegraph/conc v0.3.0 // indirect
+ github.com/spf13/afero v1.12.0 // indirect
+ github.com/spf13/cast v1.7.1 // indirect
+ github.com/spf13/pflag v1.0.6 // indirect
+ github.com/subosito/gotenv v1.6.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
- golang.org/x/crypto v0.25.0 // indirect
+ go.uber.org/atomic v1.9.0 // indirect
+ go.uber.org/multierr v1.9.0 // indirect
+ golang.org/x/crypto v0.32.0 // indirect
golang.org/x/mod v0.17.0 // indirect
- golang.org/x/net v0.27.0 // indirect
- golang.org/x/sync v0.7.0 // indirect
- golang.org/x/sys v0.22.0 // indirect
+ golang.org/x/net v0.33.0 // indirect
+ golang.org/x/sync v0.10.0 // indirect
+ golang.org/x/sys v0.29.0 // indirect
+ golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
+ gopkg.in/yaml.v3 v3.0.1 // indirect
)
diff --git a/go.sum b/go.sum
index 93b0620..679111f 100644
--- a/go.sum
+++ b/go.sum
@@ -24,6 +24,10 @@ github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
+github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
+github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
+github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
+github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE=
github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
@@ -34,6 +38,8 @@ github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMj
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys=
github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY=
+github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
+github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -53,6 +59,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/loveuer/nf v0.3.5 h1:DlgTa6Rx8D3/VtH9e0fLGAxmPwSYd7b3nfBltSMuypE=
github.com/loveuer/nf v0.3.5/go.mod h1:IAq0K1c/mlNQzLBvUzAD1LCWiVlt2GqTMPdDjej3Ryo=
+github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE=
+github.com/matoous/go-nanoid/v2 v2.1.0/go.mod h1:KlbGNQ+FhrUNIHUxZdL63t7tl4LaPkZNpUULS8H4uVM=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
@@ -60,6 +68,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
+github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
+github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -68,26 +78,45 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
+github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
+github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
+github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
+github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
+github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
+github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
+github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
+github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
+github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
+github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
+github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
-github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
-github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
+github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
+github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
+go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
+go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
+go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
+go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
-golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
-golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
+golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
+golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
@@ -99,13 +128,13 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
-golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
-golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
+golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
+golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
-golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
-golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
+golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
+golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -120,15 +149,15 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
-golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
+golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
-golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
-golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
+golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
+golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@@ -136,8 +165,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
-golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
-golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
+golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
+golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
diff --git a/internal/api/api.go b/internal/api/api.go
index 42841d3..5d6d585 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -5,6 +5,7 @@ import (
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/log"
"github.com/loveuer/nf/nft/tool"
+ "github.com/loveuer/ushare/internal/handler"
"github.com/loveuer/ushare/internal/opt"
"net"
"net/http"
@@ -17,6 +18,10 @@ func Start(ctx context.Context) <-chan struct{} {
return c.SendStatus(http.StatusOK)
})
+ app.Get("/api/share/:code", handler.Fetch())
+ app.Put("/api/share/:filename", handler.ShareNew()) // 获取上传 code, 分片大小
+ app.Post("/api/share/:code", handler.ShareUpload()) // 分片上传接口
+
ready := make(chan struct{})
ln, err := net.Listen("tcp", opt.Cfg.Address)
if err != nil {
diff --git a/internal/controller/meta.go b/internal/controller/meta.go
new file mode 100644
index 0000000..8f3a70b
--- /dev/null
+++ b/internal/controller/meta.go
@@ -0,0 +1,122 @@
+package controller
+
+import (
+ "context"
+ "fmt"
+ "github.com/loveuer/nf/nft/log"
+ "github.com/loveuer/ushare/internal/opt"
+ gonanoid "github.com/matoous/go-nanoid/v2"
+ "io"
+ "os"
+ "sync"
+ "time"
+)
+
+type metaInfo struct {
+ f *os.File
+ name string
+ create time.Time
+ last time.Time
+ size int64
+ cursor int64
+ ip string
+}
+
+func (m *metaInfo) generateMeta(code string) error {
+ content := fmt.Sprintf("filename=%s\ncreated_at=%d\nsize=%d\nuploader_ip=%s",
+ m.name, m.create.UnixMilli(), m.size, m.ip,
+ )
+
+ return os.WriteFile(opt.MetaPath(code), []byte(content), 0644)
+}
+
+type meta struct {
+ sync.Mutex
+ ctx context.Context
+ m map[string]*metaInfo
+}
+
+var (
+ MetaManager = &meta{m: make(map[string]*metaInfo)}
+)
+
+const letters = "1234567890abcdefghijklmnopqrstuvwxyz"
+
+func (m *meta) New(size int64, filename, ip string) (string, error) {
+ now := time.Now()
+ code, err := gonanoid.Generate(letters, 16)
+ if err != nil {
+ return "", err
+ }
+
+ f, err := os.Create(opt.FilePath(code))
+ if err != nil {
+ return "", err
+ }
+
+ if err = f.Truncate(size); err != nil {
+ f.Close()
+ return "", err
+ }
+
+ m.Lock()
+ defer m.Unlock()
+
+ m.m[code] = &metaInfo{f: f, name: filename, last: now, size: size, cursor: 0, create: now, ip: ip}
+
+ return code, nil
+}
+
+func (m *meta) Write(code string, start, end int64, reader io.Reader) (total, cursor int64, err error) {
+ m.Lock()
+ defer m.Unlock()
+
+ if _, ok := m.m[code]; !ok {
+ return 0, 0, fmt.Errorf("code not exist")
+ }
+
+ w, err := io.CopyN(m.m[code].f, reader, end-start+1)
+ if err != nil {
+ return 0, 0, err
+ }
+
+ m.m[code].cursor += w
+ m.m[code].last = time.Now()
+
+ total = m.m[code].size
+ cursor = m.m[code].cursor
+
+ if m.m[code].cursor == m.m[code].size {
+ defer delete(m.m, code)
+ if err = m.m[code].generateMeta(code); err != nil {
+ return 0, 0, err
+ }
+ }
+
+ return total, cursor, nil
+}
+
+func (m *meta) Start(ctx context.Context) {
+ ticker := time.NewTicker(time.Minute)
+ m.ctx = ctx
+
+ go func() {
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case now := <-ticker.C:
+ for code, info := range m.m {
+ if now.Sub(info.last) > 1*time.Minute {
+ m.Lock()
+ info.f.Close()
+ os.RemoveAll(opt.FilePath(code))
+ delete(m.m, code)
+ m.Unlock()
+ log.Warn("MetaController: code timeout removed, code = %s", code)
+ }
+ }
+ }
+ }
+ }()
+}
diff --git a/internal/handler/share.go b/internal/handler/share.go
new file mode 100644
index 0000000..979297c
--- /dev/null
+++ b/internal/handler/share.go
@@ -0,0 +1,119 @@
+package handler
+
+import (
+ "fmt"
+ "github.com/loveuer/nf"
+ "github.com/loveuer/nf/nft/log"
+ "github.com/loveuer/ushare/internal/controller"
+ "github.com/loveuer/ushare/internal/model"
+ "github.com/loveuer/ushare/internal/opt"
+ "github.com/pkg/errors"
+ "github.com/spf13/cast"
+ "github.com/spf13/viper"
+ "net/http"
+ "os"
+ "regexp"
+ "strings"
+)
+
+func Fetch() nf.HandlerFunc {
+ return func(c *nf.Ctx) error {
+ code := c.Param("code")
+ log.Debug("handler.Fetch: code = %s", code)
+ info := new(model.Meta)
+ _, err := os.Stat(opt.MetaPath(code))
+ if err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ return c.Status(http.StatusNotFound).JSON(map[string]string{"msg": "文件不存在"})
+ }
+
+ return c.SendStatus(http.StatusInternalServerError)
+ }
+
+ viper.SetConfigFile(opt.MetaPath(code))
+ viper.SetConfigType("env")
+ if err = viper.ReadInConfig(); err != nil {
+ return c.SendStatus(http.StatusInternalServerError)
+ }
+
+ if err = viper.Unmarshal(info); err != nil {
+ return c.SendStatus(http.StatusInternalServerError)
+ }
+
+ c.SetHeader("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, info.Filename))
+ http.ServeFile(c.Writer, c.Request, opt.FilePath(code))
+
+ return nil
+ }
+}
+
+func ShareNew() nf.HandlerFunc {
+ return func(c *nf.Ctx) error {
+
+ filename := strings.TrimSpace(c.Param("filename"))
+ if filename == "" {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "filename required"})
+ }
+
+ size, err := cast.ToInt64E(c.Get(opt.HeaderSize))
+ if err != nil {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "miss header: " + opt.HeaderSize})
+ }
+
+ code, err := controller.MetaManager.New(size, filename, c.IP())
+ if err != nil {
+ return c.Status(http.StatusInternalServerError).JSON(map[string]string{"msg": ""})
+ }
+
+ return c.Status(http.StatusOK).JSON(map[string]string{"code": code})
+ }
+}
+
+func ShareUpload() nf.HandlerFunc {
+ rangeValidator := regexp.MustCompile(`^bytes=\d+-\d+$`)
+ return func(c *nf.Ctx) error {
+ code := strings.TrimSpace(c.Param("code"))
+ if len(code) != 16 {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "invalid file code"})
+ }
+
+ log.Debug("handler.ShareUpload: code = %s", code)
+
+ ranger := strings.TrimSpace(c.Get("Range"))
+ if ranger == "" {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "miss header: Range"})
+ }
+
+ log.Debug("handler.ShareUpload: code = %s, ranger = %s", code, ranger)
+
+ if !rangeValidator.MatchString(ranger) {
+ log.Warn("handler.ShareUpload: invalid range, ranger = %s", ranger)
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(1)"})
+ }
+
+ strs := strings.Split(strings.TrimPrefix(ranger, "bytes="), "-")
+ if len(strs) != 2 {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(2)"})
+ }
+
+ start, err := cast.ToInt64E(strs[0])
+ if err != nil {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(3)"})
+ }
+
+ end, err := cast.ToInt64E(strs[1])
+ if err != nil {
+ return c.Status(http.StatusBadRequest).JSON(map[string]string{"msg": "Range invalid(4)"})
+ }
+
+ log.Debug("handler.ShareUpload: code = %s, start = %d, end = %d", code, start, end)
+
+ total, cursor, err := controller.MetaManager.Write(code, start, end, c.Request.Body)
+ if err != nil {
+ log.Error("handler.ShareUpload: write error: %s", err)
+ return c.Status(http.StatusInternalServerError).JSON(map[string]string{"msg": ""})
+ }
+
+ return c.Status(http.StatusOK).JSON(map[string]any{"size": total, "cursor": cursor})
+ }
+}
diff --git a/internal/model/meta.go b/internal/model/meta.go
new file mode 100644
index 0000000..6a96ef5
--- /dev/null
+++ b/internal/model/meta.go
@@ -0,0 +1,8 @@
+package model
+
+type Meta struct {
+ Filename string `json:"filename" mapstructure:"filename"`
+ CreatedAt int64 `json:"created_at" mapstructure:"created_at"`
+ Size int64 `json:"size" mapstructure:"size"`
+ UploaderIp string `json:"uploader_ip" mapstructure:"uploader_ip"`
+}
diff --git a/internal/opt/opt.go b/internal/opt/opt.go
index cf70c68..1cf532f 100644
--- a/internal/opt/opt.go
+++ b/internal/opt/opt.go
@@ -1,8 +1,9 @@
package opt
type config struct {
- Debug bool
- Address string
+ Debug bool
+ Address string
+ DataPath string
}
var (
diff --git a/internal/opt/var.go b/internal/opt/var.go
new file mode 100644
index 0000000..eb2706e
--- /dev/null
+++ b/internal/opt/var.go
@@ -0,0 +1,16 @@
+package opt
+
+import "path/filepath"
+
+const (
+ Meta = ".meta."
+ HeaderSize = "X-File-Size"
+)
+
+func FilePath(code string) string {
+ return filepath.Join(Cfg.DataPath, code)
+}
+
+func MetaPath(code string) string {
+ return filepath.Join(Cfg.DataPath, Meta+code)
+}
diff --git a/main.go b/main.go
index 2ce8db6..fa0c325 100644
--- a/main.go
+++ b/main.go
@@ -3,7 +3,9 @@ package main
import (
"context"
"flag"
+ "github.com/loveuer/nf/nft/log"
"github.com/loveuer/ushare/internal/api"
+ "github.com/loveuer/ushare/internal/controller"
"github.com/loveuer/ushare/internal/opt"
"os/signal"
"syscall"
@@ -12,13 +14,19 @@ import (
func init() {
flag.BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
flag.StringVar(&opt.Cfg.Address, "address", "0.0.0.0:80", "")
+ flag.StringVar(&opt.Cfg.DataPath, "data", "/data", "")
flag.Parse()
+
+ if opt.Cfg.Debug {
+ log.SetLogLevel(log.LogLevelDebug)
+ }
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
+ controller.MetaManager.Start(ctx)
api.Start(ctx)
<-ctx.Done()