static void TestIsFileExtensionSupported(string fileEx)
        {
            ClientResult <bool> bTemp;

            bTemp = TranslationJob.IsFileExtensionSupported(cc, fileEx);
            cc.ExecuteQuery();
            Console.WriteLine("IsFileExtensionSupported for " + fileEx + " " + bTemp.Value);
        }
 private void CreateThreadForTestFileExt(object fileExt)
 {
     try
     {
         ClientResult <bool> bTemp;
         bTemp = TranslationJob.IsFileExtensionSupported(cc, (string)fileExt);
         cc.ExecuteQuery();
         string result           = bTemp.Value.ToString();
         DispatcherOperation dop = Dispatcher.BeginInvoke(new Action <string>(PrintResult), result);
     }
     catch (Exception e)
     {
         DispatcherOperation dop = Dispatcher.BeginInvoke(new Action <string>(PrintResult), e.ToString());
     }
 }
예제 #3
0
        public ActionResult File()
        {
            try
            {
                Uri hostWebUri = new Uri(Request.QueryString["SPHostUrl"]);
                Uri appWebUri  = new Uri(Request.QueryString["SPAppWebUrl"]);

                string listId     = Request.QueryString["SPListId"];
                string listItemId = Request.QueryString["SPListItemId"];

                string culture     = string.Empty;
                string destination = string.Empty;

                //Get the settings
                List <Setting> settings = new List <Setting>();

                using (ClientContext ctx = TokenHelper.GetS2SClientContextWithWindowsIdentity(appWebUri, Request.LogonUserIdentity))
                {
                    List settingsList = ctx.Web.Lists.GetByTitle("Settings");
                    ctx.Load(settingsList);

                    ListItemCollection settingItems = settingsList.GetItems(CamlQuery.CreateAllItemsQuery());
                    ctx.Load(settingItems);

                    ctx.ExecuteQuery();

                    foreach (ListItem settingItem in settingItems)
                    {
                        settings.Add(new Setting()
                        {
                            Title = settingItem["Title"].ToString(),
                            Value = settingItem["Value"].ToString(),
                        });
                    }
                }

                culture     = settings.Where <Setting>(s => s.Title == "Culture").FirstOrDefault().Value;
                destination = settings.Where <Setting>(s => s.Title == "Destination").FirstOrDefault().Value;

                //Translate file synchronously
                using (ClientContext ctx = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWebUri, Request.LogonUserIdentity))
                {
                    //Get the file to translate
                    ListItem listItem = ctx.Web.Lists.GetById(new Guid(listId)).GetItemById(listItemId);
                    ctx.Load(listItem, i => i.File);
                    ctx.ExecuteQuery();

                    //Get the destination library
                    Folder destinationFolder = ctx.Web.Lists.GetByTitle(destination).RootFolder;
                    ctx.Load(destinationFolder, f => f.ServerRelativeUrl);
                    ctx.ExecuteQuery();

                    string ext        = listItem.File.Name.Substring(listItem.File.Name.LastIndexOf("."));
                    string inPath     = hostWebUri.Scheme + "://" + hostWebUri.Authority + ":" + hostWebUri.Port + listItem.File.ServerRelativeUrl;
                    string outPath    = hostWebUri.Scheme + "://" + hostWebUri.Authority + ":" + hostWebUri.Port + destinationFolder.ServerRelativeUrl + "/" + listItem.File.Name;
                    string returnPath = hostWebUri.Scheme + "://" + hostWebUri.Authority + ":" + hostWebUri.Port + destinationFolder.ServerRelativeUrl;
                    ViewBag.ReturnPath = returnPath;

                    //Check if extension is supported
                    ClientResult <bool> isSupported = TranslationJob.IsFileExtensionSupported(ctx, ext.Substring(1));
                    ctx.ExecuteQuery();
                    if (!isSupported.Value)
                    {
                        throw new Exception("File extension is not supported.");
                    }

                    //Translate
                    SyncTranslator job = new SyncTranslator(ctx, culture);
                    job.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
                    ClientResult <TranslationItemInfo> cr = job.Translate(inPath, outPath);
                    ctx.ExecuteQuery();

                    if (!cr.Value.Succeeded)
                    {
                        throw new Exception(cr.Value.ErrorMessage);
                    }

                    //Return to library
                    Response.Redirect(returnPath);
                }
            }
            catch (Exception x)
            {
                ViewBag.Message = x.Message;
            }

            return(View());
        }