Skip to content

Commit

Permalink
prepare for antivirus
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Mar 17, 2023
1 parent 3c11349 commit 10d39db
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 2 deletions.
109 changes: 109 additions & 0 deletions pkg/bytesize/bytesize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2018-2022 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// Package bytesize provides easy conversions from human readable strings (eg. 10MB) to bytes
package bytesize

import (
"fmt"
"strconv"
"strings"
)

// ByteSize is the size in bytes
type ByteSize uint64

// List of available byte sizes
// NOTE: max is exabyte as we convert to uint64
const (
KB ByteSize = 1000
MB ByteSize = 1000 * KB
GB ByteSize = 1000 * MB
TB ByteSize = 1000 * GB
PB ByteSize = 1000 * TB
EB ByteSize = 1000 * PB

KiB ByteSize = 1024
MiB ByteSize = 1024 * KiB
GiB ByteSize = 1024 * MiB
TiB ByteSize = 1024 * GiB
PiB ByteSize = 1024 * TiB
EiB ByteSize = 1024 * PiB
)

// Parse parses a Bytesize from a string
func Parse(s string) (ByteSize, error) {
sanitized := strings.TrimSpace(s)
if !strings.HasSuffix(sanitized, "B") {
u, err := strconv.Atoi(sanitized)
return ByteSize(u), err
}

var (
value int
unit string
)

template := "%d%s"
_, err := fmt.Sscanf(sanitized, template, &value, &unit)
if err != nil {
return 0, err
}

bytes := ByteSize(value)
switch unit {
case "KB":
bytes *= KB
case "MB":
bytes *= MB
case "GB":
bytes *= GB
case "TB":
bytes *= TB
case "PB":
bytes *= PB
case "EB":
bytes *= EB
case "KiB":
bytes *= KiB
case "MiB":
bytes *= MiB
case "GiB":
bytes *= GiB
case "TiB":
bytes *= TiB
case "PiB":
bytes *= PiB
case "EiB":
bytes *= EiB
default:
return 0, fmt.Errorf("unknown unit '%s'. Use common abbreviations such as KB, MiB, GB", unit)
}

return bytes, nil
}

// Bytes converts the ByteSize to an uint64
func (b ByteSize) Bytes() uint64 {
return uint64(b)
}

// String converts the ByteSize to a string
func (b ByteSize) String() string {
return strconv.FormatUint(uint64(b), 10)
}
124 changes: 124 additions & 0 deletions pkg/bytesize/bytesize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2018-2022 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package bytesize_test

import (
"fmt"
"testing"

"github.com/cs3org/reva/v2/pkg/bytesize"
"github.com/test-go/testify/require"
)

func TestParseSpecial(t *testing.T) {
testCases := []struct {
Alias string
Input string
ExpectedOutput uint64
ExpectError bool
}{
{
Alias: "it assumes bytes",
Input: "100",
ExpectedOutput: 100,
},
{
Alias: "it accepts a space between value and unit",
Input: "1 MB",
ExpectedOutput: 1000000,
},
{
Alias: "it accepts also more spaces between value and unit",
Input: "1 MB",
ExpectedOutput: 1000000,
},
{
Alias: "it ignores trailing and leading spaces",
Input: " 1MB ",
ExpectedOutput: 1000000,
},
{
Alias: "it errors on unknown units",
Input: "1SB",
ExpectedOutput: 0,
ExpectError: true,
},
{
Alias: "it multiplies correctly",
Input: "16MB",
ExpectedOutput: 16000000,
},
{
Alias: "it errors when no value is given",
Input: "GB",
ExpectedOutput: 0,
ExpectError: true,
},
{
Alias: "it errors when bad input is given",
Input: ",as!@@delta",
ExpectedOutput: 0,
ExpectError: true,
},
{
Alias: "it errors when using floats",
Input: "1.024GB",
ExpectedOutput: 0,
ExpectError: true,
},
}

for _, tc := range testCases {
actual, err := bytesize.Parse(tc.Input)
if tc.ExpectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}

require.Equal(t, tc.ExpectError, err != nil, tc.Alias)
require.Equal(t, int(tc.ExpectedOutput), int(actual), tc.Alias)
}
}

func TestParseHappy(t *testing.T) {
testCases := []struct {
Input string
Expected uint64
}{
{Input: "1", Expected: 1},
{Input: "1KB", Expected: 1000},
{Input: "1MB", Expected: 1000000},
{Input: "1GB", Expected: 1000000000},
{Input: "1TB", Expected: 1000000000000},
{Input: "1PB", Expected: 1000000000000000},
{Input: "1EB", Expected: 1000000000000000000},
{Input: "1MiB", Expected: 1048576},
{Input: "1GiB", Expected: 1073741824},
{Input: "1TiB", Expected: 1099511627776},
{Input: "1PiB", Expected: 1125899906842624},
{Input: "1EiB", Expected: 1152921504606846976},
}

for _, tc := range testCases {
actual, err := bytesize.Parse(tc.Input)
require.NoError(t, err)
require.Equal(t, int(tc.Expected), int(actual), fmt.Sprintf("case %s", tc.Input))
}
}
4 changes: 2 additions & 2 deletions pkg/rhttp/datatx/manager/tus/tus.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/rhttp/datatx"
"github.com/cs3org/reva/v2/pkg/rhttp/datatx/manager/registry"
"github.com/cs3org/reva/v2/pkg/rhttp/datatx/utils/download"
"github.com/cs3org/reva/v2/pkg/storage"
"github.com/cs3org/reva/v2/pkg/storage/cache"
"github.com/cs3org/reva/v2/pkg/utils"
Expand Down Expand Up @@ -156,7 +155,8 @@ func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
case "DELETE":
handler.DelFile(w, r)
case "GET":
download.GetOrHeadFile(w, r, fs, "")
handler.GetFile(w, r)
//download.GetOrHeadFile(w, r, fs, "")
default:
w.WriteHeader(http.StatusNotImplemented)
}
Expand Down

0 comments on commit 10d39db

Please sign in to comment.