feat: allow to write to notes
This commit is contained in:
@@ -257,6 +257,66 @@ func (l *LocalFsAdapter) Read(path string) (*Entry, error) {
|
||||
}
|
||||
|
||||
func (l *LocalFsAdapter) Write(path string, content []byte) error {
|
||||
pathParts := strings.Split(strings.Trim(path, "/"), "/")
|
||||
if len(pathParts) == 0 || pathParts[0] == "" {
|
||||
return errors.New("invalid path")
|
||||
}
|
||||
|
||||
rootIdentifier := pathParts[0]
|
||||
var targetRoot string
|
||||
for _, r := range l.roots {
|
||||
if filepath.Base(r) == rootIdentifier {
|
||||
targetRoot = r
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if targetRoot == "" {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
subPath := filepath.Join(pathParts[1:]...)
|
||||
target := filepath.Join(targetRoot, subPath)
|
||||
|
||||
absTarget, err := filepath.Abs(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
absRoot, err := filepath.Abs(targetRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.HasPrefix(absTarget, absRoot) {
|
||||
return errors.New("path escapes root")
|
||||
}
|
||||
|
||||
dir := filepath.Dir(absTarget)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(absTarget, content, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Invalidate cache
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
currentPath := path
|
||||
for {
|
||||
delete(l.cache, currentPath)
|
||||
if currentPath == "/" {
|
||||
break
|
||||
}
|
||||
lastSlash := strings.LastIndex(currentPath, "/")
|
||||
if lastSlash <= 0 {
|
||||
currentPath = "/"
|
||||
} else {
|
||||
currentPath = currentPath[:lastSlash]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user