MCPcopy Create free account
hub / github.com/MertJSX/folderhost / writeFile

Function writeFile

middleware/websocket/file_editor.go:257–312  ·  view source on GitHub ↗
(filepath string, lines []string, account *types.Account)

Source from the content-addressed store, hash-verified

255}
256
257func writeFile(filepath string, lines []string, account *types.Account) error {
258 content := strings.Join(lines, "\n")
259
260 if len(content) > 200*1024 {
261 return fmt.Errorf("file size exceeds 200 KB: %d Bytes", len(content))
262 }
263
264 watcherCache, ok := cache.EditorWatcherCache.Get(filepath)
265 if !ok {
266 return nil
267 }
268
269 watcherCache.IsWriting = true
270
271 cache.EditorWatcherCache.SetWithoutTTL(filepath, watcherCache)
272
273 err := os.WriteFile(filepath, []byte(content), 0644)
274
275 if err != nil {
276 watcherCache.IsWriting = false
277 cache.EditorWatcherCache.SetWithoutTTL(filepath, watcherCache)
278
279 return err
280 }
281
282 fileStat, err := os.Stat(filepath)
283
284 if err != nil {
285 watcherCache.IsWriting = false
286 cache.EditorWatcherCache.SetWithoutTTL(filepath, watcherCache)
287
288 return err
289 }
290
291 watcherCache.LastModTime = fileStat.ModTime()
292 watcherCache.IsWriting = false
293
294 cache.EditorWatcherCache.SetWithoutTTL(filepath, watcherCache)
295
296 // That's not the best way to update caches. Because not all scopes will be updated.
297 // The problem is that if we update all directory caches on every writeFile call it will not be good for performance I think...
298 if directoryCache, ok := cache.DirectoryCache.Get(cache.DirectoryCacheKey{
299 Path: utils.GetParentPath(filepath) + "/",
300 Scope: account.Scope,
301 }); ok {
302 for index, v := range directoryCache.Items {
303 v.SizeBytes = fileStat.Size()
304 directoryCache.Items[index] = v
305 }
306 cache.DirectoryCache.Set(cache.DirectoryCacheKey{
307 Path: utils.GetParentPath(filepath) + "/",
308 Scope: account.Scope,
309 }, directoryCache, 600)
310 }
311 return nil
312}

Callers 3

applyInsertFunction · 0.85
applyDeleteFunction · 0.85
applyReplaceFunction · 0.85

Calls 4

GetParentPathFunction · 0.92
GetMethod · 0.80
SetWithoutTTLMethod · 0.80
SetMethod · 0.80

Tested by

no test coverage detected