-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_file.go
78 lines (64 loc) · 2.33 KB
/
add_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Package storage provides functions for adding files to IPFS.
package ipfs
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
icore "github.com/ipfs/kubo/core/coreiface"
"github.com/pierreleocadie/SecuraChain/internal/config"
"github.com/pierreleocadie/SecuraChain/pkg/utils"
)
// prepareFileForIPFS prepares a file to be added to IPFS by creating a UnixFS node from the given path.
// It retrieves file information and creates a serial file node for IPFS.
func prepareFileForIPFS(path string) (files.Node, error) {
sanitizedPath, err := utils.SanitizePath(path)
if err != nil {
return nil, err
}
stat, err := os.Stat(sanitizedPath)
if err != nil {
return nil, err
}
fileNode, err := files.NewSerialFile(path, false, stat)
if err != nil {
return nil, err
}
return fileNode, nil
}
// AddFileToIPFS adds a file to IPFS and returns its CID. It also collects and saves file metadata.
// The function handles the file addition process and records metadata such as file size, type, name, and user public key.
func AddFile(ctx context.Context, config *config.Config, ipfsAPI icore.CoreAPI, filePath string) (path.ImmutablePath, error) {
file, err := prepareFileForIPFS(filePath)
if err != nil {
log.Println("could not get File:", err)
}
fileCid, err := ipfsAPI.Unixfs().Add(ctx, file)
if err != nil {
log.Printf("Could not add file to IPFS %v", err)
return path.ImmutablePath{}, err
}
log.Printf("File added with CID: %s", fileCid.String())
// Adding the file on the storage node (local system)
home, err := os.UserHomeDir()
if err != nil {
return path.ImmutablePath{}, err
}
localStoragePath := filepath.Join(home, ".IPFS_Local_Storage/")
if err := os.MkdirAll(localStoragePath, os.FileMode(config.FileRights)); err != nil {
return path.ImmutablePath{}, fmt.Errorf("error creating output directory : %v", err)
}
outputFilePath := filepath.Join(localStoragePath, filepath.Base(filePath))
err = MoveFile(filePath, outputFilePath)
if err != nil {
return path.ImmutablePath{}, fmt.Errorf("error copying file to output directory: %v", err)
}
// restore file permissions
if err := os.Chmod(outputFilePath, os.FileMode(config.FileRights)); err != nil {
return path.ImmutablePath{}, fmt.Errorf("error restoring file permissions: %v", err)
}
return fileCid, nil
}