Beispiel #1
0
        private void RecursiveDeleteEmptyDirectories(IDirectory dir, bool importfolder)
        {
            FileSystemResult fr = dir.Populate();

            if (fr.IsOk)
            {
                if (dir.Files.Count > 0)
                {
                    return;
                }
                foreach (IDirectory d in dir.Directories)
                {
                    RecursiveDeleteEmptyDirectories(d, false);
                }
            }
            if (importfolder)
            {
                return;
            }
            fr = dir.Populate();
            if (fr.IsOk)
            {
                if (dir.Files.Count == 0 && dir.Directories.Count == 0)
                {
                    fr = dir.Delete(true);
                    if (!fr.IsOk)
                    {
                        logger.Warn("Unable to DELETE directory: {0} error {1}", dir.FullName, fr?.Error ?? String.Empty);
                    }
                }
            }
        }
Beispiel #2
0
        public static void GetFilesForImportFolder(IDirectory sDir, ref List <string> fileList)
        {
            try
            {
                if (sDir == null)
                {
                    logger.Error("Filesystem not found");
                    return;
                }
                // get root level files

                FileSystemResult r = sDir.Populate();
                if (r.Status != Status.Ok)
                {
                    logger.Error($"Unable to retrieve folder {sDir.FullName}");
                    return;
                }

                fileList.AddRange(sDir.Files.Select(a => a.FullName));

                // search sub folders
                foreach (IDirectory dir in sDir.Directories)
                {
                    GetFilesForImportFolder(dir, ref fileList);
                }
            }
            catch (Exception excpt)
            {
                logger.Error(excpt.Message);
            }
        }
Beispiel #3
0
        public static IFile ResolveFile(string fullname)
        {
            if (string.IsNullOrEmpty(fullname))
            {
                return(null);
            }
            Tuple <SVR_ImportFolder, string> tup = VideoLocal_PlaceRepository.GetFromFullPath(fullname);
            IFileSystem fs = tup?.Item1?.FileSystem;

            if (fs == null)
            {
                return(null);
            }
            try
            {
                FileSystemResult <IObject> fobj = fs.Resolve(fullname);
                if (fobj == null || !fobj.IsOk || fobj.Result is IDirectory)
                {
                    return(null);
                }
                return(fobj.Result as IFile);
            }
            catch (Exception)
            {
                logger.Warn("File with Exception: " + fullname);
                return(null);
            }
        }
        public async Task <FileSystemResult> MoveAsync(IDirectory destination)
        {
            if (Parent == null)
            {
                return(new FileSystemResult("Unable to move root directory"));
            }
            if (!(destination is GoogleDriveDirectory))
            {
                return(new FileSystemResult("Destination should be a Google Drive Directory"));
            }
            GoogleDriveDirectory dest = (GoogleDriveDirectory)destination;

            if (dest.Id == ((GoogleDriveDirectory)Parent).Id)
            {
                return(new FileSystemResult("Source Directory and Destination Directory should be different"));
            }
            string addParents    = dest.Id;
            string removeParents = ((GoogleDriveDirectory)Parent).Id;

            string url = GooglePatch.FormatRest(Id);

            url += "?addParents=" + addParents + "&removeParents=" + removeParents;
            FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, null, null, new HttpMethod("PATCH"));

            if (ex.IsOk)
            {
                string oldFullname = this.FullName;
                this.SetData(ex.Result);
                ChangeObjectDirectory <GoogleDriveDirectory, GoogleDriveFile>(oldFullname, FS.Refs, this, (GoogleDriveDirectory)Parent, dest);
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
Beispiel #5
0
        public async Task <FileSystemResult <IDirectory> > CreateDirectoryAsync(string name, Dictionary <string, object> properties)
        {
            if (properties == null)
            {
                properties = new Dictionary <string, object>();
            }
            CreateDirectoryRequest req = new CreateDirectoryRequest();

            req.Name   = name;
            req.Folder = new Folder();
            string requesturl = CreateDir.FormatRest(this is OneDriveRoot ? "root" : Id);
            FileSystemResult <ExpandoObject> ex = await FS.OAuth.CreateMetadataStream <ExpandoObject>(requesturl, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req)), "application/json");

            if (ex.IsOk)
            {
                string id;
                if (InternalTryGetProperty(ex.Result, "id", out id))
                {
                    FileSystemResult <ExpandoObject> ex2 =
                        await FS.OAuth.CreateMetadataStream <ExpandoObject>(Item.FormatRest(FS.OneDriveUrl, id));

                    OneDriveDirectory dir = new OneDriveDirectory(FullName, FS)
                    {
                        Parent = this
                    };
                    dir.SetData(JsonConvert.SerializeObject(ex.Result));
                    FS.Refs[dir.FullName] = dir;
                    _directories.Add(dir);
                    return(new FileSystemResult <IDirectory>(dir));
                }
                return(new FileSystemResult <IDirectory>("Unable to get id from the created directory"));
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
Beispiel #6
0
        public IFileSystem Connect(Window owner)
        {
            if (string.IsNullOrEmpty(Provider))
            {
                throw new Exception("Empty provider supplied");
            }

            Dictionary <string, object> auth = AuthInstance.AuthorizationProvider.Get(Provider);

            if (auth == null)
            {
                throw new Exception("Application Authorization Not Found");
            }
            _plugin = CloudFileSystemPluginFactory.Instance.List.FirstOrDefault(a => a.Name == Provider);
            if (_plugin == null)
            {
                throw new Exception("Cannot find cloud provider '" + Provider + "'");
            }
            Icon = _plugin.CreateIconImage();
            FileSystemResult <IFileSystem> res = _plugin.Init(Name, new AuthProvider(owner), auth, ConnectionString);

            if (!res.IsOk)
            {
                throw new Exception("Unable to connect to '" + Provider + "'");
            }
            string userauth = res.Result.GetUserAuthorization();

            if (ConnectionString != userauth)
            {
                NeedSave         = true;
                ConnectionString = userauth;
            }
            return(res.Result);
        }
Beispiel #7
0
        public List <TextStream> GetStreams(SVR_VideoLocal_Place vplace)
        {
            // TODO Scan the folder for filename.lang.sub files
            string dirname = Path.GetDirectoryName(vplace.FullServerPath);
            string fname   = Path.GetFileNameWithoutExtension(vplace.FilePath);

            if (string.IsNullOrEmpty(dirname) || string.IsNullOrEmpty(fname))
            {
                return(new List <TextStream>());
            }
            string basename = Path.Combine(dirname, fname);
            var    path     = basename + ".sub";

            if (!File.Exists(basename + ".idx") || !File.Exists(path))
            {
                return(new List <TextStream>());
            }
            FileSystemResult <IObject> r = vplace.ImportFolder.FileSystem.Resolve(path);

            if (r == null || !r.IsOk || !(r.Result is IFile))
            {
                return(new List <TextStream>());
            }

            MediaContainer m = MediaInfo.GetMediaInfo(path);

            if (m == null)
            {
                return(new List <TextStream>());
            }
            m.TextStreams.ForEach(a => a.Filename = path);
            return(m.TextStreams);
        }
Beispiel #8
0
        internal async Task <FileSystemResult> MayRefreshToken(bool force = false)
        {
            if (Token == null)
            {
                return(new FileSystemResult("Authorization Token not found"));
            }
            if (Token.ExpirationDate.AddMinutes(3) < DateTime.Now || force)
            {
                string refreshToken = Token.RefreshToken;
                Dictionary <string, string> postdata = new Dictionary <string, string>();
                postdata.Add("grant_type", "refresh_token");
                postdata.Add("refresh_token", Token.RefreshToken);
                postdata.Add("client_id", ClientId);
                postdata.Add("client_secret", ClientSecret);
                Token = null;
                FileSystemResult <Token> fs = await CreateMetadataStream <Token>(OAuthUrl, Encoding.UTF8.GetBytes(postdata.PostFromDictionary()));

                if (!fs.IsOk)
                {
                    return(new FileSystemResult(fs.Error));
                }
                Token = fs.Result;
                if (string.IsNullOrEmpty(Token.RefreshToken))
                {
                    Token.RefreshToken = refreshToken;
                }
            }
            return(new FileSystemResult());
        }
Beispiel #9
0
        internal async Task <FileSystemResult <dynamic> > List(string url)
        {
            string         baseurl = url;
            int            count;
            List <dynamic> accum = new List <dynamic>();

            do
            {
                FileSystemResult <ExpandoObject> cl = await FS.OAuth.CreateMetadataStream <ExpandoObject>(url);

                if (!cl.IsOk)
                {
                    return(new FileSystemResult <dynamic>(cl.Error));
                }
                dynamic obj = cl.Result;
                count = obj.data.Count;
                if (count > 0)
                {
                    accum.AddRange(obj.data);
                    if (!((IDictionary <String, object>)obj).ContainsKey("nextToken"))
                    {
                        count = 0;
                    }
                    else
                    {
                        url = baseurl + "&startToken=" + obj.nextToken;
                    }
                }
            } while (count > 0);
            return(new FileSystemResult <dynamic>(accum));
        }
        public override FileSystemResult DehydrateFolder(string relativePath)
        {
            FileSystemResult result = new FileSystemResult(FSResult.Ok, 0);

            GitIndexProjection.PathSparseState sparseState = this.FileSystemCallbacks.GitIndexProjection.GetFolderPathSparseState(relativePath);

            if (sparseState == GitIndexProjection.PathSparseState.Included)
            {
                // When the folder is included we need to create the placeholder to make sure it is on disk for enumeration
                result = this.WritePlaceholderDirectory(relativePath);
                if (result.Result == FSResult.Ok)
                {
                    this.FileSystemCallbacks.OnPlaceholderFolderCreated(relativePath, string.Empty);
                }
                else if (result.Result == FSResult.FileOrPathNotFound)
                {
                    // This will happen when the parent folder is also in the dehydrate list and is no longer on disk.
                    result = new FileSystemResult(FSResult.Ok, 0);
                }
                else
                {
                    EventMetadata metadata = this.CreateEventMetadata(relativePath);
                    metadata.Add(nameof(result.Result), result.Result);
                    metadata.Add(nameof(result.RawResult), result.RawResult);
                    this.Context.Tracer.RelatedError(metadata, $"{nameof(this.DehydrateFolder)}: Write placeholder failed");
                }
            }

            return(result);
        }
Beispiel #11
0
        public List <Stream> Process(SVR_VideoLocal_Place vplace)
        {
            string dirname = Path.GetDirectoryName(vplace.FullServerPath);
            string fname   = Path.GetFileNameWithoutExtension(vplace.FilePath);

            if (string.IsNullOrEmpty(dirname) || string.IsNullOrEmpty(fname))
            {
                return(null);
            }
            string        basename = Path.Combine(dirname, fname);
            List <Stream> streams  = new List <Stream>();

            if (File.Exists(basename + ".idx") && File.Exists(basename + ".sub"))
            {
                FileSystemResult <IObject> r = vplace.ImportFolder.FileSystem.Resolve(basename + ".sub");
                if (r != null && r.IsOk && r.Result is IFile)
                {
                    List <Stream> ss = GetStreams((IFile)r.Result);
                    if ((ss != null) && (ss.Count > 0))
                    {
                        streams.AddRange(ss);
                    }
                }
            }
            return(streams);
        }
        public async Task <FileSystemResult> WriteMetadataAsync(ExpandoObject metadata)
        {
            //Only Name, Description and Labels supported
            Json.MetaPatch patch = new Json.MetaPatch();
            string         v;

            if (InternalTryGetProperty(metadata, "name", out v))
            {
                patch.name = v;
            }
            if (InternalTryGetProperty(metadata, "description", out v))
            {
                patch.description = v;
            }
            List <string> labels;

            if (InternalTryGetProperty(metadata, "labels", out labels))
            {
                patch.labels = labels;
            }
            string url = AmazonPatch.FormatRest(FS.OAuth.EndPoint.MetadataUrl, Id);
            FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(patch)), "application/json", new HttpMethod("PATCH"));

            if (ex.IsOk)
            {
                this.SetData(ex.Result);
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
        public new async Task <FileSystemResult <FileSystemSizes> > QuotaAsync()
        {
            FileSystemResult <ExpandoObject> cl = await FS.OAuth.CreateMetadataStream <ExpandoObject>(GoogleQuota);

            if (!cl.IsOk)
            {
                return(new FileSystemResult <FileSystemSizes>(cl.Error));
            }
            IDictionary <string, object> dic = cl.Result;

            Sizes = new FileSystemSizes();
            if (dic.ContainsKey("quotaBytesTotal"))
            {
                Sizes.TotalSize = ParseLong(dic["quotaBytesTotal"]);
            }
            if (dic.ContainsKey("quotaBytesUsed"))
            {
                Sizes.UsedSize = ParseLong(dic["quotaBytesUsed"]);
            }
            if (dic.ContainsKey("quotaBytesUsedAggregate"))
            {
                Sizes.AvailableSize = Sizes.TotalSize - ParseLong(dic["quotaBytesUsedAggregate"]);
            }
            return(new FileSystemResult <FileSystemSizes>(Sizes));
        }
        public async Task <FileSystemResult> DeleteAsync(bool skipTrash)
        {
            if (Parent == null)
            {
                return(new FileSystemResult("Unable to delete root directory"));
            }
            string url = AmazonTrash.FormatRest(FS.OAuth.EndPoint.MetadataUrl, Id);

            FileSystemResult <ExpandoObject> ex = await FS.OAuth.CreateMetadataStream <ExpandoObject>(url, null, null, HttpMethod.Put);

            if (ex.IsOk)
            {
                if (this is AmazonFile)
                {
                    Parent.Files.Remove((IFile)this);
                }
                else if (this is AmazonDirectory)
                {
                    Parent.Directories.Remove((IDirectory)this);
                }
                this.Parent = null;
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
        public async Task <FileSystemResult> MoveAsync(IDirectory destination)
        {
            if (Parent == null)
            {
                return(new FileSystemResult("Unable to move root directory"));
            }
            if (!(destination is AmazonDirectory))
            {
                return(new FileSystemResult("Destination should be an Amazon Cloud Drive Directory"));
            }
            AmazonDirectory dest = (AmazonDirectory)destination;

            if (dest.Id == ((AmazonDirectory)Parent).Id)
            {
                return(new FileSystemResult("Source Directory and Destination Directory should be different"));
            }
            Json.MoveData j = new Json.MoveData();
            j.childId    = Id;
            j.fromParent = ((AmazonDirectory)Parent).Id;

            string url = AmazonMove.FormatRest(FS.OAuth.EndPoint.MetadataUrl, dest.Id);
            FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(j)), "application/json");

            //TODO Some kind of locking
            if (ex.IsOk)
            {
                string oldFullname = this.FullName;
                this.SetData(ex.Result);
                ChangeObjectDirectory <AmazonDirectory, AmazonFile>(oldFullname, FS.Refs, this, (AmazonDirectory)Parent, dest);
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
Beispiel #16
0
        public static IFile ResolveFile(string fullname)
        {
            Tuple <ImportFolder, string> tup = VideoLocal_PlaceRepository.GetFromFullPath(fullname);
            IFileSystem fs = tup?.Item1.FileSystem;

            if (fs == null)
            {
                return(null);
            }
            try
            {
                FileSystemResult <IObject> fobj = fs?.Resolve(fullname);
                if (!fobj.IsOk || fobj.Result is IDirectory)
                {
                    logger.Warn("File not found: " + fullname);
                    return(null);
                }
                return(fobj.Result as IFile);
            }
            catch (Exception)
            {
                logger.Warn("File with Exception: " + fullname);
                throw;
            }
        }
Beispiel #17
0
        public List <Stream> Process(SVR_VideoLocal_Place vplace)
        {
            string dirname = Path.GetDirectoryName(vplace.FullServerPath);
            string fname   = Path.GetFileNameWithoutExtension(vplace.FilePath);

            if (string.IsNullOrEmpty(dirname) || string.IsNullOrEmpty(fname))
            {
                return(null);
            }
            string           basename   = Path.Combine(dirname, fname);
            HashSet <string> extensions = new HashSet <string>(SubtitleHelper.Extensions.Keys);

            extensions.Remove("idx");
            List <Stream> streams = new List <Stream>();

            foreach (string n in extensions)
            {
                string newname = basename + "." + n;
                FileSystemResult <IObject> r = vplace.ImportFolder.FileSystem.Resolve(newname);
                if (r != null && r.IsOk && r.Result is IFile)
                {
                    List <Stream> ss = GetStreams((IFile)r.Result);
                    if ((ss != null) && (ss.Count > 0))
                    {
                        streams.AddRange(ss);
                    }
                }
            }
            return(streams);
        }
Beispiel #18
0
        public List <TextStream> GetStreams(SVR_VideoLocal_Place vplace)
        {
            // TODO Scan the folder for filename.lang.sub files
            string dirname = Path.GetDirectoryName(vplace.FullServerPath);
            string fname   = Path.GetFileNameWithoutExtension(vplace.FilePath);

            if (string.IsNullOrEmpty(dirname) || string.IsNullOrEmpty(fname))
            {
                return(new List <TextStream>());
            }
            string           basename   = Path.Combine(dirname, fname);
            HashSet <string> extensions = new HashSet <string>(SubtitleHelper.Extensions.Keys);

            extensions.Remove("idx");
            List <TextStream> streams = new List <TextStream>();

            foreach (string n in extensions)
            {
                string newname = $"{basename}.{n}";
                FileSystemResult <IObject> r = vplace.ImportFolder.FileSystem.Resolve(newname);
                if (r == null || !r.IsOk || !(r.Result is IFile))
                {
                    continue;
                }
                MediaContainer    m        = MediaInfo.GetMediaInfo(newname);
                List <TextStream> tStreams = m?.TextStreams;
                if (tStreams != null && tStreams.Count > 0)
                {
                    tStreams.ForEach(a => a.Filename = newname);
                    streams.AddRange(tStreams);
                }
            }
            return(streams);
        }
Beispiel #19
0
        public IFileSystem Connect()
        {
            if (string.IsNullOrEmpty(Provider))
            {
                throw new Exception("Empty provider supplied");
            }

            Dictionary <string, object> auth = AuthInstance.AuthorizationProvider.Get(Provider);

            if (auth == null)
            {
                throw new Exception("Application Authorization Not Found");
            }
            _plugin = CloudFileSystemPluginFactory.Instance.List.FirstOrDefault(a => a.Name.EqualsInvariantIgnoreCase(Provider));
            if (_plugin == null)
            {
                throw new Exception("Cannot find cloud provider '" + Provider + "'");
            }
            FileSystemResult <IFileSystem> res = _plugin.Init(Name, ShokoServer.Instance.OAuthProvider, auth, ConnectionString);

            if (res == null || !res.IsOk)
            {
                throw new Exception("Unable to connect to '" + Provider + "'");
            }
            string userauth = res.Result.GetUserAuthorization();

            if (ConnectionString != userauth)
            {
                NeedSave         = true;
                ConnectionString = userauth;
            }
            return(res.Result);
        }
        internal async Task <FileSystemResult <dynamic> > List(string url)
        {
            int            count;
            List <dynamic> accum = new List <dynamic>();

            do
            {
                FileSystemResult <ExpandoObject> cl = await FS.OAuth.CreateMetadataStream <ExpandoObject>(url);

                if (!cl.IsOk)
                {
                    return(new FileSystemResult <dynamic>(cl.Error));
                }
                dynamic obj = cl.Result;
                count = obj.children.Count;
                if (count > 0)
                {
                    accum.AddRange(obj.items);
                    if (!((IDictionary <string, object>)obj).ContainsKey("@odata.nextLink"))
                    {
                        count = 0;
                    }
                    else
                    {
                        url = (string)((IDictionary <string, object>)obj)["@odata.nextLink"];
                    }
                }
            } while (count > 0);
            return(new FileSystemResult <dynamic>(accum));
        }
        public bool TryDehydrateFolder(string relativePath, out string errorMessage)
        {
            List <IPlaceholderData> removedPlaceholders  = null;
            List <string>           removedModifiedPaths = null;

            errorMessage = string.Empty;

            try
            {
                relativePath         = GVFSDatabase.NormalizePath(relativePath);
                removedPlaceholders  = this.placeholderDatabase.RemoveAllEntriesForFolder(relativePath);
                removedModifiedPaths = this.modifiedPaths.RemoveAllEntriesForFolder(relativePath);
                FileSystemResult result = this.fileSystemVirtualizer.DehydrateFolder(relativePath);
                if (result.Result != FSResult.Ok)
                {
                    errorMessage = $"{nameof(this.TryDehydrateFolder)} failed with {result.Result}";
                    this.context.Tracer.RelatedError(errorMessage);
                }
            }
            catch (Exception ex)
            {
                errorMessage = $"{nameof(this.TryDehydrateFolder)} threw an exception - {ex.Message}";
                EventMetadata metadata = this.CreateEventMetadata(relativePath, ex);
                this.context.Tracer.RelatedError(metadata, errorMessage);
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                if (removedPlaceholders != null)
                {
                    foreach (IPlaceholderData data in removedPlaceholders)
                    {
                        try
                        {
                            this.placeholderDatabase.AddPlaceholderData(data);
                        }
                        catch (Exception ex)
                        {
                            EventMetadata metadata = this.CreateEventMetadata(data.Path, ex);
                            this.context.Tracer.RelatedError(metadata, $"{nameof(FileSystemCallbacks)}.{nameof(this.TryDehydrateFolder)} failed to add '{data.Path}' back into PlaceholderDatabase");
                        }
                    }
                }

                if (removedModifiedPaths != null)
                {
                    foreach (string modifiedPath in removedModifiedPaths)
                    {
                        if (!this.modifiedPaths.TryAdd(modifiedPath, isFolder: modifiedPath.EndsWith(GVFSConstants.GitPathSeparatorString), isRetryable: out bool isRetryable))
                        {
                            this.context.Tracer.RelatedError($"{nameof(FileSystemCallbacks)}.{nameof(this.TryDehydrateFolder)}: failed to add '{modifiedPath}' back into ModifiedPaths");
                        }
                    }
                }
            }

            return(string.IsNullOrEmpty(errorMessage));
        }
Beispiel #22
0
        internal virtual async Task <FileSystemResult> Login(Dictionary <string, object> authorization, string name, bool isUserAuth, bool scopescommaseparated)
        {
            if (!authorization.ContainsKey(ClientIdString) || !(authorization[ClientIdString] is string))
            {
                return(new FileSystemResult("Unable to find " + name + " '" + ClientIdString + "' in settings"));
            }
            ClientId = (string)authorization[ClientIdString];
            if (!authorization.ContainsKey(ClientSecretString) || !(authorization[ClientSecretString] is string))
            {
                return(new FileSystemResult("Unable to find " + name + " '" + ClientSecretString + "' in settings"));
            }
            ClientSecret = (string)authorization[ClientSecretString];
            if (!authorization.ContainsKey(RedirectUriString) || !(authorization[RedirectUriString] is string))
            {
                RedirectUri = DefaultRedirectUri;
            }
            else
            {
                RedirectUri = (string)authorization[RedirectUriString];
            }
            if (!authorization.ContainsKey(ScopesString) || !(authorization[ScopesString] is List <string>))
            {
                Scopes = DefaultScopes;
            }
            else
            {
                Scopes = (List <string>)authorization[ScopesString];
            }
            if (!authorization.ContainsKey(UserAgentString) || !(authorization[UserAgentString] is string))
            {
                UserAgent = "CloudFileSystem/1.0";
            }
            else
            {
                UserAgent = (string)authorization[UserAgentString];
            }
            if (isUserAuth)
            {
                FileSystemResult r = await FillFromUserAuth();

                return(r);
            }
            if (_provider == null)
            {
                return(new FileSystemResult <IFileSystem>("Cannot find valid Authorization Provider for " + name));
            }
            AuthRequest request = new AuthRequest {
                Name = name, LoginUrl = OAuthLoginUrl, ClientId = ClientId, Scopes = Scopes, RedirectUri = RedirectUri, ScopesCommaSeparated = scopescommaseparated
            };
            AuthResult result = await _provider.Login(request);

            if (result.HasError)
            {
                return(new FileSystemResult <IFileSystem>(result.ErrorString));
            }
            return(await FillFromLogin(result.Code));
        }
Beispiel #23
0
        public IFile GetFile()
        {
            IFileSystem fs = ImportFolder.FileSystem;
            FileSystemResult <IObject> fobj = fs?.Resolve(FullServerPath);

            if (fobj == null || !fobj.IsOk || fobj.Result is IDirectory)
            {
                return(null);
            }
            return(fobj.Result as IFile);
        }
        public async Task <FileSystemResult> TouchAsync()
        {
            string url = GoogleTouch.FormatRest(Id);
            FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, null, null, HttpMethod.Post);

            if (ex.IsOk)
            {
                SetData(ex.Result);
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
        public async Task <FileSystemResult <IFileSystem> > InitAsync(string fname, IOAuthProvider provider, Dictionary <string, object> settings, string userauthorization = null)
        {
            FileSystemResult <OneDriveFileSystem> r = await OneDriveFileSystem.Create(fname, provider, settings, Name, userauthorization);

            if (!r.IsOk)
            {
                return(new FileSystemResult <IFileSystem>(r.Error));
            }
            OneDriveFileSystem f = r.Result;

            return(new FileSystemResult <IFileSystem>(f));
        }
        public async Task <FileSystemResult> WriteMetadataAsync(ExpandoObject metadata)
        {
            string url = Item.FormatRest(Id);
            FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(metadata)), "application/json", new HttpMethod("PATCH"));

            if (ex.IsOk)
            {
                SetData(ex.Result);
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
Beispiel #27
0
        internal async Task <FileSystemResult> CheckExpirations()
        {
            FileSystemResult r = await OAuth.MayRefreshToken();

            if (!r.IsOk)
            {
                return(r);
            }
            r = await OAuth.MayRefreshEndPoint();

            return(r);
        }
Beispiel #28
0
        public async Task <FileSystemResult> PopulateAsync()
        {
            using (await _populateLock.LockAsync())
            {
                FileSystemResult r = await FS.CheckExpirations();

                if (!r.IsOk)
                {
                    return(r);
                }
                string url = AmazonList.FormatRest(FS.OAuth.EndPoint.MetadataUrl, Id);
                FileSystemResult <dynamic> fr = await List(url);

                if (!fr.IsOk)
                {
                    return(new FileSystemResult(fr.Error));
                }
                IntFiles = new List <AmazonFile>();
                List <IDirectory> dirlist = new List <IDirectory>();
                foreach (dynamic v in fr.Result)
                {
                    if (v.kind == "FOLDER")
                    {
                        AmazonDirectory dir = new AmazonDirectory(FullName, FS)
                        {
                            Parent = this
                        };
                        dir.SetData(JsonConvert.SerializeObject(v));
                        if ((dir.Attributes & ObjectAttributes.Trashed) != ObjectAttributes.Trashed)
                        {
                            dirlist.Add(dir);
                        }
                    }
                    else if (v.kind == "FILE")
                    {
                        AmazonFile f = new AmazonFile(FullName, FS)
                        {
                            Parent = this
                        };
                        f.SetData(JsonConvert.SerializeObject(v));
                        if ((f.Attributes & ObjectAttributes.Trashed) != ObjectAttributes.Trashed)
                        {
                            IntFiles.Add(f);
                        }
                    }
                }
                FS.Refs.AddDirectories(dirlist, this);
                IntDirectories = dirlist.Cast <AmazonDirectory>().ToList();
                IsPopulated    = true;
                return(new FileSystemResult());
            }
        }
        public static async Task <FileSystemResult <IFileSystem> > Create(string name)
        {
            LocalFileSystem l = new LocalFileSystem();

            l.fname = name;
            FileSystemResult r = await l.PopulateAsync();

            if (!r.IsOk)
            {
                return(new FileSystemResult <IFileSystem>(r.Error));
            }
            return(new FileSystemResult <IFileSystem>(l));
        }
Beispiel #30
0
        public Media GetMediaFromUser(int userID)
        {
            Media n = null;

            if (Media == null)
            {
                VideoLocal_Place pl = GetBestVideoLocalPlace();
                if (pl != null)
                {
                    IFileSystem f = pl.ImportFolder.FileSystem;
                    FileSystemResult <IObject> src = f.Resolve(pl.FullServerPath);
                    if (src != null && src.IsOk && src.Result is IFile)
                    {
                        if (pl.RefreshMediaInfo())
                        {
                            RepoFactory.VideoLocal.Save(pl.VideoLocal, true);
                        }
                    }
                }
            }
            if (Media != null)
            {
                n = Media.DeepClone();
                if (n?.Parts != null)
                {
                    foreach (Part p in n?.Parts)
                    {
                        string name = UrlSafe.Replace(Path.GetFileName(FileName), " ").Replace("  ", " ").Replace("  ", " ").Trim();
                        name = UrlSafe2.Replace(name, string.Empty).Trim().Replace("..", ".").Replace("..", ".").Replace("__", "_").Replace("__", "_").Replace(" ", "_").Replace("_.", ".");
                        while (name.StartsWith("_"))
                        {
                            name = name.Substring(1);
                        }
                        while (name.StartsWith("."))
                        {
                            name = name.Substring(1);
                        }
                        p.Key = PlexAndKodi.Helper.ReplaceSchemeHost(PlexAndKodi.Helper.ConstructVideoLocalStream(userID, VideoLocalID.ToString(), name, false));
                        if (p.Streams != null)
                        {
                            foreach (Stream s in p.Streams.Where(a => a.File != null && a.StreamType == "3"))
                            {
                                s.Key = PlexAndKodi.Helper.ReplaceSchemeHost(PlexAndKodi.Helper.ConstructFileStream(userID, s.File, false));
                            }
                        }
                    }
                }
            }
            return(n);
        }
        public void MoveFileIfRequired()
        {
            try
            {
                logger.Trace("Attempting to MOVE file: {0}", this.FullServerPath);

                // check if this file is in the drop folder
                // otherwise we don't need to move it
                if (ImportFolder.IsDropSource == 0)
                {
                    logger.Trace("Not moving file as it is NOT in the drop folder: {0}", this.FullServerPath);
                    return;
                }
                IFileSystem f = this.ImportFolder.FileSystem;
                if (f == null)
                {
                    logger.Trace("Unable to MOVE, filesystem not working: {0}", this.FullServerPath);
                    return;

                }

                FileSystemResult<IObject> fsrresult = f.Resolve(FullServerPath);
                if (!fsrresult.IsOk)
                {
                    logger.Error("Could not find the file to move: {0}", this.FullServerPath);
                    return;
                }
                IFile source_file = fsrresult.Result as IFile;
                if (source_file == null)
                {
                    logger.Error("Could not find the file to move: {0}", this.FullServerPath);
                    return;
                }
                // find the default destination
                ImportFolder destFolder = null;
                foreach (ImportFolder fldr in RepoFactory.ImportFolder.GetAll().Where(a => a.CloudID == ImportFolder.CloudID))
                {
                    if (fldr.IsDropDestination == 1)
                    {
                        destFolder = fldr;
                        break;
                    }
                }

                if (destFolder == null) return;

                FileSystemResult<IObject> re = f.Resolve(destFolder.ImportFolderLocation);
                if (!re.IsOk)
                    return;

                // keep the original drop folder for later (take a copy, not a reference)
                ImportFolder dropFolder = this.ImportFolder;

                // we can only move the file if it has an anime associated with it
                List<CrossRef_File_Episode> xrefs = this.VideoLocal.EpisodeCrossRefs;
                if (xrefs.Count == 0) return;
                CrossRef_File_Episode xref = xrefs[0];

                // find the series associated with this episode
                AnimeSeries series = RepoFactory.AnimeSeries.GetByAnimeID(xref.AnimeID);
                if (series == null) return;

                // find where the other files are stored for this series
                // if there are no other files except for this one, it means we need to create a new location
                bool foundLocation = false;
                string newFullPath = "";

                // sort the episodes by air date, so that we will move the file to the location of the latest episode
                List<AnimeEpisode> allEps = series.GetAnimeEpisodes().OrderByDescending(a => a.AniDB_EpisodeID).ToList();

                IDirectory destination = null;

                foreach (AnimeEpisode ep in allEps)
                {
                    // check if this episode belongs to more than one anime
                    // if it does we will ignore it
                    List<CrossRef_File_Episode> fileEpXrefs = RepoFactory.CrossRef_File_Episode.GetByEpisodeID(ep.AniDB_EpisodeID);
                    int? animeID = null;
                    bool crossOver = false;
                    foreach (CrossRef_File_Episode fileEpXref in fileEpXrefs)
                    {
                        if (!animeID.HasValue)
                            animeID = fileEpXref.AnimeID;
                        else
                        {
                            if (animeID.Value != fileEpXref.AnimeID)
                                crossOver = true;
                        }
                    }
                    if (crossOver) continue;

                    foreach (VideoLocal vid in ep.GetVideoLocals().Where(a => a.Places.Any(b=>b.ImportFolder.CloudID == destFolder.CloudID && b.ImportFolder.IsDropSource==0)))
                    {
                        if (vid.VideoLocalID != this.VideoLocalID)
                        {
                            VideoLocal_Place place=vid.Places.FirstOrDefault(a=>a.ImportFolder.CloudID==destFolder.CloudID);
                            string thisFileName = place?.FullServerPath;
                            string folderName = Path.GetDirectoryName(thisFileName);

                            FileSystemResult<IObject> dir = f.Resolve(folderName);
                            if (dir.IsOk)
                            {
                                destination = (IDirectory)dir.Result;
                                newFullPath = folderName;
                                foundLocation = true;
                                break;
                            }
                        }
                    }
                    if (foundLocation) break;
                }

                if (!foundLocation)
                {
                    // we need to create a new folder
                    string newFolderName = Utils.RemoveInvalidFolderNameCharacters(series.GetAnime().PreferredTitle);

                    newFullPath =Path.Combine(destFolder.ParsedImportFolderLocation, newFolderName);
                    FileSystemResult<IObject> dirn = f.Resolve(newFullPath);
                    if (!dirn.IsOk)
                    {
                        dirn = f.Resolve(destFolder.ImportFolderLocation);
                        if (dirn.IsOk)
                        {
                            IDirectory d = (IDirectory)dirn.Result;
                            FileSystemResult<IDirectory> d2 = Task.Run(async () => await d.CreateDirectoryAsync(newFolderName, null)).Result;
                            destination = d2.Result;

                        }
                    }
                    else if (dirn.Result is IFile)
                    {
                        logger.Error("Destination folder is a file: {0}", newFolderName);
                    }
                    else
                    {
                        destination = (IDirectory) dirn.Result;
                    }
                }

                string newFullServerPath = Path.Combine(newFullPath, Path.GetFileName(this.FullServerPath));
                Tuple<ImportFolder, string> tup = VideoLocal_PlaceRepository.GetFromFullPath(newFullServerPath);
                if (tup == null)
                {
                    logger.Error($"Unable to LOCATE file {newFullServerPath} inside the import folders");
                    return;
                }

                logger.Info("Moving file from {0} to {1}", this.FullServerPath, newFullServerPath);

                FileSystemResult<IObject> dst = f.Resolve(newFullServerPath);
                if (dst.IsOk)
                {
                    logger.Trace("Not moving file as it already exists at the new location, deleting source file instead: {0} --- {1}",
                        this.FullServerPath, newFullServerPath);

                    // if the file already exists, we can just delete the source file instead
                    // this is safer than deleting and moving
                    FileSystemResult fr = new FileSystemResult();
                    try
                    {
                        fr = source_file.Delete(false);
                        if (!fr.IsOk)
                        {
                            logger.Warn("Unable to DELETE file: {0} error {1}", this.FullServerPath, fr?.Error ?? String.Empty);
                        }
                        this.ImportFolderID = tup.Item1.ImportFolderID;
                        this.FilePath = tup.Item2;
                        RepoFactory.VideoLocalPlace.Save(this);

                        // check for any empty folders in drop folder
                        // only for the drop folder
                        if (dropFolder.IsDropSource == 1)
                        {
                            FileSystemResult<IObject> dd = f.Resolve(dropFolder.ImportFolderLocation);
                            if (dd != null && dd.IsOk && dd.Result is IDirectory)
                            {
                                RecursiveDeleteEmptyDirectories((IDirectory)dd.Result, true);
                            }
                        }
                    }
                    catch
                    {
                        logger.Error("Unable to DELETE file: {0} error {1}", this.FullServerPath, fr?.Error ?? String.Empty);
                    }
                }
                else
                {
                    FileSystemResult fr = source_file.Move(destination);
                    if (!fr.IsOk)
                    {
                        logger.Error("Unable to MOVE file: {0} to {1} error {2)", this.FullServerPath, newFullServerPath, fr?.Error ?? String.Empty);
                        return;
                    }
                    string originalFileName = this.FullServerPath;

                    this.ImportFolderID = tup.Item1.ImportFolderID;
                    this.FilePath = tup.Item2;
                    RepoFactory.VideoLocalPlace.Save(this);

                    try
                    {
                        // move any subtitle files
                        foreach (string subtitleFile in Utils.GetPossibleSubtitleFiles(originalFileName))
                        {
                            FileSystemResult<IObject> src = f.Resolve(subtitleFile);
                            if (src.IsOk && src.Result is IFile)
                            {
                                string newSubPath = Path.Combine(Path.GetDirectoryName(newFullServerPath), ((IFile)src.Result).Name);
                                dst = f.Resolve(newSubPath);
                                if (dst.IsOk && dst.Result is IFile)
                                {
                                    FileSystemResult fr2 = src.Result.Delete(true);
                                    if (!fr2.IsOk)
                                    {
                                        logger.Warn("Unable to DELETE file: {0} error {1}", subtitleFile,
                                            fr2?.Error ?? String.Empty);
                                    }
                                }
                                else
                                {
                                    FileSystemResult fr2 = ((IFile)src.Result).Move(destination);
                                    if (!fr2.IsOk)
                                    {
                                        logger.Error("Unable to MOVE file: {0} to {1} error {2)", subtitleFile,
                                            newSubPath, fr2?.Error ?? String.Empty);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error( ex,ex.ToString());
                    }

                    // check for any empty folders in drop folder
                    // only for the drop folder
                    if (dropFolder.IsDropSource == 1)
                    {
                        FileSystemResult<IObject> dd = f.Resolve(dropFolder.ImportFolderLocation);
                        if (dd != null && dd.IsOk && dd.Result is IDirectory)
                            RecursiveDeleteEmptyDirectories((IDirectory)dd.Result,true);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = $"Could not MOVE file: {this.FullServerPath} -- {ex.ToString()}";
                logger.Error( ex,msg);
            }
        }