Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
CiottoliDev committed Sep 4, 2021
0 parents commit 4d91c55
Show file tree
Hide file tree
Showing 14 changed files with 2,033 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Swift

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
137 changes: 137 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
## Xcode User settings
xcuserdata/

## JeteBrains IDE
.idea/
cmake-build-*/
out/
.idea_modules/

## Obj-C/Swift specific
*.hmap

## Swift Package Manager

Packages/
Package.pins
Package.resolved
*.xcodeproj

.swiftpm

.build/

## Playgrounds
timeline.xctimeline
playground.xcworkspace

## MacOS
# General
.DS_Store
.AppleDouble
.LSOverride
*.icloud
*.nosync

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

## Linux
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*

##Desktop
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

## Dropbox settings and caches
.dropbox
.dropbox.attr
.dropbox.cache

## Misc
# Archives
# It's better to unpack these files and commit the raw source because
# git has its own built in compression methods.
*.7z
*.jar
*.rar
*.zip
*.gz
*.gzip
*.tgz
*.bzip
*.bzip2
*.bz2
*.xz
*.lzma
*.cab
*.xar

# Packing-only formats
*.iso
*.tar

# Package management formats
*.dmg
*.xpi
*.gem
*.egg
*.deb
*.rpm
*.msi
*.msm
*.msp
*.txz

*.pem
*.key
.env
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 FlowPay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "9a992ee3de1f8da9f2968fc96b26714834f3105f",
"version": "2.31.1"
}
},
{
"package": "XMLCoder",
"repositoryURL": "https://github.com/MaxDesiatov/XMLCoder.git",
"state": {
"branch": null,
"revision": "887de88b37b2d691d67db950770e09776229cf6d",
"version": "0.13.0"
}
}
]
},
"version": 1
}
28 changes: 28 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.4

import PackageDescription

let package = Package(
name: "FatturaElettronica-Swift",
platforms: [.macOS(.v10_15)],
products: [
.library(
name: "FatturaElettronica",
targets: ["FatturaElettronica"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
.package(url: "https://github.com/MaxDesiatov/XMLCoder.git", from: "0.11.1")
],
targets: [
.target(
name: "FatturaElettronica",
dependencies: [
.product(name: "XMLCoder", package: "XMLCoder"), .product(name: "NIO", package: "swift-nio"),
]
),
.testTarget(
name: "AppTests",
dependencies: ["FatturaElettronica"]),
]
)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# FatturaElettronica-Swift
A swift package which contains helper for handle and manage electronic invoices of Italian Public Administration
45 changes: 45 additions & 0 deletions Sources/FatturaElettronica/CustomDecoder/PABool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


@propertyWrapper
public struct PABool: Codable {

public var wrappedValue: Bool?

public init(wrappedValue: Bool?) {
self.wrappedValue = wrappedValue
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let bool = try? container.decode(Bool.self) {
self.wrappedValue = bool
} else if let string = try? container.decode(String.self) {

switch string.lowercased() {
case "si", "true", "s": self.wrappedValue = true
default: self.wrappedValue = false
}
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Impossible to decode Bool")
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.wrappedValue)
}
}

extension KeyedDecodingContainer {
public func decode(_ type: PABool.Type, forKey key: Self.Key) throws -> PABool {
return try decodeIfPresent(type, forKey: key) ?? PABool(wrappedValue: nil)
}
}

extension KeyedEncodingContainer {
public mutating func encode(_ value: PABool, forKey key: Self.Key) throws {
if value.wrappedValue != nil {
try value.encode(to: self.superEncoder(forKey: key))
}
}
}
103 changes: 103 additions & 0 deletions Sources/FatturaElettronica/FileHandler/FileHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// File.swift
//
//
// Created by Federico Giuntoli on 09/10/20.
//

import Foundation
import NIO

extension XMLHandler{

public enum FileType{
case xml
case p7m
case zip
}

private enum HandlerError: Error{
case contentNotValid
case fileNotFound
}

public func handle(_ file: Data, type: FileType) -> EventLoopFuture<FatturaElettronica> {
switch type {
case .p7m:
return handleP7m(file)
case .xml:
return handleXML(file)
case .zip:
return handleZip(file)
}
}

public func handle(_ xml: String) -> EventLoopFuture<FatturaElettronica> {
return self.xmlToInvoice(xml)
}

private func handleXML(_ data: Data) -> EventLoopFuture<FatturaElettronica> {
self.eventLoop.submit{
let content: String? = String(data: data, encoding: .utf8) ?? String(data: data, encoding: .ascii)

guard let content = content else { throw HandlerError.contentNotValid }
return content
}.flatMap{
return self.xmlToInvoice($0)
}
}

private func handleP7m(_ data: Data) -> EventLoopFuture<FatturaElettronica>{
let root = "./" + UUID().uuidString + "/"
let promise = self.eventLoop.submit{
try FileManager.default.createDirectory(atPath: root, withIntermediateDirectories: true)
let p7m = URL(fileURLWithPath: root + "invoice.xml.p7m")
try data.write(to: p7m)
return p7m
}.flatMap{
self.decryptP7M(filePath: $0)
}.flatMap{
self.xmlToInvoice($0)
}

promise.whenComplete{ _ in
try? FileManager.default.removeItem(atPath: root)
}

return promise

}
private func handleZip(_ data: Data) -> EventLoopFuture<FatturaElettronica>{

let root = "./" + UUID().uuidString + "/"
let promise = self.eventLoop.submit{
try FileManager.default.createDirectory(atPath: root, withIntermediateDirectories: true)
let zip = URL(fileURLWithPath: root + "archive.zip")
try data.write(to: zip)
return zip
}
.flatMap{
self.unzip($0)
}
.map{
FileManager.default.enumerator(atPath: root)?
.allObjects
.compactMap {$0 as? String }
.filter{ $0.hasSuffix(".p7m") }
.map { URL(fileURLWithPath: root + $0) }
.first
}
.unwrap(orError: HandlerError.fileNotFound)
.flatMap{
self.decryptP7M(filePath: $0)
}.flatMap{
self.xmlToInvoice($0)
}

promise.whenComplete{ _ in
try? FileManager.default.removeItem(atPath: root)
}

return promise
}
}
Loading

0 comments on commit 4d91c55

Please sign in to comment.