<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Utilities;
using File = Microsoft.SharePoint.Client.File;

namespace OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers
{
    internal class ObjectFiles : ObjectHandlerBase
    {
        public override string Name
        {
            get { return "Files"; }
        }
        public override void ProvisionObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_Files);

            var context = web.Context as ClientContext;

            if (!web.IsPropertyAvailable("ServerRelativeUrl"))
            {
                context.Load(web, w => w.ServerRelativeUrl);
                context.ExecuteQueryRetry();
            }

            foreach (var file in template.Files)
            {

                var folderName = file.Folder.ToParsedString();

                if (folderName.ToLower().StartsWith((web.ServerRelativeUrl.ToLower())))
                {
                    folderName = folderName.Substring(web.ServerRelativeUrl.Length);
                }


                var folder = web.EnsureFolderPath(folderName);

                File targetFile = null;

                var checkedOut = false;

                targetFile = folder.GetFile(file.Src);

                if (targetFile != null)
                {
                    if (file.Overwrite)
                    {
                        checkedOut = CheckOutIfNeeded(web, targetFile);

                        using (var stream = template.Connector.GetFileStream(file.Src))
                        {
                            targetFile = folder.UploadFile(template.Connector.GetFilenamePart(file.Src), stream, file.Overwrite);
                        }
                    }
                    else
                    {
                        checkedOut = CheckOutIfNeeded(web, targetFile);
                    }
                }
                else
                {
                    using (var stream = template.Connector.GetFileStream(file.Src))
                    {
                        targetFile = folder.UploadFile(template.Connector.GetFilenamePart(file.Src), stream, file.Overwrite);
                    }

                    checkedOut = CheckOutIfNeeded(web, targetFile);
                }
<commit_msg>Fixed a bug where files that require checkout are not found if you're using a path in the file.Src name. Because they are not found it will result in a 'File is not checked out' error.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Utilities;
using File = Microsoft.SharePoint.Client.File;

namespace OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers
{
    internal class ObjectFiles : ObjectHandlerBase
    {
        public override string Name
        {
            get { return "Files"; }
        }
        public override void ProvisionObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_Files);

            var context = web.Context as ClientContext;

            if (!web.IsPropertyAvailable("ServerRelativeUrl"))
            {
                context.Load(web, w => w.ServerRelativeUrl);
                context.ExecuteQueryRetry();
            }

            foreach (var file in template.Files)
            {

                var folderName = file.Folder.ToParsedString();

                if (folderName.ToLower().StartsWith((web.ServerRelativeUrl.ToLower())))
                {
                    folderName = folderName.Substring(web.ServerRelativeUrl.Length);
                }


                var folder = web.EnsureFolderPath(folderName);

                File targetFile = null;

                var checkedOut = false;

                targetFile = folder.GetFile(template.Connector.GetFilenamePart(file.Src));

                if (targetFile != null)
                {
                    if (file.Overwrite)
                    {
                        checkedOut = CheckOutIfNeeded(web, targetFile);

                        using (var stream = template.Connector.GetFileStream(file.Src))
                        {
                            targetFile = folder.UploadFile(template.Connector.GetFilenamePart(file.Src), stream, file.Overwrite);
                        }
                    }
                    else
                    {
                        checkedOut = CheckOutIfNeeded(web, targetFile);
                    }
                }
                else
                {
                    using (var stream = template.Connector.GetFileStream(file.Src))
                    {
                        targetFile = folder.UploadFile(template.Connector.GetFilenamePart(file.Src), stream, file.Overwrite);
                    }

                    checkedOut = CheckOutIfNeeded(web, targetFile);
                }

{'fraction_non_alphanumeric': 0.07710155273960378, 'fraction_numerical': 0.0, 'mean_word_length': 1.6877697841726618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4335165', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1310, 'n_words': 257}