MCPcopy
hub / github.com/daptin/daptin / GetFileFromLocalCloudStore

Method GetFileFromLocalCloudStore

server/resource/dbmethods.go:3943–3980  ·  view source on GitHub ↗

resolve a file column from data in column to actual file on a cloud store returns a map containing the metadata of the file and the file contents as base64 encoded can be sent to browser to invoke downloading js and data urls

(tableName string, columnName string, filesList []map[string]interface{})

Source from the content-addressed store, hash-verified

3941// returns a map containing the metadata of the file and the file contents as base64 encoded
3942// can be sent to browser to invoke downloading js and data urls
3943func (dbResource *DbResource) GetFileFromLocalCloudStore(tableName string, columnName string, filesList []map[string]interface{}) (resp []map[string]interface{}, err error) {
3944
3945 assetFolder, ok := dbResource.AssetFolderCache[tableName][columnName]
3946 if !ok {
3947 return nil, fmt.Errorf("not a synced folder [%v][%v]", tableName, columnName)
3948 }
3949
3950 for _, fileItem := range filesList {
3951 newFileItem := make(map[string]interface{})
3952
3953 for key, val := range fileItem {
3954 newFileItem[key] = val
3955 }
3956
3957 if fileItem["src"] == nil {
3958 log.Printf("file has no source: [%v][%v]", tableName, columnName)
3959 continue
3960 }
3961
3962 filePath := fileItem["src"].(string)
3963 filePath = strings.ReplaceAll(filePath, "/", string(os.PathSeparator))
3964 filePath = path.Clean(filePath)
3965 if filePath[0] != os.PathSeparator {
3966 filePath = string(os.PathSeparator) + filePath
3967 }
3968 file, err := assetFolder.GetFileByName(filePath)
3969 bytes, err := io.ReadAll(file)
3970 file.Close()
3971 CheckErr(err, "Failed to read file on storage [%v]: %v", assetFolder.LocalSyncPath, filePath)
3972 if err != nil {
3973 continue
3974 }
3975 newFileItem["reference_id"] = fileItem["name"]
3976 newFileItem["contents"] = base64.StdEncoding.EncodeToString(bytes)
3977 resp = append(resp, newFileItem)
3978 }
3979 return resp, nil
3980}

Callers 1

Calls 5

makeFunction · 0.85
GetFileByNameMethod · 0.80
ReadAllMethod · 0.80
CheckErrFunction · 0.70
CloseMethod · 0.65

Tested by

no test coverage detected