Ana içeriğe geç
Version: 1.0.1

File Operations

Provides operations such as saving files, fetching, validating and using json files as database in the file system.

In addition to saving, fetching, validating, deleting files in the file system, it also performs conversion operations compatible with IFormFile type of files.

Usage


...

private static List<string> _allowedFileExtensions = new List<string> { ".jpg" };
private static int _maxFileLength = 14000000;//in bytes
private static string _imagesFolderBasePath = "somePath";
private readonly IProductService _productService;

...

[HttpPost("Product/Image")]
public async Task<IActionResult> UpdateProductImageAsync(IFormFile formFile)
{
// Validates file
var validationResult = formFile.ValidateFile(_maxFileLength , _allowedFileExtensions, FileType.Image);

if(validationResult != FileValidationResult.Valid)
throw new MilvaUserFriendlyException("InvalidFileMessage");

// Get product from database
var product = await _productService.GetProductAsync();

// The SaveFileToPathAsync() method creates a folder if it doesn't exist. That's why we provide name generator delegate for this process. In this example, method will create folder named "ProductImages".
FormFileOperations.FilesFolderNameCreator imagesFolderNameCreator = (Type type) => type.Name + "Images";

// Saves the sent file to the server and returns the file path, making the filename the value of the specified property of the object specified in the parameters.
var path = await formFile.SaveFileToPathAsync(product, _imagesFolderBasePath, imagesFolderNameCreator, propertyName : "Id");

// Creates file url for serve files via url
var url = FormFileOperations.GetFileUrlPathSectionFromFilePath(path, "imageLib");

return url.GetObjectResponse("SuccessfullyUpdatedMessage");
}

You can upload multiple file at once. For more check here.

Json File Operations

Provides you to perform CRUD operations using json files as a database. In addition, while performing these operations, it allows to operate on encrypted files by using the AES encryption algorithm.

Usage

Add required services to service collection.


services.AddJsonOperations(options: opt =>
{
// Your key must be 128 bit.
opt.EncryptionKey = "2r5u8x/A?D(G-KaP";
opt.BasePath = "json file base path";
});

...

private readonly IJsonOperations _jsonOperations;
private const string _filePath = "test.json";

public ProductService(IJsonOperations jsonOperations) => _jsonOperations = jsonOperations;

...

public async Task DoSomethingAsync()
{
var product = new Product
{
Id = 0,
Name = "Coffee"
};

// `contentsHasId` parameter determines whether the contents are added in auto increment by "Id" property.
await _jsonOperations.AddContentAsync(product, _filePath, contentsHasId : true);

// Output will be as added product. Since the `contentsHasId` parameter is true when adding, the added product Id property will be 1.
var jsonContent = await _jsonOperations.GetContentAsync<List<Product>>(_filePath);

jsonContent.Name = "Lemonade";

// The `mappingProperty` must be a unique property that must be in the data for the record to be found in the file.
await _jsonOperations.UpdateContentAsync(jsonContent, _filePath, mappingProperty : p => p.Id);

await _jsonOperations.ClearJSONFileAsync(_filePath);
}

You can use IJsonOperations for store connection strings or secret keys encrypted. This methods supports 3 json file structure;

"samplesecretkey"
[
{
"Id": 1,
"Name": "poco1",
"Which": false
},
{
"Id": 2,
"Name": "poco2",
"Which": true
},
{
"Id": 3,
"Name": "poco3",
"Which": true
}
]


{
"Id": 1,
"Name": "poco1",
"Which": false
},
{
"Id": 2,
"Name": "poco2",
"Which": true
},
{
"Id": 3,
"Name": "poco3",
"Which": true
}

For more check here.