Esempio n. 1
0
        /// <summary>
        /// Checksi if blobs exists in the storage
        /// </summary>
        public static async Task <bool> ExistsAsync(this IBlobStorage blobStorage,
                                                    string id, CancellationToken cancellationToken = default)
        {
            IEnumerable <bool> r = await blobStorage.ExistsAsync(new[] { id }, cancellationToken);

            return(r.First());
        }
Esempio n. 2
0
        public async Task <bool> DeleteFileAsync(MethodOptions methodOptions, StorageOptions storageOptions, string filePath)
        {
            EndpointOptions endpoint = FindWriteEndpoint(methodOptions, storageOptions);

            if (!endpoint.Provider.IsFullPath)
            {
                filePath = Path.Combine(endpoint.Path ?? "", filePath);
                string fileName = Path.GetFileName(filePath);
                Log.Information("Command: Remove file {file}", filePath);
                IBlobStorage storage   = _storageProvider.GetStorage(endpoint.Provider, filePath.Replace(fileName, ""));
                bool         fileExist = await storage.ExistsAsync(fileName);

                if (!fileExist)
                {
                    return(false);
                }
                await storage.DeleteAsync(fileName);
            }
            else
            {
                IBlobStorage storage = _storageProvider.GetStorage(endpoint.Provider);
                Log.Information("Command: Remove file {file}", filePath);
                bool fileExist = await storage.ExistsAsync(filePath);

                if (!fileExist)
                {
                    return(false);
                }
                await storage.DeleteAsync(filePath);
            }

            return(true);
        }
Esempio n. 3
0
        public async Task <Stream> DownloadFileAsync(MethodOptions methodOptions, StorageOptions storageOptions, string filePath)
        {
            EndpointOptions endpoint = FindReadEndpoint(methodOptions, storageOptions);

            if (!endpoint.Provider.IsFullPath)
            {
                filePath = Path.Combine(endpoint.Path ?? "", filePath);
                Log.Information("Query: Download file {file}", filePath);
                string       fileName  = Path.GetFileName(filePath);
                IBlobStorage storage   = _storageProvider.GetStorage(endpoint.Provider, filePath.Replace(fileName, ""));
                bool         fileExist = await storage.ExistsAsync(fileName);

                if (!fileExist)
                {
                    return(null);
                }
                return(await storage.OpenReadAsync(fileName));
            }
            else
            {
                Log.Information("Query: Download file {file}", filePath);
                IBlobStorage storage   = _storageProvider.GetStorage(endpoint.Provider);
                bool         fileExist = await storage.ExistsAsync(filePath);

                if (!fileExist)
                {
                    return(null);
                }
                return(await storage.OpenReadAsync(filePath));
            }
        }
        /// <summary>
        /// Checksi if blobs exists in the storage
        /// </summary>
        public static async Task <bool> ExistsAsync(this IBlobStorage blobStorage,
                                                    string fullPath, CancellationToken cancellationToken = default)
        {
            IEnumerable <bool> r = await blobStorage.ExistsAsync(new[] { fullPath }, cancellationToken).ConfigureAwait(false);

            return(r.First());
        }
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] XmlToJsonInputModel model, ILogger log)
        {
            if (string.IsNullOrEmpty(model.File))
            {
                return(new BadRequestObjectResult(new { Message = "Missing file name" }));
            }

            var pathToRawFile = $"/{_container}/{model.File}";

            var exists = await _blobStorage.ExistsAsync(pathToRawFile);

            if (!exists)
            {
                return(new NotFoundObjectResult(new { Message = "File not found." }));
            }

            string xml;

            using (var rawFile = await _blobStorage.OpenReadAsync(pathToRawFile))
                using (var rawReader = new StreamReader(rawFile))
                {
                    xml = await rawReader.ReadToEndAsync();
                }

            var start = xml.IndexOf("<?");

            if (start < 0)
            {
                return(new BadRequestObjectResult(new { Message = "Unable to find start." }));
            }

            xml = xml.Substring(start);

            var end = xml.IndexOf("</MEDELANDE>");

            if (end < 0)
            {
                return(new BadRequestObjectResult(new { Message = "Unable to find end." }));
            }

            xml = xml.Substring(0, end);

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            string json = JsonConvert.SerializeXmlNode(doc);

            return(new OkObjectResult(JObject.Parse(json)));
        }
Esempio n. 6
0
        /// <inheritdoc/>
        public async Task <IImageResolver> GetAsync(HttpContext context)
        {
            var path = context.Request.Path.Value;

            if (!await _blobStorage.ExistsAsync(path))
            {
                return(await Task.FromResult <IImageResolver>(null));
            }

            var blob = await _blobStorage.GetBlobAsync(path);


            var metadata = new ImageMetadata(blob.LastModificationTime.Value.UtcDateTime, blob.Size.Value);

            return(await Task.FromResult <IImageResolver>(new StorageNetImageResolver(_blobStorage, blob, metadata)));
        }
Esempio n. 7
0
        /// <summary>
        /// Checks if the current user is the blob owner and the file exists
        /// </summary>
        /// <param name="userBlob"></param>
        /// <returns></returns>
        public static async Task <IApiResponse <bool> > IsBlobOwnerAndFileExistsAsync(this BaseDbContext <AuditTrails, ExceptionLogs, UserBlobs> dbContext, UserBlobs userBlobs, IBlobStorage blobStorage, string userId, bool ignoreBlobOwner = false, string controllerName = null, string actionName = null, string remoteIpAddress = null)
        {
            if (userBlobs == null)
            {
                await dbContext.CreateAuditrailsAsync(userBlobs, "Userblob is null on method IsBlobOwnerAndFileExistsAsync", userId, controllerName, actionName, remoteIpAddress);

                return(new ApiResponse <bool>()
                {
                    Status = ApiResponseStatus.Failed, Message = "FileNotFound"
                });
            }
            if (!ignoreBlobOwner)
            {
                var isBlobOwner = await dbContext.IsBlobOwnerAsync(userBlobs, userId, controllerName, actionName, remoteIpAddress);

                if (isBlobOwner.Status == ApiResponseStatus.Failed || !isBlobOwner.Data)
                {
                    return(new ApiResponse <bool>()
                    {
                        Status = ApiResponseStatus.Failed, Message = isBlobOwner.Message
                    });
                }
            }
            var fileExist = await blobStorage.ExistsAsync(userBlobs.FilePath);

            if (!fileExist)
            {
                await dbContext.CreateAuditrailsAsync(userBlobs, "File Not found on method IsBlobOwnerAndFileExists", userId, controllerName, actionName, remoteIpAddress);

                return(new ApiResponse <bool>()
                {
                    Status = ApiResponseStatus.Failed, Data = fileExist, Message = "FileNotFound"
                });
            }
            await dbContext.CreateAuditrailsAsync(userBlobs, "File was found", userId, controllerName, actionName, remoteIpAddress);

            return(new ApiResponse <bool>()
            {
                Status = ApiResponseStatus.Succeeded, Data = true
            });
        }
Esempio n. 8
0
        public override async ValueTask <object?> LoadAsync(WorkflowStorageContext context, string key, CancellationToken cancellationToken = default)
        {
            var path = GetFullPath(context, key);

            if (!await _blobStorage.ExistsAsync(path, cancellationToken))
            {
                return(null);
            }

            var blob = await _blobStorage.GetBlobAsync(path, cancellationToken);

            var contentType = blob.Metadata.GetItem("ContentType") ?? "Json";

            if (contentType == "Json")
            {
                var json = await _blobStorage.ReadTextAsync(path, cancellationToken : cancellationToken);

                return(JsonConvert.DeserializeObject(json, _serializerSettings));
            }

            return(await _blobStorage.ReadBytesAsync(path, cancellationToken));
        }
Esempio n. 9
0
 public async Task Exists_non_existing_blob_returns_false()
 {
     Assert.False(await _storage.ExistsAsync(RandomBlobId()));
 }
 public Task <bool> ExistsAsync(string path, CancellationToken cancellationToken = default)
 {
     return(_storage.ExistsAsync(_containerName + "/" + path, cancellationToken));
 }
Esempio n. 11
0
 public Task <IReadOnlyCollection <bool> > ExistsAsync(IEnumerable <string> fullPaths, CancellationToken cancellationToken = default)
 {
     return(_parent.ExistsAsync(fullPaths, cancellationToken));
 }
Esempio n. 12
0
        /// <summary>
        /// deletes file
        /// </summary>
        /// <param name="filepath"></param>
        public void Delete(string filepath)
        {
            if (string.IsNullOrWhiteSpace(filepath))
            {
                throw new ArgumentException("filepath cannot be null or empty");
            }

            //all filepaths are lowercase and all starts with folder separator
            filepath = filepath.ToLowerInvariant();
            if (!filepath.StartsWith(FOLDER_SEPARATOR))
            {
                filepath = FOLDER_SEPARATOR + filepath;
            }

            var file = Find(filepath);

            if (file == null)
            {
                return;
            }

            using (var connection = CurrentContext.CreateConnection())
            {
                try
                {
                    connection.BeginTransaction();
                    if (ErpSettings.EnableCloudBlobStorage && file.ObjectId == 0)
                    {
                        var path = GetBlobPath(file);
                        using (IBlobStorage storage = GetBlobStorage())
                        {
                            if (storage.ExistsAsync(path).Result)
                            {
                                storage.DeleteAsync(path).Wait();
                            }
                        }
                    }
                    else if (ErpSettings.EnableFileSystemStorage && file.ObjectId == 0)
                    {
                        var path = GetFileSystemPath(file);
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                    else
                    {
                        if (file.ObjectId != 0)
                        {
                            new NpgsqlLargeObjectManager(connection.connection).Unlink(file.ObjectId);
                        }
                    }

                    var command = connection.CreateCommand(@"DELETE FROM files WHERE id = @id");
                    command.Parameters.Add(new NpgsqlParameter("@id", file.Id));
                    command.ExecuteNonQuery();

                    connection.CommitTransaction();
                }
                catch
                {
                    connection.RollbackTransaction();
                    throw;
                }
            }
        }
Esempio n. 13
0
 public virtual Task <bool> ExistsAsync(string path, CancellationToken cancellationToken = default)
 {
     return(_blobStorage.ExistsAsync(path, cancellationToken));
 }
 public Task <IEnumerable <bool> > ExistsAsync(IEnumerable <string> ids, CancellationToken cancellationToken = default)
 {
     return(_parentStorage.ExistsAsync(ids, cancellationToken));
 }