示例#1
0
        protected void on_TagEntry_activate(object sender, EventArgs args)
        {
            if (this.findThread != null)
            {
                this.stopThread = true;
                this.findThread.Join();
                this.findThread = null;
            }

            this.store.Clear();
            this.stopThread = false;

            this.store = new ListStore(typeof(string), typeof(Gdk.Pixbuf), typeof(BooruImage), typeof(float));
            //store.SetSortColumnId(3, SortType.Descending);
            this.ImageThumbView.Model = store;
            this.Spinner.Active       = true;

            System.IO.Directory.CreateDirectory("/home/kolrabi/x/extract/" + TagEntry.Text);

            var start = new ThreadStart(() => {
                var reader        = this.db.QueryImagesWithTags(TagEntry.Text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                var readerColumns = new Dictionary <string, int> ();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    readerColumns [reader.GetName(i)] = i;
                }

                while (reader.Read() && !this.stopThread)
                {
                    string path    = reader.GetString(readerColumns ["path"]);
                    object md5obj  = reader.GetValue(readerColumns ["md5sum"]);
                    float elo      = reader.GetFloat(readerColumns ["elo"]);
                    int votes      = reader.GetInt32(readerColumns ["votes"]);
                    byte[] md5blob = (byte[])md5obj;

                    var data  = new BooruImageData(MD5Helper.BlobToMD5(md5blob), path, elo, votes);
                    var image = new BooruImage(data, this.db);

                    try {
                        var info = new Mono.Unix.UnixFileInfo(data.Path);
                        info.CreateSymbolicLink("/home/kolrabi/x/extract/" + TagEntry.Text + "/" + data.MD5 + System.IO.Path.GetExtension(data.Path));
                        //System.IO.File.Copy(data.Path, "/home/kolrabi/x/extract/" + TagEntry.Text + "/"+data.MD5+System.IO.Path.GetExtension(data.Path));
                    } catch (Exception ex) {
                        Console.WriteLine("Could not copy " + data.Path + ": " + ex.Message);
                    }

                    Gtk.Application.Invoke((s, a) => {
                        this.store.AppendValues(data.Path + "\n" + string.Join(" ", image.Tags), null, image, data.ELO);
                    });
                }
                Gtk.Application.Invoke((s, a) => {
                    this.Spinner.Active = false;
                });
            });

            this.findThread = new Thread(start);
            this.findThread.Start();
        }
示例#2
0
        /// <summary>
        /// Creates a symlink
        /// </summary>
        /// <param name="target">Link target</param>
        /// <param name="source">The relative path of the new link being created</param>
        public void MakeSymlink(string target, string source)
        {
            OperatingSystem os = Environment.OSVersion;

            switch (os.Platform)
            {
            case PlatformID.Win32NT:
            case PlatformID.Win32S:
            case PlatformID.Win32Windows:
                // Windows: we use CreateSymbolicLink from kernel32.dll
                int flag = Directory.Exists(target) ? UnsafeNativeMethods.SYMLINK_FLAG_DIRECTORY : 0;
                UnsafeNativeMethods.CreateSymbolicLink(source, target, flag);
                break;

            case PlatformID.MacOSX:
            case PlatformID.Unix:
                // Linux, macOS on Mono: use Mono.Posix
                Mono.Unix.UnixFileInfo f = new Mono.Unix.UnixFileInfo(target);
                f.CreateSymbolicLink(String.Format("\"{0}{1}{2}\"", RootDirectory, Path.DirectorySeparatorChar, source));

                return;

                /**
                 * // Linux, macOS on plain .NET: we run the ln command directly
                 * Process p = new Process();
                 * p.StartInfo.UseShellExecute = false;
                 * p.StartInfo.RedirectStandardOutput = true;
                 * p.StartInfo.FileName = "ln";
                 *
                 * StringBuilder sbArgs = new StringBuilder(@"-s ", 512);
                 * sbArgs.AppendFormat("\"{0}\"", target);
                 * sbArgs.Append(' ');
                 * sbArgs.AppendFormat("\"{0}{1}{2}\"", RootDirectory, Path.DirectorySeparatorChar, source);
                 *
                 * p.StartInfo.Arguments = sbArgs.ToString();
                 *
                 * p.Start();
                 *
                 * // Read the output stream first and then wait.
                 * string output = p.StandardOutput.ReadToEnd();
                 * /**/

                break;
            }
        }
示例#3
0
        public static void Extract(this ZipArchive archive, string directoryName)
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string path = Path.Combine(directoryName, entry.FullName);
                if (entry.Length == 0 && (path.EndsWith("/", StringComparison.Ordinal) || path.EndsWith("\\", StringComparison.Ordinal)))
                {
                    // Extract directory
                    FileSystemHelpers.CreateDirectory(path);
                }
                else
                {
                    FileInfoBase fileInfo = FileSystemHelpers.FileInfoFromFileName(path);

                    if (!fileInfo.Directory.Exists)
                    {
                        fileInfo.Directory.Create();
                    }

                    using (Stream zipStream = entry.Open(),
                           fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                    {
                        zipStream.CopyTo(fileStream);
                    }

                    bool   createSymLink    = false;
                    string originalFileName = string.Empty;
                    if (!OSDetector.IsOnWindows())
                    {
                        try
                        {
                            using (Stream fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                byte[] buffer = new byte[10];
                                fs.Read(buffer, 0, buffer.Length);
                                fs.Close();

                                var str = System.Text.Encoding.Default.GetString(buffer);
                                if (str.StartsWith("../"))
                                {
                                    string fullPath = Path.GetFullPath(str);
                                    if (fullPath.StartsWith(directoryName))
                                    {
                                        createSymLink    = true;
                                        originalFileName = fullPath;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not identify symlinks in zip file : " + ex.ToString());
                        }
                    }

                    fileInfo.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().DateTime;

                    if (createSymLink)
                    {
                        try
                        {
                            fileInfo.Delete();

                            Mono.Unix.UnixFileInfo unixFileInfo = new Mono.Unix.UnixFileInfo(originalFileName);
                            unixFileInfo.CreateSymbolicLink(path);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Could not create symlinks : " + ex.ToString());
                        }
                    }
                }
            }
        }