override fun publishFiles()

in azure-storage-agent/src/main/kotlin/jetbrains/buildServer/artifacts/azure/publish/AzureArtifactsPublisher.kt [43:106]


    override fun publishFiles(filePathMap: Map<File, String>): Int {
        val filesToPublish = filePathMap.entries.filter {
            !it.value.startsWith(ArtifactsConstants.TEAMCITY_ARTIFACTS_DIR)
        }

        if (filesToPublish.isNotEmpty()) {
            val build = tracker.currentBuild
            try {
                val parameters = publisherParameters

                if (publishedArtifacts.isEmpty()) {
                    setPathPrefixProperty(build)
                }

                val client = AzureUtils.getBlobClient(parameters)
                val pathValue = getPathPrefixProperty(build)
                val (containerName, pathPrefix) = AzureUtils.getContainerAndPath(pathValue)
                    ?: throw ArtifactPublishingFailedException(
                        "Invalid $PATH_PREFIX_SYSTEM_PROPERTY build system property",
                        false,
                        null
                    )

                val container = client.getContainerReference(containerName)
                if (publishedArtifacts.isEmpty()) {
                    container.createIfNotExists()
                }

                filesToPublish.forEach { (file, path) ->
                    val filePath = AzureUtils.appendPathPrefix(path, file.name)
                    val blobName = AzureUtils.appendPathPrefix(pathPrefix, filePath)
                    val blob = container.getBlockBlobReference(blobName)
                    blob.properties.contentType = AzureUtils.getContentType(file)
                    blob.streamWriteSizeInBytes = AzureUtils.getWriteBufferSize()

                    FileInputStream(file).use {
                        val length = file.length()
                        blob.upload(
                            it,
                            length,
                            null,
                            BlobRequestOptions().apply {
                                this.concurrentRequestCount = AzureUtils.getWriteConcurrentRequestCount()
                                this.timeoutIntervalInMs = AzureUtils.getTimeoutIntervalInMs()
                            },
                            null
                        )
                        val artifact = ArtifactDataInstance.create(filePath, length)
                        publishedArtifacts.add(artifact)
                    }
                }
            } catch (e: Throwable) {
                val message = AzureUtils.getExceptionMessage(e)

                LOG.warnAndDebugDetails(message, e)
                build.buildLogger.error(message)

                throw ArtifactPublishingFailedException(message, false, e)
            }
            publishArtifactsList(build)
        }

        return filesToPublish.size
    }