-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocUploadBundle.swift
150 lines (126 loc) · 4.67 KB
/
DocUploadBundle.swift
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
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
import Dependencies
public struct DocUploadBundle {
@Dependency(\.uuid) var uuid
public struct S3Folder: Codable, Equatable {
public var bucket: String
public var path: String
public init(bucket: String, path: String) {
self.bucket = bucket
self.path = path
}
}
public struct Repository {
var owner: String
var name: String
public init(owner: String, name: String) {
self.owner = owner
self.name = name
}
}
public struct Metadata: Codable, Equatable {
public var apiBaseURL: String
public var apiToken: String
public var buildId: UUID
public var docArchives: [DocArchive]
public var fileCount: Int?
public var linkablePathsCount: Int?
public var mbSize: Int?
/// Basename of the doc set source directory after unzipping. The value will be the source code revision, e.g. "1.2.3" or "main".
public var sourcePath: String
/// Target folder where the doc set will be synced to in S3.
public var targetFolder: S3Folder
}
let sourcePath: String
let bucket: String
let repository: Repository
let reference: String
let env: String
public let metadata: Metadata
public let s3Folder: S3Folder
var archiveName: String {
"\(env)-\(repository.owner)-\(repository.name)-\(reference.pathEncoded)-\(self.uuid().firstSegment).zip"
.lowercased()
}
public init(
sourcePath: String,
bucket: String,
repository: Repository,
reference: String,
apiBaseURL: String,
apiToken: String,
buildId: UUID,
docArchives: [DocArchive],
fileCount: Int? = nil,
linkablePathsCount: Int? = nil,
mbSize: Int? = nil
) {
self.sourcePath = sourcePath
self.bucket = bucket
self.repository = repository
self.reference = reference
self.env = bucket.droppingSPIPrefix().droppingDocsSuffix()
self.s3Folder = .init(
bucket: bucket,
path: "\(repository.owner)/\(repository.name)/\(reference.pathEncoded)".lowercased()
)
self.metadata = .init(
apiBaseURL: apiBaseURL,
apiToken: apiToken,
buildId: buildId,
docArchives: docArchives,
fileCount: fileCount,
linkablePathsCount: linkablePathsCount,
mbSize: mbSize,
sourcePath: URL(fileURLWithPath: sourcePath).lastPathComponent.lowercased(),
targetFolder: s3Folder
)
}
public func zip(to workDir: String, method: Zipper.Method = .library) throws -> String {
let archiveURL = URL(fileURLWithPath: "\(workDir)/\(archiveName)")
let metadataURL = URL(fileURLWithPath: "\(workDir)/metadata.json")
try JSONEncoder().encode(metadata).write(to: metadataURL)
try Zipper.zip(paths: [metadataURL, URL(fileURLWithPath: sourcePath)], to: archiveURL, method: method)
return archiveURL.path
}
public static func unzip(bundle: String, outputPath: String, fileOutputHandler: ((_ unzippedFile: URL) -> Void)? = nil) throws -> Metadata {
try Zipper.unzip(from: bundle, to: outputPath, fileOutputHandler: fileOutputHandler)
let metadataURL = URL(fileURLWithPath: "\(outputPath)/metadata.json")
let data = try Data(contentsOf: metadataURL)
return try JSONDecoder().decode(Metadata.self, from: data)
}
}
private extension UUID {
var firstSegment: String {
uuidString.components(separatedBy: "-").first!.lowercased()
}
}
private extension String {
func droppingSPIPrefix() -> String {
let prefix = "spi-"
if lowercased().hasPrefix(prefix) {
return String(dropFirst(prefix.count))
}
return self
}
func droppingDocsSuffix() -> String {
let suffix = "-docs"
if lowercased().hasSuffix(suffix) {
return String(dropLast(suffix.count))
}
return self
}
}