oxen-server 0.56.0

Oxen server is a fast data version control backend, supporting local disk and S3. Self host your repositories on your own storage, or use the hosted platform on Oxen.ai. Stores, syncs, and serves versioned datasets, model checkpoints, game assets, studio media, and any large data. Use the oxen CLI to push and pull from the oxen server.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
use crate::app_data::OxenAppData;
use crate::config::identity_policy::IdentitySource;
use crate::errors::OxenHttpError;
use crate::helpers::{get_repo, get_repo_async};
use crate::params::{
    app_data, path_param, reject_invalid_namespace_name, reject_invalid_repo_name,
};

use futures_util::TryStreamExt;
use futures_util::stream::StreamExt;
use liboxen::api::requests::{RepoNew, TransferNamespaceRequest};
// Import StreamExt for the next() method
use liboxen::constants::DEFAULT_BRANCH_NAME;
use liboxen::core::repo_locks;
use liboxen::error::OxenError;
use liboxen::model::file::{FileContents, FileNew};
use liboxen::model::parsed_resource::ParsedResourceView;
use liboxen::model::{Branch, ParsedResource, RepoIdentity};
use liboxen::repositories;
use liboxen::view::http::{MSG_RESOURCE_FOUND, MSG_RESOURCE_UPDATED, STATUS_SUCCESS};
use liboxen::view::repository::{
    DataTypeView, RepositoryCreationResponse, RepositoryCreationView, RepositoryDataTypesResponse,
    RepositoryDataTypesView, RepositoryListView, RepositoryStatsResponse, RepositoryStatsView,
};
use liboxen::view::{
    DataTypeCount, ListRepositoryResponse, RepositoryResponse, RepositoryView, StatusMessage,
};

use actix_multipart::Multipart; // Gives us Multipart
use liboxen::model::User;

use actix_web::{HttpRequest, HttpResponse, Result, web};
use serde_json::from_slice;
use std::path::PathBuf;
use utoipa;

/// List repositories
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}",
    tag = "Repositories",
    description = "List all repositories in a namespace.",
    params(
        ("namespace" = String, Path, description = "Namespace to list repositories from", example = "ox"),
    ),
    responses(
        (status = 200, description = "List of repositories", body = ListRepositoryResponse),
        (status = 400, description = "Namespace is not a single path segment"),
        (status = 404, description = "Namespace not found")
    )
)]
pub async fn index(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();

    let namespace_path = repositories::namespace_dir(&app_data.path, &namespace)?;

    let repos: Vec<RepositoryListView> = repositories::list_repos_in_namespace(&namespace_path)
        .map(|repo| RepositoryListView {
            name: repo.dirname(),
            namespace: namespace.to_string(),
            min_version: Some("0.36.0".to_string()),
        })
        .collect();
    let view = ListRepositoryResponse {
        status: StatusMessage::resource_found(),
        repositories: repos,
    };
    Ok(HttpResponse::Ok().json(view))
}

/// Get repository details
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}",
    tag = "Repositories",
    description = "Get repository details including size and data types from the main branch.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "Repository details", body = RepositoryDataTypesResponse),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn show(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();

    // Get the repository or return error
    let repository = get_repo_async(app_data, &namespace, &name).await?;
    let mut size: u64 = 0;
    let mut data_types: Vec<DataTypeCount> = vec![];
    let mut default_resource: Option<ParsedResourceView> = None;

    // If we have a commit on the main branch, we can get the size and data types from the commit
    if let Ok(Some(commit)) =
        repositories::revisions::get_async(&repository, DEFAULT_BRANCH_NAME).await
    {
        if let Some(dir_node) =
            repositories::entries::get_directory_async(&repository, &commit, PathBuf::from(""))
                .await?
        {
            size = dir_node.num_bytes();
            data_types = dir_node
                .data_type_counts()
                .iter()
                .map(|(data_type, count)| DataTypeCount {
                    data_type: data_type.to_string(),
                    count: *count as usize,
                })
                .collect();
        }

        // The resolved commit is the head of the default branch, so its id is that branch's
        // commit id; build the branch from it rather than re-reading the refs DB.
        let branch = Branch {
            name: DEFAULT_BRANCH_NAME.to_string(),
            commit_id: commit.id.clone(),
        };
        default_resource = Some(ParsedResourceView::from(ParsedResource {
            commit: Some(commit),
            branch: Some(branch),
            workspace: None,
            path: PathBuf::from(""),
            version: PathBuf::from(DEFAULT_BRANCH_NAME),
            resource: PathBuf::from(DEFAULT_BRANCH_NAME),
        }));
    }

    // A repo with no branches is empty; derive it from the same scan rather than a second read.
    let branch_count = repositories::branches::list(&repository).await?.len();

    // Return the repository view
    Ok(HttpResponse::Ok().json(RepositoryDataTypesResponse {
        status: STATUS_SUCCESS.to_string(),
        status_message: MSG_RESOURCE_FOUND.to_string(),
        repository: RepositoryDataTypesView {
            repository: RepositoryView {
                namespace,
                name,
                min_version: Some("0.36.0".to_string()),
                is_empty: branch_count == 0,
                storage_kind: repository.storage_config().kind,
                merkle_node_backend: Some(repository.merkle_node_backend()),
                repo_uuid: repository.repo_uuid(),
            },
            size,
            data_types,
            branch_count,
            default_resource,
        },
    }))
}

/// Get repository stats
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/stats",
    description = "Get the total number of files, the total size of the files, and the number of different file types.",
    tag = "Repositories",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "Repository statistics", body = RepositoryStatsResponse),
        (status = 404, description = "Repository not found"),
    )
)]
pub async fn stats(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;

    let namespace: Option<&str> = path_param(&req, "namespace").ok();
    let name: Option<&str> = path_param(&req, "repo_name").ok();
    if let (Some(name), Some(namespace)) = (name, namespace) {
        match repositories::get_by_namespace_and_name(
            &app_data.path,
            namespace,
            name,
            app_data.config.storage.s3(),
        ) {
            Ok(Some(repo)) => {
                let stats = repositories::stats::get_stats(&repo)?;
                let data_types: Vec<DataTypeView> = stats
                    .data_types
                    .values()
                    .map(|s| DataTypeView {
                        data_type: s.data_type.to_owned(),
                        file_count: s.file_count,
                        data_size: s.data_size,
                    })
                    .collect();
                Ok(HttpResponse::Ok().json(RepositoryStatsResponse {
                    status: StatusMessage::resource_found(),
                    repository: RepositoryStatsView {
                        data_size: stats.data_size,
                        data_types,
                    },
                }))
            }
            Ok(None) => {
                log::debug!("404 Could not find repo: {name}");
                Ok(HttpResponse::NotFound().json(StatusMessage::resource_not_found()))
            }
            Err(_) => {
                // `get_by_namespace_and_name` reports the failure; it holds the repo directory.
                Ok(
                    HttpResponse::InternalServerError()
                        .json(StatusMessage::internal_server_error()),
                )
            }
        }
    } else {
        let msg = "Could not find `name` or `namespace` param...";
        Ok(HttpResponse::BadRequest().json(StatusMessage::error(msg)))
    }
}

/// Update repository size
#[utoipa::path(
    put,
    path = "/api/repos/{namespace}/{repo_name}/size",
    tag = "Repositories",
    description = "Recalculate and update the cached repository size.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "Repository size updated", body = StatusMessage),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn update_size(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();

    let repository = get_repo(app_data, &namespace, &name)?;
    let _write = repo_locks::acquire_write(&repository)?;
    repositories::size::update_size(&repository)?;

    Ok(HttpResponse::Ok().json(StatusMessage::resource_updated()))
}

/// Get repository size
#[utoipa::path(
    get,
    path = "/api/repos/{namespace}/{repo_name}/size",
    tag = "Repositories",
    description = "Get the cached size of the repository in bytes.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "ImageNet-1k"),
    ),
    responses(
        (status = 200, description = "Repository size in bytes", body = u64),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn get_size(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();

    let repository = get_repo(app_data, &namespace, &name)?;
    // `size::get_size` writes the size cache on a miss (a GET that mutates — tech-debt ENG-1374);
    // guard the whole handler so a stop-the-world op (migration/prune/fsck) blocks it.
    let _write = repo_locks::acquire_write(&repository)?;
    let size = repositories::size::get_size(&repository)?;
    Ok(HttpResponse::Ok().json(size))
}

/// Create repository
#[utoipa::path(
    post,
    path = "/api/repos",
    tag = "Repositories",
    description = "Create a new repository, optionally with initial files via JSON or multipart form.",
    request_body(
        content = RepoNew,
        description = "Repository creation payload (JSON or Multipart)",
        content_type = "application/json",
        example = json!({
            "namespace": "ox",
            "name": "Cat-Dog-Classifier",
            "user": {
                "name": "bessie",
                "email": "bessie@oxen.ai"
            },
            "description": "A repository for image classification"
        })
    ),
    responses(
        (status = 200, description = "Repository created", body = RepositoryCreationResponse),
        (status = 400, description = "Invalid payload"),
        (status = 409, description = "Repository already exists"),
    )
)]
pub async fn create(
    req: HttpRequest,
    mut payload: web::Payload,
) -> Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;

    if let Some(content_type) = req.headers().get("Content-Type") {
        if content_type == "application/json" {
            let mut body_bytes = Vec::new();
            while let Some(chunk) = payload.next().await {
                let chunk = chunk.map_err(|e| {
                    println!("Failed to read payload: {e:?}");
                    OxenHttpError::BadRequest("Failed to read payload".into())
                })?;
                body_bytes.extend_from_slice(&chunk);
            }
            let json_data: RepoNew = from_slice(&body_bytes).map_err(|e| {
                println!("Failed to parse JSON: {e:?}");
                OxenHttpError::BadRequest("Invalid JSON".into())
            })?;
            return create_repo_response(app_data, json_data).await;
        } else {
            content_type
                .to_str()
                .unwrap_or("")
                .starts_with("multipart/form-data");
            {
                let multipart = Multipart::new(req.headers(), payload);
                return handle_multipart_creation(app_data, multipart).await;
            }
        }
    }
    Err(OxenHttpError::BadRequest("Unsupported Content-Type".into()))
}

async fn handle_multipart_creation(
    app_data: &OxenAppData,
    mut multipart: Multipart,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let mut repo_new: Option<RepoNew> = None;
    let mut files: Vec<FileNew> = vec![];
    let mut name: Option<String> = None;
    let mut email: Option<String> = None;

    // Parse multipart form fields
    while let Some(mut field) = multipart
        .try_next()
        .await
        .map_err(OxenHttpError::MultipartError)?
    {
        let disposition = field.content_disposition().ok_or(OxenHttpError::NotFound)?;
        let field_name = disposition
            .get_name()
            .ok_or(OxenHttpError::NotFound)?
            .to_string(); // Convert to owned String

        match field_name.as_str() {
            "new_repo" => {
                let mut body = String::new();
                while let Some(chunk) = field
                    .try_next()
                    .await
                    .map_err(OxenHttpError::MultipartError)?
                {
                    body.push_str(
                        std::str::from_utf8(&chunk)
                            .map_err(|e| OxenHttpError::BadRequest(e.to_string().into()))?,
                    );
                }
                repo_new = Some(serde_json::from_str(&body)?);
            }
            "name" | "email" => {
                let mut bytes = Vec::new();
                while let Some(chunk) = field
                    .try_next()
                    .await
                    .map_err(OxenHttpError::MultipartError)?
                {
                    bytes.extend_from_slice(&chunk);
                }
                let value = String::from_utf8(bytes)
                    .map_err(|e| OxenHttpError::BadRequest(e.to_string().into()))?;

                if field_name == "name" {
                    name = Some(value);
                } else {
                    email = Some(value);
                }
            }
            "file[]" | "file" => {
                let filename = disposition.get_filename().map_or_else(
                    || uuid::Uuid::new_v4().to_string(),
                    sanitize_filename::sanitize,
                );

                let mut contents = Vec::new();
                while let Some(chunk) = field
                    .try_next()
                    .await
                    .map_err(OxenHttpError::MultipartError)?
                {
                    contents.extend_from_slice(&chunk);
                }

                files.push(FileNew {
                    path: PathBuf::from(&filename),
                    contents: FileContents::Binary(contents),
                    user: User {
                        name: name
                            .clone()
                            .ok_or_else(|| OxenHttpError::BadRequest("Name is required".into()))?,
                        email: email
                            .clone()
                            .ok_or_else(|| OxenHttpError::BadRequest("Email is required".into()))?,
                    },
                });
            }
            _ => continue,
        }
    }

    // Handle repository creation
    let repo_data = {
        let Some(mut repo_data) = repo_new else {
            return Ok(
                HttpResponse::BadRequest().json(StatusMessage::error("Missing new_repo field"))
            );
        };

        repo_data.files = if !files.is_empty() { Some(files) } else { None };
        repo_data
    };

    // Create repository
    create_repo_response(app_data, repo_data).await
}

/// Create the repository from a [`RepoNew`] and build the response that both creation routes
/// (JSON and multipart) send back. `data.storage_kind` is resolved against the server's storage
/// policy (`None` selects the server default).
async fn create_repo_response(
    app_data: &OxenAppData,
    mut data: RepoNew,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    reject_invalid_namespace_name(data.namespace_name.as_deref())?;
    reject_invalid_repo_name(data.repo_name.as_deref())?;
    data.storage_kind = Some(app_data.config.storage.resolve(data.storage_kind)?);
    let namespace = data.namespace.clone();
    let name = data.name.clone();
    let identity = match app_data.config.identity.repo_uuids_assigned_by() {
        IdentitySource::OxenServer => Some(RepoIdentity::minted(&namespace, &name)),
        IdentitySource::AuthProvider => {
            RepoIdentity::from_supplied(data.repo_uuid, &name).map(|identity| RepoIdentity {
                namespace: data.namespace_name.clone(),
                name: data.repo_name.clone(),
                ..identity
            })
        }
    };
    if identity.is_none() {
        log::warn!("Creating {namespace}/{name} with no repository UUID; recording no identity");
    }
    match repositories::create(&app_data.path, data, identity, app_data.config.storage.s3()).await {
        Ok(repo) => {
            // The repository exists by this point, so a failed lookup only degrades the
            // response's latest_commit to None rather than failing the creation.
            let latest_commit = match repositories::commits::latest_commit(&repo) {
                Ok(commit) => Some(commit),
                Err(OxenError::NoCommitsFound) => None,
                Err(err) => {
                    log::error!("Err repositories::commits::latest_commit: {err:?}");
                    None
                }
            };
            Ok(HttpResponse::Ok().json(RepositoryCreationResponse {
                status: STATUS_SUCCESS.to_string(),
                status_message: MSG_RESOURCE_FOUND.to_string(),
                repository: RepositoryCreationView {
                    namespace,
                    name,
                    latest_commit,
                    min_version: Some("0.36.0".to_string()),
                    storage_kind: repo.storage_config().kind,
                    merkle_node_backend: Some(repo.merkle_node_backend()),
                    repo_uuid: repo.repo_uuid(),
                },
            }))
        }
        Err(err) => Ok(map_create_error_to_response(err)),
    }
}

/// Map an [`OxenError`] returned by [`repositories::create`] to the HTTP
/// response that both creation routes (JSON and multipart) send back.
///
/// Kept as a free function so both handlers stay byte-identical on the error
/// path; any new variant only needs to be added here.
fn map_create_error_to_response(err: OxenError) -> HttpResponse {
    match err {
        OxenError::RepoAlreadyExists(path) => {
            log::debug!("Repo already exists: {path:?}");
            HttpResponse::Conflict().json(StatusMessage::error("Repo already exists."))
        }
        OxenError::InvalidRepoName(name) => {
            log::debug!("Invalid repo name: {name}");
            HttpResponse::BadRequest().json(StatusMessage::error(format!(
                "Invalid repository name '{name}'. Must match [a-zA-Z0-9][a-zA-Z0-9_.-]+"
            )))
        }
        OxenError::InvalidNamespaceName(name) => {
            log::debug!("Invalid namespace name: {name}");
            HttpResponse::BadRequest().json(StatusMessage::error(format!(
                "Invalid namespace name '{name}'. Must match [a-zA-Z0-9][a-zA-Z0-9_-]{{1,49}}"
            )))
        }
        err => {
            log::error!("Err repositories::create: {err:?}");
            HttpResponse::InternalServerError().json(StatusMessage::error("Invalid body."))
        }
    }
}

/// Delete repository
#[utoipa::path(
    delete,
    path = "/api/repos/{namespace}/{repo_name}",
    tag = "Repositories",
    description = "Delete a repository. Deletion runs in the background.",
    params(
        ("namespace" = String, Path, description = "Namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "Cat-Dog-Classifier"),
    ),
    responses(
        (status = 200, description = "Repository deletion started", body = StatusMessage),
        (status = 400, description = "Namespace or repository name is not a single path segment"),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn delete(req: HttpRequest) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    let namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();

    // Validates the segments, so it also rejects anything that could name a directory outside the
    // sync dir. Must come before the removal below, which is why the dir is not taken from the
    // repository lookup (that lookup fails for exactly the repos this endpoint still has to
    // delete).
    let repo_dir = repositories::repo_dir(&app_data.path, &namespace, &name)?;

    // Opened directly rather than through `get_repo_async`, whose identity check and hint refresh
    // can also fail: the fallback below deletes, so only a failure to open may reach it.
    let repository = match repositories::get_by_namespace_and_name_async(
        &app_data.path,
        &namespace,
        &name,
        app_data.config.storage.s3(),
    )
    .await
    {
        Ok(Some(repository)) => Some(repository),
        Ok(None) => {
            return Ok(HttpResponse::NotFound().json(StatusMessage::resource_not_found()));
        }
        // A repository the server cannot open is still deleted. Reporting it as missing would
        // strand the directory on disk with no way for a caller to reclaim it, and version blobs
        // held outside the directory are unreachable without the repository config anyway.
        Err(err) => {
            log::warn!("Deleting unreadable repo {namespace}/{name}: {err}");
            None
        }
    };

    // Taken only where the repository opened, since an unreadable one has no lock to contend for.
    let write_guard = repository
        .as_ref()
        .map(repo_locks::acquire_write)
        .transpose()?;

    // Delete in a background task because it could take awhile; the blocking directory
    // removal runs inside delete's own spawn_blocking.
    tokio::spawn(async move {
        // Hold the write guard across the deferred removal (the handler has already returned), so
        // a maintenance operation waits instead of running against a directory that is going away.
        let _write = write_guard;

        let result = match repository {
            Some(repository) => repositories::delete(repository).await,
            None => repositories::delete_dir(&repo_dir).await,
        };

        match result {
            Ok(()) => log::info!("Deleted repo: {namespace}/{name}"),
            Err(err) => log::error!("Err deleting repo: {err}"),
        }
    });

    Ok(HttpResponse::Ok().json(StatusMessage::resource_deleted()))
}

/// Transfer repository namespace
#[utoipa::path(
    patch,
    path = "/api/repos/{namespace}/{repo_name}/transfer",
    tag = "Repositories",
    description = "Transfer a repository to a different namespace.",
    params(
        ("namespace" = String, Path, description = "Current namespace of the repository", example = "ox"),
        ("repo_name" = String, Path, description = "Name of the repository", example = "Cat-Dog-Classifier"),
    ),
    request_body(
        content = TransferNamespaceRequest,
        description = "Target namespace to transfer the repository to.",
        example = json!({
            "namespace": "new_org"
        })
    ),
    responses(
        (status = 200, description = "Repository transferred successfully", body = RepositoryResponse),
        (status = 400, description = "Invalid body or target namespace"),
        (status = 404, description = "Repository not found")
    )
)]
pub async fn transfer_namespace(
    req: HttpRequest,
    body: String,
) -> actix_web::Result<HttpResponse, OxenHttpError> {
    let app_data = app_data(&req)?;
    // Parse body
    let from_namespace = path_param(&req, "namespace")?.to_string();
    let name = path_param(&req, "repo_name")?.to_string();
    let data: TransferNamespaceRequest = serde_json::from_str(&body)?;
    reject_invalid_namespace_name(data.namespace_name.as_deref())?;
    let to_namespace = data.namespace;
    // Checked before the repository opens, so a destination a namespace cannot be named refuses
    // the request rather than moving the repository into it.
    reject_invalid_namespace_name(Some(&to_namespace))?;

    log::debug!("transfer_namespace from: {from_namespace} to: {to_namespace}");

    // Dropped before the move below: the repository holds its Merkle node store open, and the
    // directory housing that store cannot be renamed under it.
    drop(get_repo_async(app_data, &from_namespace, &name).await?);

    // Where the request's positions carry names, the destination position is the name and a
    // body-stated one is ignored. Under a control plane the body is the only source, and a request
    // that states none leaves the repository with no recorded namespace.
    let identity_source = app_data.config.identity.repo_uuids_assigned_by();
    let namespace_hint = if identity_source.supplies_names() {
        Some(to_namespace.clone())
    } else {
        data.namespace_name
    };
    let repo = repositories::transfer_namespace(
        &app_data.path,
        &name,
        &from_namespace,
        &to_namespace,
        namespace_hint.as_deref(),
        app_data.config.storage.s3(),
    )?;

    // Return repository view under new namespace
    Ok(HttpResponse::Ok().json(RepositoryResponse {
        status: STATUS_SUCCESS.to_string(),
        status_message: MSG_RESOURCE_UPDATED.to_string(),
        repository: RepositoryView {
            namespace: to_namespace,
            name,
            min_version: Some("0.36.0".to_string()),
            is_empty: repositories::is_empty(&repo).await?,
            storage_kind: repo.storage_config().kind,
            merkle_node_backend: Some(repo.merkle_node_backend()),
            repo_uuid: repo.repo_uuid(),
        },
    }))
}

#[cfg(test)]
mod tests {
    use crate::app_data::OxenAppData;
    use crate::config::Config;
    use crate::errors::OxenHttpError;
    use crate::test;
    use actix_web::test::TestRequest;
    use actix_web::{App, ResponseError, http, web};
    use liboxen::api::requests::RepoNew;
    use liboxen::config::RepositoryConfig;
    use liboxen::core::repo_locks;
    use liboxen::error::OxenError;
    use liboxen::model::RepoIdentity;
    use liboxen::util;
    use std::path::Path;
    use std::time::{Duration, Instant};
    use uuid::Uuid;

    /// Waits for the handler's background delete to finish.
    async fn wait_until_gone(path: &Path) -> bool {
        let deadline = Instant::now() + Duration::from_secs(10);
        while Instant::now() < deadline {
            if !path.exists() {
                return true;
            }
            tokio::time::sleep(Duration::from_millis(25)).await;
        }
        false
    }

    #[actix_web::test]
    async fn test_delete_removes_a_repo_the_server_cannot_open() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let namespace = "Testing-Namespace";
        let repo_name = "Testing-Repo";

        let repo = test::create_local_repo(&sync_dir, namespace, repo_name)?;
        let repo_dir = repo.path.clone();

        // Declare an on-disk format this build refuses to load, so the handler cannot open the
        // repo. Deleting it must still clear the directory: a repo the server can read the name of
        // but not the contents of is exactly the one an operator needs removed.
        let config_path = util::fs::config_filepath(&repo_dir);
        let mut config = RepositoryConfig::from_file(&config_path)?;
        config.min_version = Some("0.19.0".to_string());
        config.save(&config_path)?;

        let req = test::repo_request(&sync_dir, "/", namespace, repo_name);
        let resp = super::delete(req)
            .await
            .expect("delete handler should succeed");

        assert_eq!(resp.status(), http::StatusCode::OK);
        assert!(
            wait_until_gone(&repo_dir).await,
            "repo dir should be deleted: {repo_dir:?}"
        );

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// A `..` namespace or name must not let the delete escape the sync directory. The request
    /// goes through a real service so actix's own path decoding runs: `%2e%2e` decodes to `..`.
    #[actix_web::test]
    async fn test_delete_rejects_path_traversal_segments() -> Result<(), OxenError> {
        let root = test::get_sync_dir()?;
        let sync_dir = root.join("level1").join("level2");
        util::fs::create_dir_all(&sync_dir)?;

        // `sync_dir/../..` resolves to `root`. If the handler acts on that path, this file goes.
        let canary = root.join("canary.txt");
        std::fs::write(&canary, b"canary").expect("test fixture write should succeed");

        let app = actix_web::test::init_service(
            App::new()
                .app_data(OxenAppData::new(sync_dir.clone()))
                .route(
                    "/repos/{namespace}/{repo_name}",
                    web::delete().to(super::delete),
                ),
        )
        .await;

        let req = actix_web::test::TestRequest::delete()
            .uri("/repos/%2e%2e/%2e%2e")
            .to_request();
        let resp = actix_web::test::call_service(&app, req).await;

        assert_eq!(resp.status(), http::StatusCode::BAD_REQUEST);

        // The removal runs in a background task, so give it time to do damage before asserting.
        tokio::time::sleep(Duration::from_millis(500)).await;
        assert!(
            canary.exists(),
            "traversal escaped the sync dir: {canary:?} was removed"
        );

        test::cleanup_sync_dir(&root)?;
        Ok(())
    }

    #[actix_web::test]
    async fn test_delete_reports_not_found_for_a_repo_that_is_not_there() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;

        let req = test::repo_request(&sync_dir, "/", "Testing-Namespace", "no-such-repo");
        let resp = super::delete(req)
            .await
            .expect("delete handler should succeed");

        assert_eq!(resp.status(), http::StatusCode::NOT_FOUND);

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// A repository the server cannot open is deleted unconditionally, so a repository held by a
    /// maintenance operation must stop before reaching that removal rather than being cleared out
    /// from under the operation rewriting it.
    #[actix_web::test]
    async fn test_delete_refuses_while_a_maintenance_operation_holds_the_repo()
    -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let namespace = "Testing-Namespace";
        let repo_name = "Testing-Repo";

        let repo = test::create_local_repo(&sync_dir, namespace, repo_name)?;
        let repo_dir = repo.path.clone();

        let req = test::repo_request(&sync_dir, "/", namespace, repo_name);

        repo_locks::with_repo_exclusive(&repo, async {
            let result = super::delete(req).await;

            assert!(
                matches!(
                    result,
                    Err(OxenHttpError::InternalOxenError(OxenError::LockTimeout(_)))
                ),
                "a delete on a repository held for maintenance must be refused"
            );
            Ok::<(), OxenError>(())
        })
        .await?;

        tokio::time::sleep(Duration::from_millis(500)).await;
        assert!(repo_dir.exists(), "repo dir should survive: {repo_dir:?}");

        test::cleanup_repo_and_sync_dir(repo, &sync_dir)?;
        Ok(())
    }

    /// A control plane addresses both positions by UUID, so the names it states in the body are
    /// the only ones the created repository can record.
    #[actix_web::test]
    async fn test_create_records_the_names_stated_in_the_body() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let app_data = OxenAppData {
            path: sync_dir.clone(),
            config: Config {
                identity: toml::from_str(r#"repo_uuids_assigned_by = "auth-provider""#)
                    .expect("a known source parses"),
                ..Default::default()
            },
            test_mode: true,
        };

        let namespace = Uuid::new_v4().to_string();
        let repo_uuid = Uuid::new_v4();
        let mut data = RepoNew::from_namespace_name(&namespace, repo_uuid.to_string(), None);
        data.repo_uuid = Some(repo_uuid);
        data.namespace_name = Some("bessie".to_string());
        data.repo_name = Some("cats".to_string());

        let resp = super::create_repo_response(&app_data, data)
            .await
            .expect("create should succeed");
        assert_eq!(resp.status(), http::StatusCode::OK);

        let repo_dir = sync_dir.join(&namespace).join(repo_uuid.to_string());
        let identity = RepositoryConfig::from_file(util::fs::config_filepath(&repo_dir))?
            .identity
            .expect("create records identity");
        assert_eq!(identity.repo_uuid, repo_uuid);
        assert_eq!(identity.namespace.as_deref(), Some("bessie"));
        assert_eq!(identity.name.as_deref(), Some("cats"));

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// The destination position is a UUID where a control plane owns namespaces, so the name the
    /// body states is what the moved repository records.
    #[actix_web::test]
    async fn test_transfer_records_the_namespace_name_stated_in_the_body() -> Result<(), OxenError>
    {
        let sync_dir = test::get_sync_dir()?;
        let namespace = "Testing-Namespace";
        let repo_name = "Testing-Repo";

        let repo = test::create_local_repo(&sync_dir, namespace, repo_name)?;
        let config_path = util::fs::config_filepath(&repo.path);
        let mut config = RepositoryConfig::from_file(&config_path)?;
        config.identity = Some(RepoIdentity::minted(namespace, repo_name));
        config.save(&config_path)?;

        let req = TestRequest::with_uri("/")
            .app_data(OxenAppData {
                path: sync_dir.clone(),
                config: Config {
                    identity: toml::from_str(r#"repo_uuids_assigned_by = "auth-provider""#)
                        .expect("a known source parses"),
                    ..Default::default()
                },
                test_mode: false,
            })
            .param("namespace", namespace)
            .param("repo_name", repo_name)
            .to_http_request();
        let body = r#"{"namespace":"Other-Namespace","namespace_name":"bessie"}"#.to_string();
        let resp = super::transfer_namespace(req, body)
            .await
            .expect("transfer should succeed");
        assert_eq!(resp.status(), http::StatusCode::OK);

        let moved = sync_dir.join("Other-Namespace").join(repo_name);
        let identity = RepositoryConfig::from_file(util::fs::config_filepath(&moved))?
            .identity
            .expect("identity is intact");
        assert_eq!(
            identity.namespace.as_deref(),
            Some("bessie"),
            "the body's name wins over the addressed position"
        );

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// The destination position is the name where the server owns its namespaces, so the moved
    /// repository records that and not a name stated in the body.
    #[actix_web::test]
    async fn test_transfer_ignores_a_body_name_where_the_position_is_the_name()
    -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let namespace = "Testing-Namespace";
        let repo_name = "Testing-Repo";

        let repo = test::create_local_repo(&sync_dir, namespace, repo_name)?;
        let config_path = util::fs::config_filepath(&repo.path);
        let mut config = RepositoryConfig::from_file(&config_path)?;
        config.identity = Some(RepoIdentity::minted(namespace, repo_name));
        config.save(&config_path)?;

        let req = test::repo_request(&sync_dir, "/", namespace, repo_name);
        let body = r#"{"namespace":"Other-Namespace","namespace_name":"bessie"}"#.to_string();
        let resp = super::transfer_namespace(req, body)
            .await
            .expect("transfer should succeed");
        assert_eq!(resp.status(), http::StatusCode::OK);

        let moved = sync_dir.join("Other-Namespace").join(repo_name);
        let identity = RepositoryConfig::from_file(util::fs::config_filepath(&moved))?
            .identity
            .expect("identity is intact");
        assert_eq!(
            identity.namespace.as_deref(),
            Some("Other-Namespace"),
            "the addressed position wins over the body's name"
        );

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// A blank name is refused wherever it is stated, including on a server that would ignore the
    /// names a request states.
    #[actix_web::test]
    async fn test_create_rejects_a_blank_stated_name() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let app_data = OxenAppData {
            path: sync_dir.clone(),
            config: Config::default(),
            test_mode: false,
        };

        let namespace = Uuid::new_v4().to_string();
        let repo_uuid = Uuid::new_v4();
        let mut data = RepoNew::from_namespace_name(&namespace, repo_uuid.to_string(), None);
        data.namespace_name = Some("   ".to_string());

        let err = super::create_repo_response(&app_data, data)
            .await
            .expect_err("a blank stated name must be refused");

        assert_eq!(err.error_response().status(), http::StatusCode::BAD_REQUEST);
        assert!(
            !sync_dir.join(&namespace).exists(),
            "nothing may be created for a refused request"
        );

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// The addressed namespace holds to the narrower namespace rule, so a name only the repository
    /// position allows is a bad request rather than a server error.
    #[actix_web::test]
    async fn test_create_rejects_an_invalid_addressed_namespace() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let app_data = OxenAppData {
            path: sync_dir.clone(),
            config: Config::default(),
            test_mode: false,
        };

        // Valid in the repository position, and not in the namespace position.
        let namespace = "my.org";
        let data = RepoNew::from_namespace_name(namespace, Uuid::new_v4().to_string(), None);

        let resp = super::create_repo_response(&app_data, data)
            .await
            .expect("the error path builds a response");

        assert_eq!(resp.status(), http::StatusCode::BAD_REQUEST);
        assert!(
            !sync_dir.join(namespace).exists(),
            "nothing may be created for a refused request"
        );

        test::cleanup_sync_dir(&sync_dir)?;
        Ok(())
    }

    /// The destination a repository moves into holds to the namespace rule, so a request naming a
    /// destination a namespace cannot have must be refused before anything moves.
    #[actix_web::test]
    async fn test_transfer_rejects_an_invalid_destination_namespace() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let namespace = "Testing-Namespace";
        let repo_name = "Testing-Repo";

        let repo = test::create_local_repo(&sync_dir, namespace, repo_name)?;
        let repo_dir = repo.path.clone();

        let req = test::repo_request(&sync_dir, "/", namespace, repo_name);
        // Valid in the repository position, and not in the namespace position.
        let body = r#"{"namespace":"other.org"}"#.to_string();
        let err = super::transfer_namespace(req, body)
            .await
            .expect_err("an invalid destination namespace must be refused");

        assert_eq!(err.error_response().status(), http::StatusCode::BAD_REQUEST);
        assert!(
            repo_dir.exists(),
            "repo dir should not have moved: {repo_dir:?}"
        );
        assert!(
            !sync_dir.join("other.org").exists(),
            "the destination namespace must not be created"
        );

        test::cleanup_repo_and_sync_dir(repo, &sync_dir)?;
        Ok(())
    }

    /// Moving a repository relocates its directory, so a request stating a name a namespace cannot
    /// have must be refused before anything moves.
    #[actix_web::test]
    async fn test_transfer_rejects_an_invalid_stated_namespace_name() -> Result<(), OxenError> {
        let sync_dir = test::get_sync_dir()?;
        let namespace = "Testing-Namespace";
        let repo_name = "Testing-Repo";

        let repo = test::create_local_repo(&sync_dir, namespace, repo_name)?;
        let repo_dir = repo.path.clone();

        let req = test::repo_request(&sync_dir, "/", namespace, repo_name);
        // Valid in the repository position, and not in the namespace position.
        let body = r#"{"namespace":"Other-Namespace","namespace_name":"my.org"}"#.to_string();
        let err = super::transfer_namespace(req, body)
            .await
            .expect_err("an invalid stated namespace name must be refused");

        assert_eq!(err.error_response().status(), http::StatusCode::BAD_REQUEST);
        assert!(
            repo_dir.exists(),
            "repo dir should not have moved: {repo_dir:?}"
        );
        assert!(
            !sync_dir.join("Other-Namespace").join(repo_name).exists(),
            "the repo must not appear in the destination namespace"
        );

        test::cleanup_repo_and_sync_dir(repo, &sync_dir)?;
        Ok(())
    }
}