A file preview image conversion method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202311377505.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-23
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2043-10-23
AI Technical Summary
人们需要快速的掌握信息咨询,这其中office文档预览是很重要的操作,现在的实现方案主要是把office文档转换成pdf文件,但有的pdf合同文件比较大,如果直接传输整个pdf床到前端预览,会导致耗时较多且资源占用较多
[0008]可选的,在本申请实施例中,利用构建线程根据待转换文件对应的属性字段,构建转换实例组,包括:构建锁对象;锁对象用于控制转换实例组由对应的一个构建线程进行构建;基于锁对象,判断构建线程是否获取锁资格;若构建线程获取锁资格,则利用构建线程根据待转换文件对应的属性字段,构建转换实例组;若构建线程未获取锁资格,则将构建线程的状态变更为阻塞等待。
Smart Images

Figure CN117349229B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a method, apparatus, electronic device, and storage medium for converting file preview images. Background Technology
[0002] With the development and widespread adoption of smartphones and mobile internet, mobile office has become a trend. People need to quickly access information, and previewing Office documents is a crucial operation in this process. Current solutions mainly involve converting Office documents into PDF files. However, some PDF contract files are quite large, and directly transmitting the entire PDF to the front end for preview would result in significant time consumption and resource usage. Summary of the Invention
[0003] The purpose of this application is to provide a file preview image conversion method, apparatus, electronic device, and storage medium that previews contracts incrementally. Before conversion, key information data of the file body is parsed into the conversion instance object, making more reasonable use of server resources, reducing resource consumption, and improving the efficiency of file preview.
[0004] In a first aspect, embodiments of this application provide a method for converting a file preview image, comprising: obtaining a file conversion request; the file conversion request including a file to be converted; the file to be converted including a file identifier; obtaining a conversion instance group corresponding to the file to be converted based on the file identifier; the conversion instance group being constructed by a pre-created build thread corresponding to the file to be converted; the conversion instance group being used to store attribute information of the file to be converted; and converting the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted.
[0005] In the above implementation process, the key information data of the file body is parsed into the conversion instance object before conversion, so as to make more reasonable use of server resources, reduce resource consumption, and improve the efficiency of file preview.
[0006] Optionally, in this embodiment, obtaining the conversion instance group corresponding to the file to be converted based on the file identifier includes: creating a build thread corresponding to the file to be converted; the build thread is used to build the conversion instance group corresponding to the file to be converted; obtaining the attribute fields corresponding to the file to be converted; using the build thread to build the conversion instance group according to the attribute fields corresponding to the file to be converted; storing the mapping relationship between the file identifier of the file to be converted and the conversion instance group in a hash set; and obtaining the conversion instance group corresponding to the file to be converted from the hash set based on the file identifier.
[0007] In the above implementation process, a conversion instance group is constructed based on the file to be converted. The conversion instance group includes multiple conversion instances. The conversion instances in the conversion instance group are used to convert images to obtain the preview image corresponding to the file to be converted. By managing the conversion instances, the conversion of the file to be converted can be controlled, making more reasonable use of server resources, reducing resource consumption, and improving the efficiency of file preview.
[0008] Optionally, in this embodiment, the construction thread constructs a conversion instance group based on the attribute fields corresponding to the file to be converted, including: constructing a lock object; the lock object is used to control that the conversion instance group is constructed by a corresponding construction thread; based on the lock object, it is determined whether the construction thread has acquired the lock qualification; if the construction thread has acquired the lock qualification, the construction thread constructs the conversion instance group based on the attribute fields corresponding to the file to be converted; if the construction thread has not acquired the lock qualification, the state of the construction thread is changed to blocked waiting.
[0009] In the above implementation, by constructing a lock object, the construction of each transformation instance group is controlled by a corresponding construction thread, ensuring that each file to be transformed has its own transformation instance group, thus improving resource utilization. This avoids the situation where multiple construction threads construct the same transformation instance group for the same file, resulting in resource waste. Simultaneously, the lock ensures thread safety, and the transformation instance group is cached in a thread-safe hash set.
[0010] Optionally, in this embodiment of the application, converting the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted includes: using a thread that has not acquired lock eligibility as a conversion thread; the conversion thread is used to convert the conversion instances into images; and using the conversion thread to convert the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted.
[0011] In the above implementation process, by constructing a lock object, the conversion instance group is controlled to be constructed by a corresponding construction thread, so that a file to be converted has a conversion instance group. Threads that do not acquire the lock can perform image conversion on the conversion instance, thereby improving resource utilization.
[0012] Optionally, in this embodiment of the application, after converting the conversion instances in the conversion instance group into images, the method further includes: obtaining the read lock attribute in the attribute information of the file to be converted, wherein the read lock attribute is used to count the number of conversion instances that have completed the image conversion; and determining, based on the total number of conversion instances in the conversion instance group and the read lock attribute, that all conversion instances in the conversion instance group have completed the image conversion.
[0013] In the above implementation process, by querying the read locks in the conversion instance group and the total number of conversion instances in the conversion instance group, it is determined whether there are any unfinished conversion tasks in the conversion instance group. This ensures that all conversion instances in the conversion instance group have completed the image conversion, improves the responsiveness of conversion requests, and better manages the conversion of preview images.
[0014] Optionally, in this embodiment, determining that all conversion instances in the conversion instance group have completed image conversion based on the total number of conversion instances in the conversion instance group and the read lock attribute includes: obtaining a preset timer corresponding to the conversion instance; the timer is used to determine whether the conversion instance has expired; based on the total number of conversion instances in the conversion instance group and the read lock attribute, determining whether the current conversion instance is the last conversion instance to perform image conversion; if the current conversion instance is the last conversion instance to perform image conversion, then if the timer corresponding to the current conversion instance has expired, the read lock is released and the conversion task corresponding to the file to be converted is terminated; if the current conversion instance is not the last conversion instance to perform image conversion, then the timer of the current conversion instance is reset.
[0015] In the above implementation process, if the number of conversion instances is not properly managed or if conversion instances are released too late, the consumption of server resources such as memory, disk, and CPU will become uncontrollable, easily leading to resource bottlenecks such as memory overflow. It may also cause multiple copies of the PDF to be downloaded to local temporary files, and a surge in downloads will consume more storage concurrency and bandwidth, all of which can cause system lag and slow interface response. Read locks can conveniently and accurately record the number of conversion tasks, making it easy to determine whether conversion instances still have tasks, while timers can be used to determine whether conversion instances have expired and can be released.
[0016] Optionally, in this embodiment of the application, after obtaining the file conversion request, the method further includes: performing format verification on the file to be converted in the file conversion request and obtaining the format verification result; if the format verification result passes, performing the conversion instance group corresponding to the file to be converted based on the file identifier; if the format verification result fails, outputting the format verification result.
[0017] In the above implementation process, format verification comprehensively verifies the correctness of the file's format and content, ensuring the file's reliability and security. This improves the success rate of converting files.
[0018] Secondly, embodiments of this application also provide a file preview image conversion apparatus, comprising: an acquisition module for acquiring a file conversion request; the file conversion request includes a file to be converted; the file to be converted includes a file identifier; a conversion instance group module for obtaining a conversion instance group corresponding to the file to be converted based on the file identifier; the conversion instance group is constructed by a pre-created construction thread corresponding to the file to be converted; the conversion instance group is used to store attribute information of the file to be converted; and an image conversion module for converting the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted.
[0019] Thirdly, embodiments of this application also provide an electronic device, including: a processor and a memory, the memory storing machine-readable instructions executable by the processor, which, when executed by the processor, perform the method described above.
[0020] Fourthly, embodiments of this application also provide a computer-readable storage medium storing a computer program that, when executed by a processor, performs the methods described above.
[0021] The file preview image conversion method, apparatus, electronic device and storage medium provided in this application will first parse the key information data of the file body into the conversion instance object before conversion, so as to make more reasonable use of server resources, reduce resource consumption and improve the efficiency of file preview. Attached Figure Description
[0022] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 A flowchart illustrating a file preview image conversion method provided in this application embodiment;
[0024] Figure 2 A flowchart illustrating the conversion instance management and expiration policy provided in this application embodiment;
[0025] Figure 3 This is a schematic diagram of the structure of the file preview image conversion device provided in the embodiments of this application;
[0026] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0027] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.
[0028] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit this application.
[0029] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more, unless otherwise explicitly defined.
[0030] Electronic signature technology is becoming increasingly widespread. PDF files are an excellent medium for electronic contracts, and they are indispensable in the signing process. Viewing an electronic contract also requires previewing its content. However, PDF contract files are often large and contain a lot of content. Directly transmitting the entire PDF to the front end for previewing would be very slow, potentially triggering bottlenecks during transmission and preventing previewing altogether. Furthermore, it consumes a significant amount of bandwidth.
[0031] To address these issues, this application's embodiments employ an incremental preview method. Incremental preview transmits the view of only the page the customer needs to view to the front end. This incremental preview solution is implemented by converting the PDF into images.
[0032] For example, a conversion instance group can be built based on the file to be converted. The conversion instance group includes multiple conversion instances. The conversion instances in the conversion instance group are used to convert images to obtain a preview image corresponding to the file to be converted. By managing the conversion instances, the conversion of the file to be converted can be controlled, making more reasonable use of server resources, reducing resource consumption, and improving the efficiency of file preview.
[0033] Please see Figure 1The illustrated diagram shows a flowchart of a file preview image conversion method provided in an embodiment of this application. The file preview image conversion method provided in this application can be applied to electronic devices, which may include a terminal and a server; wherein the terminal may specifically be a smartphone, tablet computer, computer, personal digital assistant (PDA), etc.; the server may specifically be an application server or a web server. The file preview image conversion method may include:
[0034] Step S110: Obtain a file conversion request; the file conversion request includes the file to be converted; the file to be converted includes a file identifier.
[0035] Step S120: Obtain the conversion instance group corresponding to the file to be converted based on the file identifier; the conversion instance group is constructed by the build thread corresponding to the pre-created file to be converted; the conversion instance group is used to store the attribute information of the file to be converted.
[0036] Step S130: Convert the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted.
[0037] In step S110, the user terminal initiates a file conversion request, which is then received and processed by the file preview image conversion system. The file conversion request includes the file to be converted, which can be a PDF file that needs to be previewed. The file to be converted includes a file identifier, which is used to uniquely identify and distinguish different files.
[0038] In step S120, the file identifier and conversion instance group of the file to be converted are pre-stored in a storage medium such as a database. After obtaining the file identifier, the conversion instance group corresponding to the file to be converted can be obtained based on the file identifier. The conversion instance group can be pre-constructed through threads.
[0039] In an optional embodiment, to prevent multiple threads from repeatedly constructing the same conversion instance group for the file to be converted, a target construction thread corresponding to the file to be converted can be determined first. This target construction thread is then used to construct the conversion instance group for the file to be converted based on its attribute information. The conversion instance group can store information such as the temporary storage directory, file identifier field, file size, total number of pages, and conversion object of the file to be converted. The conversion instance group can also store lock objects and read lock-related attribute fields.
[0040] In step S130, the conversion thread performs image conversion on the conversion instances in the conversion instance group to obtain a preview image corresponding to the file to be converted. The conversion thread is used to perform image conversion on the conversion instances.
[0041] In the above implementation process, the key information data of the file body is parsed into the conversion instance object before conversion, so as to make more reasonable use of server resources, reduce resource consumption, and improve the efficiency of file preview.
[0042] Optionally, in this embodiment, obtaining the conversion instance group corresponding to the file to be converted based on the file identifier includes: creating a build thread corresponding to the file to be converted; the build thread is used to build the conversion instance group corresponding to the file to be converted; obtaining the attribute fields corresponding to the file to be converted; using the build thread to build the conversion instance group according to the attribute fields corresponding to the file to be converted; storing the mapping relationship between the file identifier of the file to be converted and the conversion instance group in a hash set; and obtaining the conversion instance group corresponding to the file to be converted from the hash set based on the file identifier.
[0043] In the specific implementation process: when creating the build thread corresponding to the file to be converted, the time wheel technique can be used. Therefore, the build thread can be a time wheel thread, which is used to build the conversion instance group corresponding to the file to be converted. The time wheel technique is a technique used to build scheduled tasks. The time wheel can be used in conjunction with threads to execute multiple delayed tasks with different delay times using a small number of threads, and it can also execute tasks within a predetermined time interval.
[0044] To build threads using time wheels, for example: Choose a time wheel library, use its functionality to create a time wheel object. This time wheel object will be used to manage and schedule tasks. Define task functions, which are the code executed within the thread; a task function can be a function, method, or block of code. Use the methods provided by the thread library to manage and control the thread's state, such as starting, pausing, and stopping.
[0045] Retrieve the attribute fields corresponding to the file to be converted. These attribute fields include: the temporary storage directory of the file to be converted, the file identifier field, the file size, the total number of pages, and the conversion object. Then, use a build thread to construct a conversion instance group based on the attribute fields corresponding to the file to be converted.
[0046] For example, a thread is built to retrieve the source file to be converted and records its temporary storage directory in the conversion instance group. During conversion, the file to be converted needs to be retrieved from this directory. The PDF file identifier attributes, file size, total number of pages, thread-safe local lock, and read lock-related attribute fields are retrieved and added to the conversion instance group. The read lock attribute is used to count the number of conversion instances that have completed image conversion. The mapping between the current PDF's identifier attributes and the conversion instance group is recorded in a hash set, which can be a ConcurrentHashMap collection.
[0047] Subsequent conversion tasks for the same file to be converted will carry the same file identifier to the collection to obtain the conversion instance group, so there is no need to repeatedly build the conversion instance group for the file to be converted.
[0048] As an optional implementation, a Map collection with soft references can be used to store and manage the mapping between file identifiers of files to be converted and groups of conversion instances. The advantage of using a Map collection with soft references is that if the data in the Map collection is not referenced and the JVM (Java Virtual Machine) is full, it will be automatically reclaimed, releasing the occupied memory in a timely manner. Soft references are a method of referencing objects in Java.
[0049] In the above implementation process, a conversion instance group is constructed based on the file to be converted. The conversion instance group includes multiple conversion instances. The conversion instances in the conversion instance group are used to convert images to obtain the preview image corresponding to the file to be converted. By managing the conversion instances, the conversion of the file to be converted can be controlled, making more reasonable use of server resources, reducing resource consumption, and improving the efficiency of file preview.
[0050] Optionally, in this embodiment, the construction thread constructs a conversion instance group based on the attribute fields corresponding to the file to be converted, including: constructing a lock object; the lock object is used to control that the conversion instance group is constructed by a corresponding construction thread; based on the lock object, it is determined whether the construction thread has acquired the lock qualification; if the construction thread has acquired the lock qualification, the construction thread constructs the conversion instance group based on the attribute fields corresponding to the file to be converted; if the construction thread has not acquired the lock qualification, the state of the construction thread is changed to blocked waiting.
[0051] In its implementation: the lock object provides an exclusive operation mode, meaning that only one thread is allowed to enter the critical section to access or execute a specific code block at a time. Once a thread acquires control of the lock object, other threads will be blocked until the first thread releases the lock object. In other words, the lock object controls which conversion instance group is built by a corresponding build thread, ensuring that each file to be converted has a dedicated conversion instance group, thus reducing resource waste.
[0052] Constructing a lock object can be done in several ways, such as using the lock object constructor provided by the thread library. The specific implementation and API (interface) will vary depending on the programming language. For example, in Python, you can use the `Lock()` function from the `threading` module to create a lock object.
[0053] To determine whether a thread is eligible for a lock or has already acquired it, you can use methods or properties provided by the lock object. For example, by querying the lock object's state properties, you can determine the ownership status of the lock and thus determine whether a thread is eligible for the lock. For instance, in Java, you can use the `isHeldByCurrentThread()` method to determine if the current thread holds the lock object. In Python, you can use the `locked()` method to determine if the lock object is already locked.
[0054] You can also use the "attempt to acquire lock" operation of the lock object to obtain control of the lock object and get the return value; it returns true if successful, otherwise it returns false. By checking the return value, you can determine whether a thread is eligible to acquire the lock.
[0055] If it is determined that the build thread is qualified to acquire the lock, then the build thread is used to construct a conversion instance group based on the attribute fields corresponding to the file to be converted; if the build thread is not qualified to acquire the lock, then the state of the build thread is changed to blocked waiting.
[0056] In the above implementation, by constructing a lock object, the construction of each transformation instance group is controlled by a corresponding construction thread, ensuring that each file to be transformed has its own transformation instance group, thus improving resource utilization. This avoids the situation where multiple construction threads construct the same transformation instance group for the same file, resulting in resource waste. Simultaneously, the lock ensures thread safety, and the transformation instance group is cached in a thread-safe hash set.
[0057] Optionally, in this embodiment of the application, converting the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted includes: using a thread that has not acquired lock eligibility as a conversion thread; the conversion thread is used to convert the conversion instances into images; and using the conversion thread to convert the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted.
[0058] In the specific implementation process: Threads that have not acquired the lock can act as conversion threads, converting images from conversion instances in the conversion instance group to obtain preview images of the files to be converted.
[0059] In the above implementation process, by constructing a lock object, the conversion instance group is controlled to be constructed by a corresponding construction thread, so that a file to be converted has a conversion instance group. Threads that do not acquire the lock can perform image conversion on the conversion instance, thereby improving resource utilization.
[0060] Optionally, in this embodiment of the application, after converting the conversion instances in the conversion instance group into images, the method further includes: obtaining the read lock attribute in the attribute information of the file to be converted, wherein the read lock attribute is used to count the number of conversion instances that have completed the image conversion; and determining, based on the total number of conversion instances in the conversion instance group and the read lock attribute, that all conversion instances in the conversion instance group have completed the image conversion.
[0061] In the specific implementation process: the attribute information of the file to be converted also includes a read lock attribute. The read lock attribute is used to count the number of conversion instances that have completed image conversion. In other words, the read lock can record how many thread tasks are performing conversion, and determine whether there are any unfinished conversion requests for the file to be converted. Therefore, the number of conversion instances can be accurately controlled through the lock object.
[0062] As an optional implementation, read lock attributes can be stored in the conversion instance group. For example, when constructing the conversion instance group, read lock-related attribute fields can be saved to the conversion instance group. By querying the read locks in the conversion instance group and the total number of conversion instances in the conversion instance group, it can be determined whether there are any unfinished conversion tasks. If the read locks indicate that there are no unfinished conversion tasks, it means that all conversion instances in the conversion instance group have completed image conversion.
[0063] In the above implementation process, by querying the read locks in the conversion instance group and the total number of conversion instances in the conversion instance group, it is determined whether there are any unfinished conversion tasks in the conversion instance group. This ensures that all conversion instances in the conversion instance group have completed the image conversion, improves the responsiveness of conversion requests, and better manages the conversion of preview images.
[0064] Optionally, in this embodiment, determining that all conversion instances in the conversion instance group have completed image conversion based on the total number of conversion instances in the conversion instance group and the read lock attribute includes: obtaining a preset timer corresponding to the conversion instance; the timer is used to determine whether the conversion instance has expired; based on the total number of conversion instances in the conversion instance group and the read lock attribute, determining whether the current conversion instance is the last conversion instance to perform image conversion; if the current conversion instance is the last conversion instance to perform image conversion, then if the timer corresponding to the current conversion instance has expired, the read lock is released and the conversion task corresponding to the file to be converted is terminated; if the current conversion instance is not the last conversion instance to perform image conversion, then the timer of the current conversion instance is reset.
[0065] In the specific implementation process: the conversion instance occupies some server resources. Therefore, after the user preview ends, the conversion instance will be released to free up server resources. Considering that releasing it too early will lead to a poor user experience, while releasing it too late will result in resource consumption, this embodiment of the application uses a timer corresponding to the conversion instance to control the release operation; the timer is used to determine whether the conversion instance has expired.
[0066] For example, after all conversion instances in the conversion instance group have completed image conversion, and there are no new conversion requests for the current file to be converted, in order to avoid conversion instances occupying memory for a long time and wasting resources, it is possible to determine whether the last conversion instance that performed image conversion has expired. If it has expired, the read lock is released and the conversion task corresponding to the file to be converted is terminated.
[0067] Based on the total number of transformation instances in the transformation instance group and the read lock attribute, it is determined whether the current transformation instance is the last one to perform image transformation. For example, if the total number of transformation instances equals the number of transformation instances that have completed image transformation, the current transformation instance can be considered the last one to perform image transformation. Then, it is checked whether the timer corresponding to the current transformation instance has expired. If the timer exceeds the preset time period, the timer corresponding to the current transformation instance is considered to have expired.
[0068] If the timer for the current conversion instance expires, release the read lock and terminate the conversion task for the file to be converted. If the current conversion instance is not the last conversion instance to perform image conversion, reset the timer for the current conversion instance and continue to respond to file conversion requests.
[0069] In the above implementation process, if the number of conversion instances is not properly managed or if conversion instances are released too late, the consumption of server resources such as memory, disk, and CPU (Central Processing Unit) will become uncontrollable, easily leading to resource bottlenecks such as memory overflow. It may also cause multiple copies of the PDF to be downloaded to local temporary files, and a surge in downloads will consume more storage concurrency and bandwidth, all of which can cause system lag and slow interface response. Read locks can conveniently and accurately record the number of conversion tasks, making it easy to determine whether conversion instances still have tasks, while timers can be used to determine whether conversion instances have expired and can be released.
[0070] Optionally, in this embodiment of the application, after obtaining the file conversion request, the method further includes: performing format verification on the file to be converted in the file conversion request and obtaining the format verification result; if the format verification result passes, performing the conversion instance group corresponding to the file to be converted based on the file identifier; if the format verification result fails, outputting the format verification result.
[0071] In the specific implementation process: the format of the file to be converted in the file conversion request is validated, and the format validation result is obtained. Format validation includes, for example, file extension validation, file header validation, data structure validation, data format validation, and / or character encoding validation. If the format validation result is successful, the conversion instance group corresponding to the file to be converted is obtained based on the file identifier; if the format validation result is unsuccessful, the format validation result is output.
[0072] In the above implementation process, format verification comprehensively verifies the correctness of the file's format and content, ensuring the file's reliability and security. This improves the success rate of converting files.
[0073] Please see Figure 2 The diagram shown is a flowchart illustrating the conversion instance management and expiration policy provided in an embodiment of this application.
[0074] After obtaining the file to be converted, the input parameters are validated. Only those that pass the validation can continue to the subsequent conversion steps. If the validation fails, the conversion ends, an overflow message is thrown, and the result is returned directly.
[0075] Create and retrieve threads that monitor the expiration of conversion instances. Construct a conversion instance group. The conversion instance group records the temporary storage directory of the PDF source file, file identifier field, file size, total number of pages, thread-safe local locks, read lock-related attribute fields, such as conversion objects based on pdfbox parsing, etc.
[0076] Construct or obtain a thread-safe lock object for the current PDF. This lock ensures atomicity, guaranteeing that only one thread constructs the corresponding transformation instance group at a time, and that only one transformation instance group exists for a PDF at any given time, thus reducing resource waste.
[0077] Request threads that fail to acquire the lock must block and wait for the thread that acquires the lock to first construct the transformation instance group, and then acquire that transformation instance group to transform the image. Request threads that acquire the lock can then be responsible for constructing, storing, and managing the transformation instance group.
[0078] The thread acquiring the lock constructs a conversion instance group, for example, by retrieving the PDF source file and recording its temporary storage directory. During conversion, the PDF source file needs to be retrieved from this directory. The conversion instance group also retrieves PDF file identifier attributes, file size, total number of pages, thread-safe local locks, and read lock-related attribute fields. The read lock is used to record the number of conversion tasks, allowing the conversion instance expiration monitoring thread to know whether a corresponding conversion instance still has conversion tasks.
[0079] The mapping between the current PDF's identifier attributes and conversion instance groups is recorded in a ConcurrentHashMap collection. Subsequent conversion tasks for the same PDF will retrieve the conversion instance group from this collection, eliminating the need to repeatedly construct the conversion instance group. Alternatively, a Map collection with soft references can be used for storage and management. The advantage of using a Map collection with soft references is that data in the collection that is not referenced will be automatically reclaimed when the JVM is full, ensuring timely release of unused memory. An expiration monitoring request for the conversion instance group is constructed and submitted to the conversion instance expiration monitoring thread (time wheel technology).
[0080] The system checks the read locks in the conversion instance group to determine if any conversion tasks remain. Essentially, read locks track the number of thread tasks currently performing conversions, allowing the system to determine if there are any unfinished conversion requests or tasks for the current PDF file.
[0081] If there are still unfinished transformation tasks, reset the timer for the transformation instance group and continue monitoring the current transformation instance.
[0082] If no transformation task exists, check if the timer for the transformation instance group has expired. If it has expired, release the transformation instance group and the transformation instances within it; if it has not expired, continue monitoring the current transformation instance group.
[0083] Upon receiving a conversion task, the system retrieves the corresponding conversion instance group from the Map collection based on the PDF's identifier attributes. Then, it acquires the read lock created during the build process from this conversion instance group. After acquiring the read lock (which must be released upon completion of the conversion), the task count recorded by the read lock is incremented, and the construction and acquisition of conversion instances begins. A conversion instance group can contain multiple conversion instances. The conversion instance group stores the PDF's general attribute data and the conversion instances themselves. These conversion instances are used to convert the images.
[0084] The identification attribute of the conversion instance is constructed based on the PDF's identification attribute and a sequential number. Then, a lock object is constructed or obtained based on this identification attribute. Finally, the lock object is used to ensure and control the number of conversion instances for each PDF.
[0085] The thread that acquires the lock can then construct the corresponding transformation instance. Threads that are not eligible to acquire the lock wait for the thread that acquires the lock to complete the construction of the transformation instance.
[0086] Subsequent PDF conversion task requests can retrieve conversion instances from the conversion instance group based on the PDF's identifier attributes to perform image conversion.
[0087] The read lock is released when the conversion request task is completed. The task count recorded by the read lock is decremented by one, and the conversion process is complete.
[0088] Please see Figure 3 The diagram shown is a structural schematic of a file preview image conversion device provided in an embodiment of this application; this embodiment of the application provides a file preview image conversion device 200, including:
[0089] The acquisition module 210 is used to acquire a file conversion request; the file conversion request includes a file to be converted; the file to be converted includes a file identifier;
[0090] The conversion instance group module 220 is used to obtain the conversion instance group corresponding to the file to be converted based on the file identifier; the conversion instance group is constructed by the build thread corresponding to the pre-created file to be converted; the conversion instance group is used to store the attribute information of the file to be converted;
[0091] Image conversion module 230 is used to convert images of conversion instances in the conversion instance group to obtain a preview image corresponding to the file to be converted.
[0092] Optionally, in this embodiment of the application, the file preview image conversion device, the conversion instance group module 220, is further configured to create a construction thread corresponding to the file to be converted; the construction thread is configured to construct a conversion instance group corresponding to the file to be converted; obtain the attribute fields corresponding to the file to be converted; construct the conversion instance group according to the attribute fields corresponding to the file to be converted using the construction thread; store the mapping relationship between the file identifier of the file to be converted and the conversion instance group in a hash set; and obtain the conversion instance group corresponding to the file to be converted in the hash set based on the file identifier.
[0093] Optionally, in this embodiment, the file preview image conversion device, the conversion instance group module 220, is further used to construct a lock object; the lock object is used to control the conversion instance group to be constructed by a corresponding construction thread; based on the lock object, it is determined whether the construction thread has acquired lock eligibility; if the construction thread has acquired lock eligibility, the construction thread constructs the conversion instance group according to the attribute fields corresponding to the file to be converted; if the construction thread has not acquired lock eligibility, the state of the construction thread is changed to blocked waiting.
[0094] Optionally, in this embodiment of the application, the file preview image conversion device, image conversion module 230, is used to use threads that have not acquired lock qualifications as conversion threads; the conversion threads are used to perform image conversion on the conversion instances; using the conversion threads, the conversion instances in the conversion instance group are converted into images to obtain the preview image corresponding to the file to be converted.
[0095] Optionally, in this embodiment of the application, the file preview image conversion device further includes: a quantity control module, used to obtain the read lock attribute in the attribute information of the file to be converted, the read lock attribute being used to count the number of conversion instances that have completed image conversion; and based on the total number of conversion instances in the conversion instance group and the read lock attribute, determining that all conversion instances in the conversion instance group have completed image conversion.
[0096] Optionally, in this embodiment of the application, the file preview image conversion device and the quantity control module are further configured to obtain a timer corresponding to a preset conversion instance; the timer is used to determine whether the conversion instance has expired; based on the total number of conversion instances in the conversion instance group and the read lock attribute, it is determined whether the current conversion instance is the last conversion instance to perform image conversion; if the current conversion instance is the last conversion instance to perform image conversion, then if the timer corresponding to the current conversion instance has expired, the read lock is released and the conversion task corresponding to the file to be converted is terminated; if the current conversion instance is not the last conversion instance to perform image conversion, then the timer of the current conversion instance is reset.
[0097] Optionally, in this embodiment of the application, the file preview image conversion device further includes: a file format verification module, used to perform format verification on the file to be converted in the file conversion request and obtain a format verification result; if the format verification result passes, to perform the conversion instance group corresponding to the file to be converted based on the file identifier; if the format verification result fails, to output the format verification result.
[0098] It should be understood that this device corresponds to the above-described document preview image conversion method embodiment and is capable of performing the various steps involved in the above method embodiment. The specific functions of this device can be found in the description above, and detailed descriptions are omitted here to avoid repetition. The device includes at least one software function module that can be stored in memory or embedded in the device's operating system (OS) in the form of software or firmware.
[0099] Please see Figure 4The diagram shows a structural schematic of an electronic device provided in an embodiment of this application. An electronic device 300 provided in this application includes a processor 310 and a memory 320. The memory 320 stores machine-readable instructions executable by the processor 310. When the machine-readable instructions are executed by the processor 310, the method described above is performed.
[0100] This application also provides a storage medium storing a computer program, which is executed by a processor to perform the above-described method.
[0101] The storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read Only Memory (EPROM), Programmable Red-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0102] It should be understood that the disclosed apparatus and methods can also be implemented in other ways, given the several embodiments provided in this application. The apparatus embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code, which contains one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, or they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0103] In addition, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0104] The above description is only an optional implementation of the embodiments of this application, but the protection scope of the embodiments of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the embodiments of this application should be covered within the protection scope of the embodiments of this application.
Claims
1. A method for converting file preview images, characterized in that, include: Obtain a file conversion request; the file conversion request includes a file to be converted; the file to be converted includes a file identifier; The conversion instance group corresponding to the file to be converted is obtained based on the file identifier; the conversion instance group is constructed by a pre-created build thread corresponding to the file to be converted; the conversion instance group is used to store the attribute information of the file to be converted; Threads that do not acquire the lock are designated as conversion threads; these conversion threads are used to perform image conversion on the conversion instances. Using the conversion thread, the conversion instances in the conversion instance group are converted into images to obtain the preview image corresponding to the file to be converted; The method further includes: using a Map collection with soft references to store and manage the mapping relationship between the file identifiers of the files to be converted and the conversion instance group; the soft reference is a Java object reference method; After performing image conversion on the conversion instances in the conversion instance group, the method further includes: Obtain the read lock attribute from the attribute information of the file to be converted. The read lock attribute is used to count the number of conversion instances that have completed the image conversion. Based on the total number of conversion instances in the conversion instance group and the read lock attribute, it is determined that all conversion instances in the conversion instance group have completed image conversion; The step of determining that all conversion instances in the conversion instance group have completed image conversion based on the total number of conversion instances in the conversion instance group and the read lock attribute includes: Obtain a timer corresponding to the preset conversion instance; the timer is used to determine whether the conversion instance has expired; Based on the total number of conversion instances in the conversion instance group and the read lock attribute, determine whether the current conversion instance is the last conversion instance to perform image conversion; If the current conversion instance is the last conversion instance to perform image conversion, then if the timer corresponding to the current conversion instance expires, the read lock is released and the conversion task corresponding to the file to be converted is terminated. If the current conversion instance is not the last conversion instance to perform image conversion, then the timer for the current conversion instance is reset; When creating the build thread corresponding to the file to be converted, a time wheel technique is used; the build thread is a time wheel thread; the time wheel technique is used to execute multiple delayed tasks with different delay times using a small number of threads.
2. The method according to claim 1, characterized in that, The step of obtaining the conversion instance group corresponding to the file to be converted based on the file identifier includes: Create the build thread corresponding to the file to be converted; the build thread is used to construct the conversion instance group corresponding to the file to be converted. Obtain the attribute fields corresponding to the file to be converted; The conversion instance group is constructed using the construction thread based on the attribute fields corresponding to the file to be converted; Store the mapping relationship between the file identifier of the file to be converted and the conversion instance group in a hash set; Based on the file identifier, the conversion instance group corresponding to the file to be converted is obtained from the hash set.
3. The method according to claim 2, characterized in that, The step of constructing the conversion instance group using the construction thread based on the attribute fields corresponding to the file to be converted includes: Construct a lock object; the lock object is used to control that the transformation instance group is constructed by a corresponding construction thread; Based on the lock object, determine whether the construction thread is qualified to acquire the lock; If the build thread acquires the lock qualification, then the build thread constructs the conversion instance group according to the attribute fields corresponding to the file to be converted; If the construction thread is not qualified to acquire the lock, the state of the construction thread is changed to blocked waiting.
4. The method according to claim 1, characterized in that, After obtaining the file conversion request, the method further includes: Perform format verification on the file to be converted in the file conversion request, and obtain the format verification result; If the format verification result passes, the process of obtaining the conversion instance group corresponding to the file to be converted based on the file identifier is executed; If the format verification result fails, the format verification result is output.
5. A file preview image conversion device, characterized in that, include: The acquisition module is used to acquire file conversion requests; the file conversion request includes a file to be converted; the file to be converted includes a file identifier; The conversion instance group module is used to obtain the conversion instance group corresponding to the file to be converted based on the file identifier; the conversion instance group is constructed by a pre-created build thread corresponding to the file to be converted; the conversion instance group is used to store the attribute information of the file to be converted; The image conversion module is used to convert the conversion instances in the conversion instance group into images to obtain a preview image corresponding to the file to be converted. The image conversion module is specifically used to use threads that have not acquired lock privileges as conversion threads; the conversion threads are used to perform image conversion on the conversion instances; using the conversion threads, the conversion instances in the conversion instance group are converted to obtain the preview image corresponding to the file to be converted; The device is also used to store and manage the mapping relationship between the file identifiers of the files to be converted and the conversion instance groups using a Map collection with soft references; the soft reference is a way of referencing objects in Java; The quantity control module is used to obtain the read lock attribute from the attribute information of the file to be converted. The read lock attribute is used to count the number of conversion instances that have completed image conversion. Based on the total number of conversion instances in the conversion instance group and the read lock attribute, it is determined that all conversion instances in the conversion instance group have completed image conversion. The quantity control module is also used to obtain a timer corresponding to the preset conversion instance; The timer is used to determine whether the conversion instance has expired; based on the total number of conversion instances in the conversion instance group and the read lock attribute, it is determined whether the current conversion instance is the last conversion instance to perform image conversion; if the current conversion instance is the last conversion instance to perform image conversion, then if the timer corresponding to the current conversion instance has expired, the read lock is released and the conversion task corresponding to the file to be converted is terminated; if the current conversion instance is not the last conversion instance to perform image conversion, then the timer of the current conversion instance is reset. When creating the build thread corresponding to the file to be converted, a time wheel technique is used; The construction thread is a time wheel thread; the time wheel technology is used to execute multiple delayed tasks with different delay times using a small number of threads.
6. An electronic device, characterized in that, include: A processor and a memory, the memory storing machine-readable instructions executable by the processor, which, when executed by the processor, perform the method as described in any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, performs the method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
File conversion management method and device for online preview, equipment and medium
CN114528813A
File previewing method, device and equipment and computer readable storage medium
CN115145873A