示例#1
0
        //checks if a file already exists, uploads it if not
        private static void CheckOrUploadFile(string localFolder, string cmsDestinationPath, AccessAsset accessAsset, string file)
        {
            string cmsPathToCheck = "";
              //1 - check if file already exists
              cmsPathToCheck = file.ToLower().Replace(localFolder, cmsDestinationPath).Replace("\\", "/");
              var existsResponse = accessAsset.Exists(new AssetExistsRequest(cmsPathToCheck));
              if (existsResponse.exists)
              {
            Console.WriteLine("Skipping, asset {0} already exists.  (id: {1})", cmsPathToCheck, existsResponse.assetId);
              }
              else
              {
            //file was not found in the CMS instance, so we will upload it, but first make sure we do have an existing folder structure in the CMS
            //2 - check if folder exists, create otherwise
            string path = Path.GetDirectoryName(file);
            int folderId = -1;
            if (folderCache.ContainsKey(path))
            {
              folderId = folderCache[path];
            }
            else
            {
              //attempt to create folder and cache new assetId as the folderId
              cmsPathToCheck = path.ToLower().Replace(localFolder, cmsDestinationPath).Replace("\\", "/");
              existsResponse = accessAsset.Exists(new AssetExistsRequest(cmsPathToCheck));
              if (existsResponse.exists)
              {
            folderId = existsResponse.assetId;
            folderCache.Add(path, folderId);
              }
              else
              {
            //could not find folder, split and build path
            folderId = BuildFolderTree(cmsPathToCheck);
            if (folderId < 0)
            {
              //something went wrong, exit app, so we don't upload files to wrong folders, or have excessive # of errors.
              Console.WriteLine("Error, Unable to create folder {0} ", cmsPathToCheck);
              Environment.Exit(-1);
            }
            folderCache.Add(path, folderId);
              }
            }

            //upload asset here
            AssetUploadRequest req = new AssetUploadRequest(Path.GetFileName(file), folderId);
            req.bytes = File.ReadAllBytes(file);
            var resp = accessAsset.Upload(req);
            if (resp.IsSuccessful)
            {
              Console.WriteLine("upload successful: {0} in {1}", file, path, resp.asset.id);
            }
            else
            {
              Console.WriteLine("Error uploading file: {0} in {1}: {2}", file, path, resp.ErrorMessage);
            }
              }
        }
示例#2
0
        public string UploadAsset(string assetPath, string base64StringData)
        {
            EnsureAccessApiSession();

            var accessAsset = new AccessAsset(_session.Client);
            var uploadPath = _settings.ImageBrowsePath + assetPath;

            // Remove existing asset if exists
            if (!DeleteAsset(accessAsset, uploadPath))
                throw new InvalidOperationException("Unable to remove existing asset: " + uploadPath);

            // Get target folder
            var folderId = GetAssetId(accessAsset, Path.GetDirectoryName(uploadPath).Replace("\\", "//"));

            if (folderId <= 0)
                throw new InvalidOperationException("Specified upload path does not exist: " + uploadPath);

            // Upload new asset to target folder
            var fileName = Path.GetFileName(uploadPath);
            var assetDataParts = base64StringData.Split(",".ToCharArray());

            AssetUploadRequest request = new AssetUploadRequest(fileName, folderId);

            if (!string.IsNullOrEmpty(_settings.ImageUploadWorkflowId))
                request.workflowId = int.Parse(_settings.ImageUploadWorkflowId);

            request.bytes = Convert.FromBase64String(assetDataParts.Length > 1 ? assetDataParts[1] : assetDataParts[0]);

            var response = accessAsset.Upload(request);

            if (!response.IsSuccessful)
                throw new InvalidOperationException("Unable to upload asset: " + uploadPath + " (" + response.ErrorMessage ?? string.Empty + ")");

            // Return published relative path of asset
            var publishedPath = _settings.ImageBrowsePublishServerPath + assetPath;

            return publishedPath;
        }