2022-06-08 10:19:45 +00:00
|
|
|
/*
|
|
|
|
* @Author: LinkLeong link@icewhale.com
|
|
|
|
* @Date: 2021-12-20 14:15:46
|
|
|
|
* @LastEditors: LinkLeong
|
2022-07-04 10:07:06 +00:00
|
|
|
* @LastEditTime: 2022-07-04 16:18:23
|
2022-06-08 10:19:45 +00:00
|
|
|
* @FilePath: /CasaOS/service/file.go
|
|
|
|
* @Description:
|
|
|
|
* @Website: https://www.casaos.io
|
|
|
|
* Copyright (c) 2022 by icewhale, All Rights Reserved.
|
|
|
|
*/
|
2021-12-29 08:42:20 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2022-06-08 10:19:45 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-11-29 17:17:14 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
2022-06-08 10:19:45 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
2022-06-10 05:33:53 +00:00
|
|
|
"go.uber.org/zap"
|
2021-12-29 08:42:20 +00:00
|
|
|
)
|
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
var FileQueue sync.Map
|
|
|
|
|
|
|
|
var OpStrArr []string
|
2021-12-29 08:42:20 +00:00
|
|
|
|
|
|
|
type reader struct {
|
|
|
|
ctx context.Context
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader wraps an io.Reader to handle context cancellation.
|
|
|
|
//
|
|
|
|
// Context state is checked BEFORE every Read.
|
|
|
|
func NewReader(ctx context.Context, r io.Reader) io.Reader {
|
|
|
|
if r, ok := r.(*reader); ok && ctx == r.ctx {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return &reader{ctx: ctx, r: r}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *reader) Read(p []byte) (n int, err error) {
|
|
|
|
select {
|
|
|
|
case <-r.ctx.Done():
|
|
|
|
return 0, r.ctx.Err()
|
|
|
|
default:
|
|
|
|
return r.r.Read(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
ctx context.Context
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
type copier struct {
|
|
|
|
writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWriter(ctx context.Context, w io.Writer) io.Writer {
|
|
|
|
if w, ok := w.(*copier); ok && ctx == w.ctx {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
return &copier{writer{ctx: ctx, w: w}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements io.Writer, but with context awareness.
|
|
|
|
func (w *writer) Write(p []byte) (n int, err error) {
|
|
|
|
select {
|
|
|
|
case <-w.ctx.Done():
|
|
|
|
return 0, w.ctx.Err()
|
|
|
|
default:
|
|
|
|
return w.w.Write(p)
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 10:19:45 +00:00
|
|
|
|
2022-11-29 17:17:14 +00:00
|
|
|
func FileOperate(k string) {
|
2022-06-08 10:19:45 +00:00
|
|
|
list, ok := FileQueue.Load(k)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
temp := list.(model.FileOperate)
|
|
|
|
if temp.ProcessedSize > 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-06-10 05:33:53 +00:00
|
|
|
for i := 0; i < len(temp.Item); i++ {
|
|
|
|
v := temp.Item[i]
|
2022-06-08 10:19:45 +00:00
|
|
|
if temp.Type == "move" {
|
|
|
|
lastPath := v.From[strings.LastIndex(v.From, "/")+1:]
|
|
|
|
if !file.CheckNotExist(temp.To + "/" + lastPath) {
|
2022-06-10 05:33:53 +00:00
|
|
|
if temp.Style == "skip" {
|
|
|
|
temp.Item[i].Finished = true
|
|
|
|
continue
|
|
|
|
} else {
|
2022-06-16 08:57:13 +00:00
|
|
|
os.RemoveAll(temp.To + "/" + lastPath)
|
2022-06-10 05:33:53 +00:00
|
|
|
}
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
|
|
|
err := os.Rename(v.From, temp.To+"/"+lastPath)
|
|
|
|
if err != nil {
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Error("file move error", zap.Any("err", err))
|
2022-07-04 10:07:06 +00:00
|
|
|
err = file.MoveFile(v.From, temp.To+"/"+lastPath)
|
|
|
|
if err != nil {
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Error("MoveFile error", zap.Any("err", err))
|
2022-07-04 10:07:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
|
|
|
} else if temp.Type == "copy" {
|
2022-06-10 05:33:53 +00:00
|
|
|
err := file.CopyDir(v.From, temp.To, temp.Style)
|
2022-06-08 10:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-10 05:33:53 +00:00
|
|
|
}
|
|
|
|
temp.Finished = true
|
|
|
|
FileQueue.Store(k, temp)
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExecOpFile() {
|
|
|
|
len := len(OpStrArr)
|
|
|
|
if len == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len > 1 {
|
|
|
|
len = 1
|
|
|
|
}
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
go FileOperate(OpStrArr[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// file move or copy and send notify
|
|
|
|
func CheckFileStatus() {
|
|
|
|
for {
|
|
|
|
if len(OpStrArr) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, v := range OpStrArr {
|
|
|
|
var total int64 = 0
|
|
|
|
item, ok := FileQueue.Load(v)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
temp := item.(model.FileOperate)
|
|
|
|
for i := 0; i < len(temp.Item); i++ {
|
|
|
|
if !temp.Item[i].Finished {
|
|
|
|
size, err := file.GetFileOrDirSize(temp.To + "/" + filepath.Base(temp.Item[i].From))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
temp.Item[i].ProcessedSize = size
|
|
|
|
if size == temp.Item[i].Size {
|
|
|
|
temp.Item[i].Finished = true
|
|
|
|
}
|
|
|
|
total += size
|
|
|
|
} else {
|
|
|
|
total += temp.Item[i].ProcessedSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
temp.ProcessedSize = total
|
|
|
|
FileQueue.Store(v, temp)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
}
|
|
|
|
}
|