()
| 1287 | } |
| 1288 | |
| 1289 | func (app *BaseApp) registerBaseHooks() { |
| 1290 | deletePrefix := func(prefix string) error { |
| 1291 | fs, err := app.NewFilesystem() |
| 1292 | if err != nil { |
| 1293 | return err |
| 1294 | } |
| 1295 | defer fs.Close() |
| 1296 | |
| 1297 | failed := fs.DeletePrefix(prefix) |
| 1298 | if len(failed) > 0 { |
| 1299 | return errors.New("failed to delete the files at " + prefix) |
| 1300 | } |
| 1301 | |
| 1302 | return nil |
| 1303 | } |
| 1304 | |
| 1305 | maxFilesDeleteWorkers := cast.ToInt64(os.Getenv("PB_FILES_DELETE_MAX_WORKERS")) |
| 1306 | if maxFilesDeleteWorkers <= 0 { |
| 1307 | maxFilesDeleteWorkers = 2000 // the value is arbitrary chosen and may change in the future |
| 1308 | } |
| 1309 | |
| 1310 | deleteSem := semaphore.NewWeighted(maxFilesDeleteWorkers) |
| 1311 | |
| 1312 | // try to delete the storage files from deleted Collection, Records, etc. model |
| 1313 | app.OnModelAfterDeleteSuccess().Bind(&hook.Handler[*ModelEvent]{ |
| 1314 | Id: "__pbFilesManagerDelete__", |
| 1315 | Func: func(e *ModelEvent) error { |
| 1316 | if m, ok := e.Model.(FilesManager); ok && m.BaseFilesPath() != "" && supportFiles(e.Model) { |
| 1317 | // ensure that there is a trailing slash so that the list iterator could start walking from the prefix dir |
| 1318 | // (https://github.com/pocketbase/pocketbase/discussions/5246#discussioncomment-10128955) |
| 1319 | prefix := strings.TrimRight(m.BaseFilesPath(), "/") + "/" |
| 1320 | |
| 1321 | // note: for now assume no context cancellation |
| 1322 | err := deleteSem.Acquire(context.Background(), 1) |
| 1323 | if err != nil { |
| 1324 | app.Logger().Error( |
| 1325 | "Failed to delete storage prefix (couldn't acquire a worker)", |
| 1326 | slog.String("prefix", prefix), |
| 1327 | slog.String("error", err.Error()), |
| 1328 | ) |
| 1329 | } else { |
| 1330 | // run in the background for "optimistic" delete to avoid blocking the delete transaction |
| 1331 | routine.FireAndForget(func() { |
| 1332 | defer deleteSem.Release(1) |
| 1333 | |
| 1334 | if err := deletePrefix(prefix); err != nil { |
| 1335 | app.Logger().Error( |
| 1336 | "Failed to delete storage prefix (non critical error; usually could happen because of S3 api limits)", |
| 1337 | slog.String("prefix", prefix), |
| 1338 | slog.String("error", err.Error()), |
| 1339 | ) |
| 1340 | } |
| 1341 | }) |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | return e.Next() |
| 1346 | }, |
no test coverage detected