fission-command-package 0.6.3

Packaging, readiness, static hosting, and distribution workflows for the fission command
Documentation
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
use super::*;
use anyhow::{bail, Context, Result};
use aws_config::{BehaviorVersion, Region};
use aws_sdk_s3::primitives::ByteStream;
use aws_sdk_s3::types::ObjectCannedAcl;
use fission_credentials as credentials;
use reqwest::blocking::Client;
use reqwest::header::{CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, LOCATION};
use serde_json::{json, Value};
use std::fs;
use std::path::{Path, PathBuf};

const DROPBOX_SIMPLE_UPLOAD_LIMIT: u64 = 150 * 1024 * 1024;
const DROPBOX_CHUNK_SIZE: usize = 8 * 1024 * 1024;

struct UploadItem {
    path: PathBuf,
    relative_path: String,
    mime_type: String,
}

struct UploadedFile {
    relative_path: String,
    provider_id: Option<String>,
    url: Option<String>,
}

pub(super) fn publish_s3(
    options: &DistributeOptions,
    config: &PublishManifest,
    artifact_path: &Path,
    manifest: &ArtifactManifest,
) -> Result<DistributionReceipt> {
    let cfg = s3_config(config, &options.site)?;
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .context("failed to create S3 upload runtime")?;
    let uploaded = rt.block_on(upload_s3(&cfg, manifest, artifact_path))?;
    let canonical_url =
        s3_canonical_url(&cfg, uploaded.first().and_then(|file| file.url.as_deref()));
    Ok(upload_receipt(
        options,
        artifact_path,
        "s3",
        "published",
        canonical_url,
        uploaded,
    ))
}

pub(super) fn publish_google_drive(
    options: &DistributeOptions,
    config: &PublishManifest,
    artifact_path: &Path,
    manifest: &ArtifactManifest,
) -> Result<DistributionReceipt> {
    let cfg = google_drive_config(config, &options.site)?;
    let token = credentials::provider_secret(
        DistributionProvider::GoogleDrive,
        &["GOOGLE_DRIVE_ACCESS_TOKEN"],
    )?
    .context("Google Drive upload requires GOOGLE_DRIVE_ACCESS_TOKEN or a stored google-drive credential")?;
    let client = Client::new();
    let mut uploaded = Vec::new();
    for item in upload_items(manifest, artifact_path)? {
        uploaded.push(upload_google_drive_item(&client, &token, &cfg, &item)?);
    }
    Ok(upload_receipt(
        options,
        artifact_path,
        "google-drive",
        "published",
        uploaded.iter().find_map(|file| file.url.clone()),
        uploaded,
    ))
}

pub(super) fn publish_onedrive(
    options: &DistributeOptions,
    config: &PublishManifest,
    artifact_path: &Path,
    manifest: &ArtifactManifest,
) -> Result<DistributionReceipt> {
    let cfg = onedrive_config(config, &options.site)?;
    let token =
        credentials::provider_secret(DistributionProvider::OneDrive, &["ONEDRIVE_ACCESS_TOKEN"])?
            .context(
            "OneDrive upload requires ONEDRIVE_ACCESS_TOKEN or a stored onedrive credential",
        )?;
    let client = Client::new();
    let mut uploaded = Vec::new();
    for item in upload_items(manifest, artifact_path)? {
        uploaded.push(upload_onedrive_item(&client, &token, &cfg, &item)?);
    }
    Ok(upload_receipt(
        options,
        artifact_path,
        "onedrive",
        "published",
        uploaded.iter().find_map(|file| file.url.clone()),
        uploaded,
    ))
}

pub(super) fn publish_dropbox(
    options: &DistributeOptions,
    config: &PublishManifest,
    artifact_path: &Path,
    manifest: &ArtifactManifest,
) -> Result<DistributionReceipt> {
    let cfg = dropbox_config(config, &options.site)?;
    let token =
        credentials::provider_secret(DistributionProvider::Dropbox, &["DROPBOX_ACCESS_TOKEN"])?
            .context(
                "Dropbox upload requires DROPBOX_ACCESS_TOKEN or a stored dropbox credential",
            )?;
    let client = Client::new();
    let mut uploaded = Vec::new();
    for item in upload_items(manifest, artifact_path)? {
        uploaded.push(upload_dropbox_item(&client, &token, &cfg, &item)?);
    }
    Ok(upload_receipt(
        options,
        artifact_path,
        "dropbox",
        "published",
        uploaded.iter().find_map(|file| file.url.clone()),
        uploaded,
    ))
}

pub(super) fn s3_status(
    options: &DistributeOptions,
    config: &PublishManifest,
) -> Result<DistributionReceipt> {
    let cfg = s3_config(config, &options.site)?;
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .context("failed to create S3 status runtime")?;
    let value = rt.block_on(s3_status_value(&cfg))?;
    Ok(file_status_receipt(
        options,
        "s3",
        "ok",
        s3_canonical_url(&cfg, None),
        value,
    ))
}

pub(super) fn google_drive_status(
    options: &DistributeOptions,
    config: &PublishManifest,
) -> Result<DistributionReceipt> {
    let cfg = google_drive_config(config, &options.site)?;
    let token = credentials::provider_secret(
        DistributionProvider::GoogleDrive,
        &["GOOGLE_DRIVE_ACCESS_TOKEN"],
    )?
    .context("Google Drive status requires GOOGLE_DRIVE_ACCESS_TOKEN or a stored google-drive credential")?;
    let client = Client::new();
    let value = if let Some(folder_id) = cfg.folder_id.as_deref().filter(|value| !value.is_empty())
    {
        let response = client
            .get(format!("https://www.googleapis.com/drive/v3/files/{folder_id}?fields=id,name,webViewLink,capabilities"))
            .bearer_auth(token.trim())
            .send()
            .context("failed to query Google Drive folder")?;
        json_http_response(response, "Google Drive folder status")?
    } else {
        let response = client
            .get("https://www.googleapis.com/drive/v3/about?fields=user,storageQuota")
            .bearer_auth(token.trim())
            .send()
            .context("failed to query Google Drive account")?;
        json_http_response(response, "Google Drive account status")?
    };
    Ok(file_status_receipt(
        options,
        "google-drive",
        "ok",
        value
            .get("webViewLink")
            .and_then(Value::as_str)
            .map(str::to_string),
        value,
    ))
}

pub(super) fn onedrive_status(
    options: &DistributeOptions,
    config: &PublishManifest,
) -> Result<DistributionReceipt> {
    let cfg = onedrive_config(config, &options.site)?;
    let token =
        credentials::provider_secret(DistributionProvider::OneDrive, &["ONEDRIVE_ACCESS_TOKEN"])?
            .context(
            "OneDrive status requires ONEDRIVE_ACCESS_TOKEN or a stored onedrive credential",
        )?;
    let root = cfg
        .root
        .as_deref()
        .unwrap_or("me/drive/root")
        .trim_matches('/');
    let url = if let Some(prefix) = cfg
        .path_prefix
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        format!(
            "https://graph.microsoft.com/v1.0/{root}:/{}/",
            encode_path(prefix.trim_matches('/'))
        )
    } else {
        format!("https://graph.microsoft.com/v1.0/{root}")
    };
    let response = Client::new()
        .get(url)
        .bearer_auth(token.trim())
        .send()
        .context("failed to query OneDrive destination")?;
    let value = json_http_response(response, "OneDrive destination status")?;
    Ok(file_status_receipt(
        options,
        "onedrive",
        "ok",
        value
            .get("webUrl")
            .and_then(Value::as_str)
            .map(str::to_string),
        value,
    ))
}

pub(super) fn dropbox_status(
    options: &DistributeOptions,
    _config: &PublishManifest,
) -> Result<DistributionReceipt> {
    let token =
        credentials::provider_secret(DistributionProvider::Dropbox, &["DROPBOX_ACCESS_TOKEN"])?
            .context(
                "Dropbox status requires DROPBOX_ACCESS_TOKEN or a stored dropbox credential",
            )?;
    let response = Client::new()
        .post("https://api.dropboxapi.com/2/users/get_current_account")
        .bearer_auth(token.trim())
        .send()
        .context("failed to query Dropbox account")?;
    let value = json_http_response(response, "Dropbox account status")?;
    Ok(file_status_receipt(options, "dropbox", "ok", None, value))
}

pub(super) fn readiness_s3(
    site: &str,
    config: &PublishManifest,
    checks: &mut Vec<ReadinessCheck>,
) -> Result<()> {
    let cfg = s3_config(config, site)?;
    checks.push(required_value(
        "release.s3.bucket_configured",
        cfg.bucket.as_deref(),
        "S3 bucket is configured",
        "Set distribution.s3.<site>.bucket.",
    ));
    checks.push(secret_check(
        "release.s3.credentials_available",
        DistributionProvider::S3,
        &["AWS_PROFILE", "AWS_ACCESS_KEY_ID"],
        "AWS/S3 credentials are available",
        "Set AWS_PROFILE, AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY, or import S3 credentials into the Fission vault.",
    ));
    checks.push(check(
        "release.s3.direct_rust_backend",
        CheckSeverity::Info,
        CheckStatus::Passed,
        "S3 upload uses the Rust AWS SDK backend",
        Some(format!(
            "endpoint = {}, path_style = {}, visibility = {}, presign_ttl_seconds = {}",
            cfg.endpoint.as_deref().unwrap_or("<provider default>"),
            cfg.path_style.unwrap_or(false),
            cfg.visibility.as_deref().unwrap_or("private"),
            cfg.presign_ttl_seconds
                .map(|value| value.to_string())
                .unwrap_or_else(|| "<none>".to_string())
        )),
        Vec::new(),
    ));
    Ok(())
}

pub(super) fn readiness_google_drive(
    site: &str,
    config: &PublishManifest,
    checks: &mut Vec<ReadinessCheck>,
) -> Result<()> {
    let cfg = google_drive_config(config, site)?;
    checks.push(secret_check(
        "release.google_drive.token_available",
        DistributionProvider::GoogleDrive,
        &["GOOGLE_DRIVE_ACCESS_TOKEN"],
        "Google Drive OAuth token is available",
        "Run `fission auth import google-drive --from env:GOOGLE_DRIVE_ACCESS_TOKEN --yes` or set GOOGLE_DRIVE_ACCESS_TOKEN in CI.",
    ));
    checks.push(check(
        "release.google_drive.folder_selected",
        CheckSeverity::Info,
        CheckStatus::Passed,
        "Google Drive folder destination is selected",
        Some(
            cfg.folder_id
                .unwrap_or_else(|| "root drive folder".to_string()),
        ),
        Vec::new(),
    ));
    Ok(())
}

pub(super) fn readiness_onedrive(
    site: &str,
    config: &PublishManifest,
    checks: &mut Vec<ReadinessCheck>,
) -> Result<()> {
    let cfg = onedrive_config(config, site)?;
    checks.push(secret_check(
        "release.onedrive.token_available",
        DistributionProvider::OneDrive,
        &["ONEDRIVE_ACCESS_TOKEN"],
        "OneDrive OAuth token is available",
        "Run `fission auth import onedrive --from env:ONEDRIVE_ACCESS_TOKEN --yes` or set ONEDRIVE_ACCESS_TOKEN in CI.",
    ));
    checks.push(check(
        "release.onedrive.path_selected",
        CheckSeverity::Info,
        CheckStatus::Passed,
        "OneDrive upload path is selected",
        Some(
            cfg.path_prefix
                .unwrap_or_else(|| "Fission releases".to_string()),
        ),
        Vec::new(),
    ));
    Ok(())
}

pub(super) fn readiness_dropbox(
    site: &str,
    config: &PublishManifest,
    checks: &mut Vec<ReadinessCheck>,
) -> Result<()> {
    let cfg = dropbox_config(config, site)?;
    checks.push(secret_check(
        "release.dropbox.token_available",
        DistributionProvider::Dropbox,
        &["DROPBOX_ACCESS_TOKEN"],
        "Dropbox OAuth token is available",
        "Run `fission auth import dropbox --from env:DROPBOX_ACCESS_TOKEN --yes` or set DROPBOX_ACCESS_TOKEN in CI.",
    ));
    checks.push(check(
        "release.dropbox.path_selected",
        CheckSeverity::Info,
        CheckStatus::Passed,
        "Dropbox upload path is selected",
        Some(
            cfg.path_prefix
                .unwrap_or_else(|| "/Fission releases".to_string()),
        ),
        Vec::new(),
    ));
    Ok(())
}

async fn s3_status_value(cfg: &S3Config) -> Result<Value> {
    let bucket = cfg
        .bucket
        .as_deref()
        .context("distribution.s3.<site>.bucket is required")?;
    let mut loader = aws_config::defaults(BehaviorVersion::latest());
    if let Some(region) = cfg
        .region
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        loader = loader.region(Region::new(region.to_string()));
    }
    if let Some(profile) = cfg
        .profile
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        loader = loader.profile_name(profile);
    }
    if let Some(endpoint) = cfg
        .endpoint
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        loader = loader.endpoint_url(endpoint);
    }
    let shared = loader.load().await;
    let mut builder = aws_sdk_s3::config::Builder::from(&shared);
    if cfg.path_style.unwrap_or(false) {
        builder = builder.force_path_style(true);
    }
    let client = aws_sdk_s3::Client::from_conf(builder.build());
    let prefix = normalized_prefix(cfg.prefix.as_deref());
    let result = client
        .list_objects_v2()
        .bucket(bucket)
        .prefix(prefix.clone())
        .max_keys(10)
        .send()
        .await
        .with_context(|| format!("failed to list s3://{bucket}/{prefix}"))?;
    Ok(json!({
        "bucket": bucket,
        "prefix": prefix,
        "key_count": result.key_count(),
        "objects": result.contents().iter().map(|object| json!({
            "key": object.key(),
            "size": object.size(),
            "etag": object.e_tag(),
        })).collect::<Vec<_>>()
    }))
}

fn json_http_response(response: reqwest::blocking::Response, operation: &str) -> Result<Value> {
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), operation)?;
    serde_json::from_str(&text)
        .with_context(|| format!("failed to parse {operation} response: {text}"))
}

fn file_status_receipt(
    options: &DistributeOptions,
    provider: &str,
    status: &str,
    canonical_url: Option<String>,
    value: Value,
) -> DistributionReceipt {
    DistributionReceipt {
        schema_version: 1,
        created_at_unix_seconds: now_unix_seconds(),
        provider: provider.to_string(),
        site: options.site.clone(),
        action: "status".to_string(),
        artifact_manifest: None,
        deployment_id: options.deploy.clone(),
        canonical_url,
        preview_url: None,
        custom_domain: None,
        status: status.to_string(),
        stdout: serde_json::to_string_pretty(&value).ok(),
        stderr: None,
        manual_follow_up: Vec::new(),
    }
}

async fn upload_s3(
    cfg: &S3Config,
    manifest: &ArtifactManifest,
    artifact_path: &Path,
) -> Result<Vec<UploadedFile>> {
    let bucket = cfg
        .bucket
        .as_deref()
        .context("distribution.s3.<site>.bucket is required")?;
    let mut loader = aws_config::defaults(BehaviorVersion::latest());
    if let Some(region) = cfg
        .region
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        loader = loader.region(Region::new(region.to_string()));
    }
    if let Some(profile) = cfg
        .profile
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        loader = loader.profile_name(profile);
    }
    if let Some(endpoint) = cfg
        .endpoint
        .as_deref()
        .filter(|value| !value.trim().is_empty())
    {
        loader = loader.endpoint_url(endpoint);
    }
    let shared = loader.load().await;
    let mut builder = aws_sdk_s3::config::Builder::from(&shared);
    if cfg.path_style.unwrap_or(false) {
        builder = builder.force_path_style(true);
    }
    let client = aws_sdk_s3::Client::from_conf(builder.build());
    let prefix = normalized_prefix(cfg.prefix.as_deref());
    let mut uploaded = Vec::new();
    for item in upload_items(manifest, artifact_path)? {
        let key = format!("{prefix}{}", item.relative_path);
        let body = ByteStream::from_path(&item.path)
            .await
            .with_context(|| format!("failed to open {} for S3 upload", item.path.display()))?;
        let mut request = client
            .put_object()
            .bucket(bucket)
            .key(&key)
            .body(body)
            .content_type(item.mime_type.clone());
        if cfg.visibility.as_deref() == Some("public") {
            request = request.acl(ObjectCannedAcl::PublicRead);
        }
        request.send().await.with_context(|| {
            format!(
                "failed to upload {} to s3://{bucket}/{key}",
                item.path.display()
            )
        })?;
        uploaded.push(UploadedFile {
            relative_path: item.relative_path,
            provider_id: Some(format!("s3://{bucket}/{key}")),
            url: s3_object_url(cfg, bucket, &key),
        });
    }
    Ok(uploaded)
}

fn upload_google_drive_item(
    client: &Client,
    token: &str,
    cfg: &GoogleDriveConfig,
    item: &UploadItem,
) -> Result<UploadedFile> {
    let metadata = if let Some(folder_id) =
        cfg.folder_id.as_deref().filter(|value| !value.is_empty())
    {
        json!({ "name": drive_name(cfg.name_prefix.as_deref(), &item.relative_path), "parents": [folder_id] })
    } else {
        json!({ "name": drive_name(cfg.name_prefix.as_deref(), &item.relative_path) })
    };
    let len = fs::metadata(&item.path)?.len();
    let response = client
        .post("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&fields=id,name,webViewLink,webContentLink")
        .bearer_auth(token.trim())
        .header(CONTENT_TYPE, "application/json; charset=utf-8")
        .header("X-Upload-Content-Type", item.mime_type.as_str())
        .header("X-Upload-Content-Length", len.to_string())
        .json(&metadata)
        .send()
        .context("failed to start Google Drive resumable upload")?;
    let status = response.status();
    let location = response_location(&response)?;
    if !status.is_success() {
        bail!(
            "Google Drive upload start failed with {status}: {}",
            response.text()?
        );
    }
    let bytes = fs::read(&item.path)?;
    let response = client
        .put(location)
        .header(CONTENT_TYPE, item.mime_type.as_str())
        .header(CONTENT_LENGTH, bytes.len().to_string())
        .body(bytes)
        .send()
        .context("failed to upload file bytes to Google Drive")?;
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), "Google Drive upload")?;
    let value: Value =
        serde_json::from_str(&text).context("failed to parse Google Drive upload response")?;
    let id = value.get("id").and_then(Value::as_str).map(str::to_string);
    if cfg.share.unwrap_or(false) {
        if let Some(id) = id.as_deref() {
            let response = client
                .post(format!(
                    "https://www.googleapis.com/drive/v3/files/{id}/permissions"
                ))
                .bearer_auth(token.trim())
                .json(&json!({ "type": "anyone", "role": "reader" }))
                .send()
                .context("failed to create Google Drive sharing permission")?;
            let status = response.status();
            let text = response.text()?;
            ensure_success(status, text, "Google Drive sharing permission")?;
        }
    }
    Ok(UploadedFile {
        relative_path: item.relative_path.clone(),
        provider_id: id,
        url: value
            .get("webViewLink")
            .or_else(|| value.get("webContentLink"))
            .and_then(Value::as_str)
            .map(str::to_string),
    })
}

fn upload_onedrive_item(
    client: &Client,
    token: &str,
    cfg: &OneDriveConfig,
    item: &UploadItem,
) -> Result<UploadedFile> {
    let root = cfg
        .root
        .as_deref()
        .unwrap_or("me/drive/root")
        .trim_matches('/');
    let upload_path = joined_remote_path(cfg.path_prefix.as_deref(), &item.relative_path)
        .trim_start_matches('/')
        .to_string();
    let url = format!(
        "https://graph.microsoft.com/v1.0/{root}:/{}/createUploadSession",
        encode_path(&upload_path)
    );
    let conflict = cfg.conflict_behavior.as_deref().unwrap_or("replace");
    let response = client
        .post(url)
        .bearer_auth(token.trim())
        .json(&json!({ "item": { "@microsoft.graph.conflictBehavior": conflict } }))
        .send()
        .context("failed to create OneDrive upload session")?;
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), "OneDrive upload session")?;
    let value: Value =
        serde_json::from_str(&text).context("failed to parse OneDrive upload session")?;
    let upload_url = value
        .get("uploadUrl")
        .and_then(Value::as_str)
        .context("OneDrive upload session response did not contain uploadUrl")?;
    let bytes = fs::read(&item.path)?;
    if bytes.is_empty() {
        bail!(
            "OneDrive upload does not support empty file {} yet",
            item.path.display()
        );
    }
    let range = format!("bytes 0-{}/{}", bytes.len() - 1, bytes.len());
    let response = client
        .put(upload_url)
        .header(CONTENT_LENGTH, bytes.len().to_string())
        .header(CONTENT_RANGE, range)
        .body(bytes)
        .send()
        .context("failed to upload file bytes to OneDrive")?;
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), "OneDrive upload")?;
    let value: Value =
        serde_json::from_str(&text).context("failed to parse OneDrive upload response")?;
    Ok(UploadedFile {
        relative_path: item.relative_path.clone(),
        provider_id: value.get("id").and_then(Value::as_str).map(str::to_string),
        url: value
            .get("webUrl")
            .and_then(Value::as_str)
            .map(str::to_string),
    })
}

fn upload_dropbox_item(
    client: &Client,
    token: &str,
    cfg: &DropboxConfig,
    item: &UploadItem,
) -> Result<UploadedFile> {
    let remote_path = joined_remote_path(cfg.path_prefix.as_deref(), &item.relative_path);
    let size = fs::metadata(&item.path)?.len();
    if size <= DROPBOX_SIMPLE_UPLOAD_LIMIT {
        upload_dropbox_simple(client, token, cfg, item, &remote_path)
    } else {
        upload_dropbox_session(client, token, cfg, item, &remote_path)
    }
}

fn upload_dropbox_simple(
    client: &Client,
    token: &str,
    cfg: &DropboxConfig,
    item: &UploadItem,
    remote_path: &str,
) -> Result<UploadedFile> {
    let mode = cfg.mode.as_deref().unwrap_or("overwrite");
    let arg = json!({
        "path": remote_path,
        "mode": mode,
        "autorename": cfg.autorename.unwrap_or(false),
        "mute": false,
        "strict_conflict": false
    });
    let response = client
        .post("https://content.dropboxapi.com/2/files/upload")
        .bearer_auth(token.trim())
        .header("Dropbox-API-Arg", arg.to_string())
        .header(CONTENT_TYPE, "application/octet-stream")
        .body(fs::read(&item.path)?)
        .send()
        .context("failed to upload file to Dropbox")?;
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), "Dropbox upload")?;
    let value: Value =
        serde_json::from_str(&text).context("failed to parse Dropbox upload response")?;
    Ok(UploadedFile {
        relative_path: item.relative_path.clone(),
        provider_id: value.get("id").and_then(Value::as_str).map(str::to_string),
        url: value
            .get("path_display")
            .and_then(Value::as_str)
            .map(str::to_string),
    })
}

fn upload_dropbox_session(
    client: &Client,
    token: &str,
    cfg: &DropboxConfig,
    item: &UploadItem,
    remote_path: &str,
) -> Result<UploadedFile> {
    let bytes = fs::read(&item.path)?;
    let first_len = DROPBOX_CHUNK_SIZE.min(bytes.len());
    let response = client
        .post("https://content.dropboxapi.com/2/files/upload_session/start")
        .bearer_auth(token.trim())
        .header("Dropbox-API-Arg", json!({"close": false}).to_string())
        .header(CONTENT_TYPE, "application/octet-stream")
        .body(bytes[..first_len].to_vec())
        .send()
        .context("failed to start Dropbox upload session")?;
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), "Dropbox upload session start")?;
    let value: Value =
        serde_json::from_str(&text).context("failed to parse Dropbox session response")?;
    let session_id = value
        .get("session_id")
        .and_then(Value::as_str)
        .context("Dropbox upload session did not return session_id")?;
    let mut offset = first_len;
    while offset + DROPBOX_CHUNK_SIZE < bytes.len() {
        let next = offset + DROPBOX_CHUNK_SIZE;
        let arg = json!({"cursor": {"session_id": session_id, "offset": offset}});
        let response = client
            .post("https://content.dropboxapi.com/2/files/upload_session/append_v2")
            .bearer_auth(token.trim())
            .header("Dropbox-API-Arg", arg.to_string())
            .header(CONTENT_TYPE, "application/octet-stream")
            .body(bytes[offset..next].to_vec())
            .send()
            .context("failed to append Dropbox upload session")?;
        let status = response.status();
        let text = response.text()?;
        ensure_success(status, text, "Dropbox upload session append")?;
        offset = next;
    }
    let mode = cfg.mode.as_deref().unwrap_or("overwrite");
    let arg = json!({
        "cursor": {"session_id": session_id, "offset": offset},
        "commit": {
            "path": remote_path,
            "mode": mode,
            "autorename": cfg.autorename.unwrap_or(false),
            "mute": false,
            "strict_conflict": false
        }
    });
    let response = client
        .post("https://content.dropboxapi.com/2/files/upload_session/finish")
        .bearer_auth(token.trim())
        .header("Dropbox-API-Arg", arg.to_string())
        .header(CONTENT_TYPE, "application/octet-stream")
        .body(bytes[offset..].to_vec())
        .send()
        .context("failed to finish Dropbox upload session")?;
    let status = response.status();
    let text = response.text()?;
    ensure_success(status, text.clone(), "Dropbox upload session finish")?;
    let value: Value =
        serde_json::from_str(&text).context("failed to parse Dropbox finish response")?;
    Ok(UploadedFile {
        relative_path: item.relative_path.clone(),
        provider_id: value.get("id").and_then(Value::as_str).map(str::to_string),
        url: value
            .get("path_display")
            .and_then(Value::as_str)
            .map(str::to_string),
    })
}

fn upload_receipt(
    options: &DistributeOptions,
    artifact_path: &Path,
    provider: &str,
    status: &str,
    canonical_url: Option<String>,
    uploaded: Vec<UploadedFile>,
) -> DistributionReceipt {
    let stdout = serde_json::to_string_pretty(&json!({
        "uploaded": uploaded.iter().map(|file| json!({
            "relative_path": file.relative_path,
            "provider_id": file.provider_id,
            "url": file.url,
        })).collect::<Vec<_>>()
    }))
    .ok();
    DistributionReceipt {
        schema_version: 1,
        created_at_unix_seconds: now_unix_seconds(),
        provider: provider.to_string(),
        site: options.site.clone(),
        action: "publish".to_string(),
        artifact_manifest: Some(artifact_path.display().to_string()),
        deployment_id: options.deploy.clone(),
        canonical_url,
        preview_url: uploaded.iter().find_map(|file| file.url.clone()),
        custom_domain: None,
        status: status.to_string(),
        stdout,
        stderr: None,
        manual_follow_up: Vec::new(),
    }
}

fn upload_items(manifest: &ArtifactManifest, artifact_path: &Path) -> Result<Vec<UploadItem>> {
    let mut items = manifest
        .artifacts
        .iter()
        .map(|file| UploadItem {
            path: PathBuf::from(&file.path),
            relative_path: file.relative_path.clone(),
            mime_type: file.mime_type.clone(),
        })
        .collect::<Vec<_>>();
    items.push(UploadItem {
        path: artifact_path.to_path_buf(),
        relative_path: ARTIFACT_MANIFEST.to_string(),
        mime_type: content_type(artifact_path).to_string(),
    });
    Ok(items)
}

fn secret_check(
    id: &str,
    provider: DistributionProvider,
    env_names: &[&str],
    summary: &str,
    remediation: &str,
) -> ReadinessCheck {
    let found_env = env_names
        .iter()
        .find(|name| std::env::var_os(name).is_some());
    let found_secret = credentials::provider_secret(provider, env_names)
        .ok()
        .flatten()
        .is_some();
    check(
        id,
        CheckSeverity::Error,
        if found_env.is_some() || found_secret {
            CheckStatus::Passed
        } else {
            CheckStatus::Missing
        },
        summary,
        found_env
            .map(|name| format!("environment: {name}"))
            .or_else(|| found_secret.then(|| "vault credential".to_string())),
        vec![remediation],
    )
}

fn ensure_success(status: reqwest::StatusCode, body: String, context: &str) -> Result<()> {
    if status.is_success() {
        Ok(())
    } else {
        bail!("{context} failed with {status}: {body}")
    }
}

fn response_location(response: &reqwest::blocking::Response) -> Result<String> {
    response
        .headers()
        .get(LOCATION)
        .and_then(|value| value.to_str().ok())
        .map(str::to_string)
        .context("resumable upload response did not include Location header")
}

fn normalized_prefix(prefix: Option<&str>) -> String {
    prefix
        .unwrap_or("")
        .trim_matches('/')
        .split('/')
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>()
        .join("/")
        .pipe(|value| {
            if value.is_empty() {
                value
            } else {
                format!("{value}/")
            }
        })
}

fn joined_remote_path(prefix: Option<&str>, relative: &str) -> String {
    let prefix = prefix.unwrap_or("").trim_matches('/');
    let relative = relative.trim_start_matches('/');
    if prefix.is_empty() {
        format!("/{relative}")
    } else {
        format!("/{prefix}/{relative}")
    }
}

fn drive_name(prefix: Option<&str>, relative: &str) -> String {
    let name = relative.replace('/', "__");
    match prefix.map(str::trim).filter(|value| !value.is_empty()) {
        Some(prefix) => format!("{prefix}-{name}"),
        None => name,
    }
}

fn encode_path(path: &str) -> String {
    path.split('/')
        .map(percent_encode_segment)
        .collect::<Vec<_>>()
        .join("/")
}

fn percent_encode_segment(segment: &str) -> String {
    let mut out = String::new();
    for byte in segment.as_bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(*byte as char)
            }
            _ => out.push_str(&format!("%{byte:02X}")),
        }
    }
    out
}

fn s3_object_url(cfg: &S3Config, bucket: &str, key: &str) -> Option<String> {
    if cfg.visibility.as_deref() != Some("public") {
        return None;
    }
    if let Some(endpoint) = cfg
        .endpoint
        .as_deref()
        .filter(|value| value.starts_with("http"))
    {
        if cfg.path_style.unwrap_or(false) {
            Some(format!(
                "{}/{}/{}",
                endpoint.trim_end_matches('/'),
                bucket,
                key
            ))
        } else {
            let endpoint = endpoint
                .trim_start_matches("https://")
                .trim_start_matches("http://")
                .trim_end_matches('/');
            Some(format!("https://{bucket}.{endpoint}/{key}"))
        }
    } else {
        let region = cfg.region.as_deref().unwrap_or("us-east-1");
        Some(format!("https://{bucket}.s3.{region}.amazonaws.com/{key}"))
    }
}

fn s3_canonical_url(cfg: &S3Config, fallback: Option<&str>) -> Option<String> {
    cfg.bucket.as_deref().and_then(|bucket| {
        let prefix = normalized_prefix(cfg.prefix.as_deref());
        if prefix.is_empty() {
            fallback.map(str::to_string)
        } else {
            s3_object_url(cfg, bucket, &prefix)
        }
    })
}

trait Pipe: Sized {
    fn pipe<T>(self, f: impl FnOnce(Self) -> T) -> T {
        f(self)
    }
}
impl<T> Pipe for T {}