public string SaveFiles(byte[] contents, string filename, out int fileId)
{
fileId = 0;
var barcodeId = -1;
var row = 'A';
var col = -1;
ParseFileName(filename, out barcodeId, out row, out col);
// invalid filename
if (barcodeId == -1)
{
return FileUploadErrorKeys.InvalidFileName;
}
// load up the barcode, from db
var barcode = _repositoryFactory.BarcodeRepository.GetNullableById(barcodeId);
if (barcode == null)
{
return FileUploadErrorKeys.InvalidBarcode;
}
// check db for existing file
var existingFile = _repositoryFactory.BarcodeFileRepository.Queryable.FirstOrDefault(a => a.Barcode == barcode && a.WellColumn == col && a.WellRow == row);
// check the file system
if (File.Exists(string.Format(@"{0}\raw\{1}\{2}", _storageLocation, barcodeId, filename)) || existingFile != null)
{
return FileUploadErrorKeys.DuplicateFile;
}
if (WriteFile(filename, contents, string.Format(@"{0}\raw\{1}", _storageLocation, barcodeId)))
{
var bfile = new BarcodeFile()
{
Barcode = barcode,
WellColumn = col,
WellRow = row,
Uploaded = true
};
_repositoryFactory.BarcodeFileRepository.EnsurePersistent(bfile);
fileId = bfile.Id;
return string.Empty;
}
return FileUploadErrorKeys.WriteError;
}